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/basic/text-basic-renderer.h> // TODO - Get from RendererFactory
30 using namespace Dali::Toolkit::Text;
43 const Property::Index TextField::PROPERTY_PLACEHOLDER_TEXT( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX );
44 const Property::Index TextField::PROPERTY_TEXT( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 1 );
45 const Property::Index TextField::PROPERTY_CURSOR_IMAGE( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 2 );
46 const Property::Index TextField::PROPERTY_PRIMARY_CURSOR_COLOR( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 3 );
47 const Property::Index TextField::PROPERTY_SECONDARY_CURSOR_COLOR( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 4 );
48 const Property::Index TextField::PROPERTY_ENABLE_CURSOR_BLINK( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 5 );
49 const Property::Index TextField::PROPERTY_CURSOR_BLINK_INTERVAL( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 6 );
50 const Property::Index TextField::PROPERTY_CURSOR_BLINK_DURATION( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 7 );
51 const Property::Index TextField::PROPERTY_GRAB_HANDLE_IMAGE( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 8 );
62 return Toolkit::TextField::New();
65 TypeRegistration mType( typeid(Toolkit::TextField), typeid(Toolkit::Control), Create );
67 PropertyRegistration property1( mType, "placeholder-text", Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
68 PropertyRegistration property2( mType, "text", Toolkit::TextField::PROPERTY_TEXT, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
69 PropertyRegistration property3( mType, "cursor-image", Toolkit::TextField::PROPERTY_CURSOR_IMAGE, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
70 PropertyRegistration property4( mType, "primary-cursor-color", Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR, Property::VECTOR4, &TextField::SetProperty, &TextField::GetProperty );
71 PropertyRegistration property5( mType, "secondary-cursor-color", Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR, Property::VECTOR4, &TextField::SetProperty, &TextField::GetProperty );
72 PropertyRegistration property6( mType, "enable-cursor-blink", Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK, Property::BOOLEAN, &TextField::SetProperty, &TextField::GetProperty );
73 PropertyRegistration property7( mType, "cursor-blink-interval", Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL, Property::FLOAT, &TextField::SetProperty, &TextField::GetProperty );
74 PropertyRegistration property8( mType, "cursor-blink-duration", Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION, Property::FLOAT, &TextField::SetProperty, &TextField::GetProperty );
75 PropertyRegistration property9( mType, "grab-handle-image", Toolkit::TextField::PROPERTY_GRAB_HANDLE_IMAGE, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
79 Toolkit::TextField TextField::New()
81 // Create the implementation, temporarily owned by this handle on stack
82 IntrusivePtr< TextField > impl = new TextField();
84 // Pass ownership to CustomActor handle
85 Toolkit::TextField handle( *impl );
87 // Second-phase init of the implementation
88 // This can only be done after the CustomActor connection has been made...
94 void TextField::SetRenderer( Text::RendererPtr renderer )
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_PLACEHOLDER_TEXT:
111 if( impl.mController )
113 //impl.mController->SetPlaceholderText( value.Get< std::string >() ); TODO
117 case Toolkit::TextField::PROPERTY_TEXT:
119 if( impl.mController )
121 impl.mController->SetText( value.Get< std::string >() );
125 case Toolkit::TextField::PROPERTY_CURSOR_IMAGE:
127 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
129 if( impl.mDecorator )
131 impl.mDecorator->SetCursorImage( image );
135 case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
137 if( impl.mDecorator )
139 impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
143 case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
145 if( impl.mDecorator )
147 impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
151 case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
153 if( impl.mController )
155 //impl.mController->SetEnableCursorBlink( value.Get< bool >() ); TODO
159 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
161 if( impl.mDecorator )
163 impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
167 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
169 if( impl.mDecorator )
171 impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
175 case Toolkit::TextField::PROPERTY_GRAB_HANDLE_IMAGE:
177 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
179 if( impl.mDecorator )
181 impl.mDecorator->SetGrabHandleImage( image );
189 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
191 Property::Value value;
193 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
197 TextField& impl( GetImpl( textField ) );
201 case Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT:
203 DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
206 case Toolkit::TextField::PROPERTY_TEXT:
208 DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
211 case Toolkit::TextField::PROPERTY_CURSOR_IMAGE:
213 if( impl.mDecorator )
215 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
218 value = image.GetUrl();
223 case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
225 if( impl.mDecorator )
227 value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
231 case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
233 if( impl.mDecorator )
235 value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
239 case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
241 //value = impl.mController->GetEnableCursorBlink(); TODO
244 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
246 if( impl.mDecorator )
248 value = impl.mDecorator->GetCursorBlinkInterval();
252 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
254 if( impl.mDecorator )
256 value = impl.mDecorator->GetCursorBlinkDuration();
260 case Toolkit::TextField::PROPERTY_GRAB_HANDLE_IMAGE:
262 if( impl.mDecorator )
264 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
267 value = image.GetUrl();
278 void TextField::OnInitialize()
280 mController = Text::Controller::New( *this );
282 mDecorator = Text::Decorator::New( *this, *mController );
284 mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
286 mController->EnableTextInput( mDecorator );
288 // Forward input events to controller
289 EnableGestureDetection( Gesture::Tap );
292 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
294 if( mController->Relayout( size ) )
298 // TODO - Get from RendererFactory
299 mRenderer = Dali::Toolkit::Text::BasicRenderer::New();
304 Actor renderableActor = mRenderer->Render( mController->GetView() );
306 if( renderableActor )
308 Self().Add( renderableActor );
314 void TextField::OnTap( const TapGesture& tap )
316 mController->TapEvent( tap.localPoint.x, tap.localPoint.y );
319 void TextField::RequestTextRelayout()
324 TextField::TextField()
325 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) )
329 TextField::~TextField()
333 } // namespace Internal
335 } // namespace Toolkit