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 );
61 return Toolkit::TextField::New();
64 TypeRegistration mType( typeid(Toolkit::TextField), typeid(Toolkit::Control), Create );
66 PropertyRegistration property1( mType, "placeholder-text", Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
67 PropertyRegistration property2( mType, "text", Toolkit::TextField::PROPERTY_TEXT, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
68 PropertyRegistration property3( mType, "cursor-image", Toolkit::TextField::PROPERTY_CURSOR_IMAGE, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
69 PropertyRegistration property4( mType, "primary-cursor-color", Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR, Property::VECTOR4, &TextField::SetProperty, &TextField::GetProperty );
70 PropertyRegistration property5( mType, "secondary-cursor-color", Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR, Property::VECTOR4, &TextField::SetProperty, &TextField::GetProperty );
71 PropertyRegistration property6( mType, "enable-cursor-blink", Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK, Property::BOOLEAN, &TextField::SetProperty, &TextField::GetProperty );
72 PropertyRegistration property7( mType, "cursor-blink-interval", Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL, Property::FLOAT, &TextField::SetProperty, &TextField::GetProperty );
73 PropertyRegistration property8( mType, "cursor-blink-duration", Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION, Property::FLOAT, &TextField::SetProperty, &TextField::GetProperty );
77 Toolkit::TextField TextField::New()
79 // Create the implementation, temporarily owned by this handle on stack
80 IntrusivePtr< TextField > impl = new TextField();
82 // Pass ownership to CustomActor handle
83 Toolkit::TextField handle( *impl );
85 // Second-phase init of the implementation
86 // This can only be done after the CustomActor connection has been made...
92 void TextField::SetRenderer( Text::RendererPtr renderer )
97 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
99 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
103 TextField& impl( GetImpl( textField ) );
107 case Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT:
109 if( impl.mController )
111 //impl.mController->SetPlaceholderText( value.Get< std::string >() ); TODO
115 case Toolkit::TextField::PROPERTY_TEXT:
117 if( impl.mController )
119 impl.mController->SetText( value.Get< std::string >() );
123 case Toolkit::TextField::PROPERTY_CURSOR_IMAGE:
125 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
127 if( impl.mDecorator )
129 impl.mDecorator->SetCursorImage( image );
133 case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
135 if( impl.mDecorator )
137 impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
141 case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
143 if( impl.mDecorator )
145 impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
149 case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
151 if( impl.mController )
153 //impl.mController->SetEnableCursorBlink( value.Get< bool >() ); TODO
157 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
159 if( impl.mDecorator )
161 impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
165 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
167 if( impl.mDecorator )
169 impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
177 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
179 Property::Value value;
181 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
185 TextField& impl( GetImpl( textField ) );
189 case Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT:
191 DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
194 case Toolkit::TextField::PROPERTY_TEXT:
196 DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
199 case Toolkit::TextField::PROPERTY_CURSOR_IMAGE:
201 if( impl.mDecorator )
203 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
206 value = image.GetUrl();
211 case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
213 if( impl.mDecorator )
215 value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
219 case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
221 if( impl.mDecorator )
223 value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
227 case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
229 //value = impl.mController->GetEnableCursorBlink(); TODO
232 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
234 if( impl.mDecorator )
236 value = impl.mDecorator->GetCursorBlinkInterval();
240 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
242 if( impl.mDecorator )
244 value = impl.mDecorator->GetCursorBlinkDuration();
254 void TextField::OnInitialize()
256 mDecorator = Text::Decorator::New( *this );
258 mController = Text::Controller::New();
259 mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
260 //mController->EnableTextInput( mDecorator ); TODO
263 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
265 if( mController->Relayout( size ) )
269 // TODO - Get from RendererFactory
270 mRenderer = Dali::Toolkit::Text::BasicRenderer::New();
275 Actor renderableActor = mRenderer->Render( mController->GetView() );
277 if( renderableActor )
279 Self().Add( renderableActor );
285 TextField::TextField()
286 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) )
290 TextField::~TextField()
294 } // namespace Internal
296 } // namespace Toolkit