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/integration-api/debug.h>
27 #include <dali-toolkit/public-api/text/layouts/layout-engine.h>
28 #include <dali-toolkit/public-api/text/rendering/text-backend.h>
30 using namespace Dali::Toolkit::Text;
35 const unsigned int DEFAULT_RENDERING_BACKEND = 0;
46 const Property::Index TextField::PROPERTY_RENDERING_BACKEND( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX );
47 const Property::Index TextField::PROPERTY_PLACEHOLDER_TEXT( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 1 );
48 const Property::Index TextField::PROPERTY_TEXT( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 2 );
49 const Property::Index TextField::PROPERTY_CURSOR_IMAGE( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 3 );
50 const Property::Index TextField::PROPERTY_PRIMARY_CURSOR_COLOR( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 4 );
51 const Property::Index TextField::PROPERTY_SECONDARY_CURSOR_COLOR( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 5 );
52 const Property::Index TextField::PROPERTY_ENABLE_CURSOR_BLINK( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 6 );
53 const Property::Index TextField::PROPERTY_CURSOR_BLINK_INTERVAL( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 7 );
54 const Property::Index TextField::PROPERTY_CURSOR_BLINK_DURATION( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 8 );
55 const Property::Index TextField::PROPERTY_GRAB_HANDLE_IMAGE( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 9 );
66 return Toolkit::TextField::New();
69 TypeRegistration mType( typeid(Toolkit::TextField), typeid(Toolkit::Control), Create );
71 PropertyRegistration property01( mType, "rendering-backend", Toolkit::TextField::PROPERTY_RENDERING_BACKEND, Property::UNSIGNED_INTEGER, &TextField::SetProperty, &TextField::GetProperty );
72 PropertyRegistration property02( mType, "placeholder-text", Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
73 PropertyRegistration property03( mType, "text", Toolkit::TextField::PROPERTY_TEXT, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
74 PropertyRegistration property04( mType, "cursor-image", Toolkit::TextField::PROPERTY_CURSOR_IMAGE, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
75 PropertyRegistration property05( mType, "primary-cursor-color", Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR, Property::VECTOR4, &TextField::SetProperty, &TextField::GetProperty );
76 PropertyRegistration property06( mType, "secondary-cursor-color", Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR, Property::VECTOR4, &TextField::SetProperty, &TextField::GetProperty );
77 PropertyRegistration property07( mType, "enable-cursor-blink", Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK, Property::BOOLEAN, &TextField::SetProperty, &TextField::GetProperty );
78 PropertyRegistration property08( mType, "cursor-blink-interval", Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL, Property::FLOAT, &TextField::SetProperty, &TextField::GetProperty );
79 PropertyRegistration property09( mType, "cursor-blink-duration", Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION, Property::FLOAT, &TextField::SetProperty, &TextField::GetProperty );
80 PropertyRegistration property10( mType, "grab-handle-image", Toolkit::TextField::PROPERTY_GRAB_HANDLE_IMAGE, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
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 unsigned int backend = value.Get< unsigned 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_CURSOR_IMAGE:
138 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
140 if( impl.mDecorator )
142 impl.mDecorator->SetCursorImage( image );
146 case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
148 if( impl.mDecorator )
150 impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
154 case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
156 if( impl.mDecorator )
158 impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
162 case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
164 if( impl.mController )
166 //impl.mController->SetEnableCursorBlink( value.Get< bool >() ); TODO
170 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
172 if( impl.mDecorator )
174 impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
178 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
180 if( impl.mDecorator )
182 impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
186 case Toolkit::TextField::PROPERTY_GRAB_HANDLE_IMAGE:
188 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
190 if( impl.mDecorator )
192 impl.mDecorator->SetGrabHandleImage( image );
200 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
202 Property::Value value;
204 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
208 TextField& impl( GetImpl( textField ) );
212 case Toolkit::TextField::PROPERTY_RENDERING_BACKEND:
214 value = impl.mRenderingBackend;
217 case Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT:
219 DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
222 case Toolkit::TextField::PROPERTY_TEXT:
224 DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
227 case Toolkit::TextField::PROPERTY_CURSOR_IMAGE:
229 if( impl.mDecorator )
231 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
234 value = image.GetUrl();
239 case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
241 if( impl.mDecorator )
243 value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
247 case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
249 if( impl.mDecorator )
251 value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
255 case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
257 //value = impl.mController->GetEnableCursorBlink(); TODO
260 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
262 if( impl.mDecorator )
264 value = impl.mDecorator->GetCursorBlinkInterval();
268 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
270 if( impl.mDecorator )
272 value = impl.mDecorator->GetCursorBlinkDuration();
276 case Toolkit::TextField::PROPERTY_GRAB_HANDLE_IMAGE:
278 if( impl.mDecorator )
280 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
283 value = image.GetUrl();
294 void TextField::OnInitialize()
296 mController = Text::Controller::New( *this );
298 mDecorator = Text::Decorator::New( *this, *mController );
300 mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
302 mController->EnableTextInput( mDecorator );
304 // Forward input events to controller
305 EnableGestureDetection( Gesture::Tap );
307 // TODO - Fix TapGestureDetector to support single and double tap
308 mDoubleTapDetector = TapGestureDetector::New();
309 mDoubleTapDetector.SetTapsRequired( 2 );
310 mDoubleTapDetector.DetectedSignal().Connect( this, &TextField::OnDoubleTap );
311 mDoubleTapDetector.Attach(Self());
314 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
316 if( mController->Relayout( size ) )
320 mDecorator->Relayout( size );
325 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
330 Actor renderableActor = mRenderer->Render( mController->GetView() );
332 if( renderableActor )
334 Self().Add( renderableActor );
340 void TextField::OnTap( const TapGesture& tap )
342 mController->TapEvent( tap.numberOfTaps, tap.localPoint.x, tap.localPoint.y );
345 void TextField::OnDoubleTap( Actor actor, const TapGesture& tap )
347 mController->TapEvent( tap.numberOfTaps, tap.localPoint.x, tap.localPoint.y );
350 void TextField::RequestTextRelayout()
355 TextField::TextField()
356 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) ),
357 mRenderingBackend( DEFAULT_RENDERING_BACKEND )
361 TextField::~TextField()
365 } // namespace Internal
367 } // namespace Toolkit