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;
43 const Property::Index TextField::PROPERTY_RENDERING_BACKEND( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX );
44 const Property::Index TextField::PROPERTY_PLACEHOLDER_TEXT( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 1 );
45 const Property::Index TextField::PROPERTY_TEXT( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 2 );
46 const Property::Index TextField::PROPERTY_CURSOR_IMAGE( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 3 );
47 const Property::Index TextField::PROPERTY_PRIMARY_CURSOR_COLOR( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 4 );
48 const Property::Index TextField::PROPERTY_SECONDARY_CURSOR_COLOR( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 5 );
49 const Property::Index TextField::PROPERTY_ENABLE_CURSOR_BLINK( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 6 );
50 const Property::Index TextField::PROPERTY_CURSOR_BLINK_INTERVAL( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 7 );
51 const Property::Index TextField::PROPERTY_CURSOR_BLINK_DURATION( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 8 );
52 const Property::Index TextField::PROPERTY_GRAB_HANDLE_IMAGE( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 9 );
63 return Toolkit::TextField::New();
66 TypeRegistration mType( typeid(Toolkit::TextField), typeid(Toolkit::Control), Create );
68 PropertyRegistration property01( mType, "rendering-backend", Toolkit::TextField::PROPERTY_RENDERING_BACKEND, Property::UNSIGNED_INTEGER, &TextField::SetProperty, &TextField::GetProperty );
69 PropertyRegistration property02( mType, "placeholder-text", Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
70 PropertyRegistration property03( mType, "text", Toolkit::TextField::PROPERTY_TEXT, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
71 PropertyRegistration property04( mType, "cursor-image", Toolkit::TextField::PROPERTY_CURSOR_IMAGE, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
72 PropertyRegistration property05( mType, "primary-cursor-color", Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR, Property::VECTOR4, &TextField::SetProperty, &TextField::GetProperty );
73 PropertyRegistration property06( mType, "secondary-cursor-color", Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR, Property::VECTOR4, &TextField::SetProperty, &TextField::GetProperty );
74 PropertyRegistration property07( mType, "enable-cursor-blink", Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK, Property::BOOLEAN, &TextField::SetProperty, &TextField::GetProperty );
75 PropertyRegistration property08( mType, "cursor-blink-interval", Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL, Property::FLOAT, &TextField::SetProperty, &TextField::GetProperty );
76 PropertyRegistration property09( mType, "cursor-blink-duration", Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION, Property::FLOAT, &TextField::SetProperty, &TextField::GetProperty );
77 PropertyRegistration property10( mType, "grab-handle-image", Toolkit::TextField::PROPERTY_GRAB_HANDLE_IMAGE, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
81 Toolkit::TextField TextField::New()
83 // Create the implementation, temporarily owned by this handle on stack
84 IntrusivePtr< TextField > impl = new TextField();
86 // Pass ownership to CustomActor handle
87 Toolkit::TextField handle( *impl );
89 // Second-phase init of the implementation
90 // This can only be done after the CustomActor connection has been made...
96 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
98 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
102 TextField& impl( GetImpl( textField ) );
106 case Toolkit::TextField::PROPERTY_RENDERING_BACKEND:
108 unsigned int backend = value.Get< unsigned int >();
110 if( impl.mRenderingBackend != backend )
112 impl.mRenderingBackend = backend;
113 impl.mRenderer.Reset();
117 case Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT:
119 if( impl.mController )
121 //impl.mController->SetPlaceholderText( value.Get< std::string >() ); TODO
125 case Toolkit::TextField::PROPERTY_TEXT:
127 if( impl.mController )
129 impl.mController->SetText( value.Get< std::string >() );
133 case Toolkit::TextField::PROPERTY_CURSOR_IMAGE:
135 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
137 if( impl.mDecorator )
139 impl.mDecorator->SetCursorImage( image );
143 case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
145 if( impl.mDecorator )
147 impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
151 case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
153 if( impl.mDecorator )
155 impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
159 case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
161 if( impl.mController )
163 //impl.mController->SetEnableCursorBlink( value.Get< bool >() ); TODO
167 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
169 if( impl.mDecorator )
171 impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
175 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
177 if( impl.mDecorator )
179 impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
183 case Toolkit::TextField::PROPERTY_GRAB_HANDLE_IMAGE:
185 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
187 if( impl.mDecorator )
189 impl.mDecorator->SetGrabHandleImage( image );
197 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
199 Property::Value value;
201 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
205 TextField& impl( GetImpl( textField ) );
209 case Toolkit::TextField::PROPERTY_RENDERING_BACKEND:
211 value = impl.mRenderingBackend;
214 case Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT:
216 DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
219 case Toolkit::TextField::PROPERTY_TEXT:
221 DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
224 case Toolkit::TextField::PROPERTY_CURSOR_IMAGE:
226 if( impl.mDecorator )
228 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
231 value = image.GetUrl();
236 case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
238 if( impl.mDecorator )
240 value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
244 case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
246 if( impl.mDecorator )
248 value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
252 case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
254 //value = impl.mController->GetEnableCursorBlink(); TODO
257 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
259 if( impl.mDecorator )
261 value = impl.mDecorator->GetCursorBlinkInterval();
265 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
267 if( impl.mDecorator )
269 value = impl.mDecorator->GetCursorBlinkDuration();
273 case Toolkit::TextField::PROPERTY_GRAB_HANDLE_IMAGE:
275 if( impl.mDecorator )
277 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
280 value = image.GetUrl();
291 void TextField::OnInitialize()
293 mController = Text::Controller::New( *this );
295 mDecorator = Text::Decorator::New( *this, *mController );
297 mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
299 mController->EnableTextInput( mDecorator );
301 // Forward input events to controller
302 EnableGestureDetection( Gesture::Tap );
305 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
307 if( mController->Relayout( size ) )
311 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
316 Actor renderableActor = mRenderer->Render( mController->GetView() );
318 if( renderableActor )
320 Self().Add( renderableActor );
326 void TextField::OnTap( const TapGesture& tap )
328 mController->TapEvent( tap.localPoint.x, tap.localPoint.y );
331 void TextField::RequestTextRelayout()
336 TextField::TextField()
337 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) )
341 TextField::~TextField()
345 } // namespace Internal
347 } // namespace Toolkit