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>
23 #include <dali/public-api/adaptor-framework/key.h>
24 #include <dali/public-api/common/stage.h>
25 #include <dali/public-api/images/resource-image.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/public-api/object/type-registry-helper.h>
28 #include <dali/public-api/scripting/scripting.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/public-api/adaptor-framework/virtual-keyboard.h>
33 #include <dali-toolkit/public-api/text/rendering-backend.h>
34 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
35 #include <dali-toolkit/internal/text/rendering/text-backend.h>
36 #include <dali-toolkit/internal/styling/style-manager-impl.h>
38 using namespace Dali::Toolkit::Text;
51 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
57 const Scripting::StringEnum< Toolkit::Text::LayoutEngine::HorizontalAlignment > HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
59 { "BEGIN", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_BEGIN },
60 { "CENTER", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_CENTER },
61 { "END", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_END },
63 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
65 const Scripting::StringEnum< Toolkit::Text::LayoutEngine::VerticalAlignment > VERTICAL_ALIGNMENT_STRING_TABLE[] =
67 { "TOP", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_TOP },
68 { "CENTER", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_CENTER },
69 { "BOTTOM", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_BOTTOM },
71 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
76 return Toolkit::TextField::New();
79 // Setup properties, signals and actions using the type-registry.
80 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextField, Toolkit::Control, Create );
82 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "rendering-backend", INTEGER, RENDERING_BACKEND )
83 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "placeholder-text", STRING, PLACEHOLDER_TEXT )
84 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "text", STRING, TEXT )
85 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "font-family", STRING, FONT_FAMILY )
86 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "font-style", STRING, FONT_STYLE )
87 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "point-size", FLOAT, POINT_SIZE )
88 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "exceed-policy", INTEGER, EXCEED_POLICY )
89 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "primary-cursor-color", VECTOR4, PRIMARY_CURSOR_COLOR )
90 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "secondary-cursor-color", VECTOR4, SECONDARY_CURSOR_COLOR )
91 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "enable-cursor-blink", BOOLEAN, ENABLE_CURSOR_BLINK )
92 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "cursor-blink-interval", FLOAT, CURSOR_BLINK_INTERVAL )
93 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "cursor-blink-duration", FLOAT, CURSOR_BLINK_DURATION )
94 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "grab-handle-image", STRING, GRAB_HANDLE_IMAGE )
95 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "decoration-bounding-box", RECTANGLE, DECORATION_BOUNDING_BOX )
96 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "horizontal-alignment", STRING, HORIZONTAL_ALIGNMENT )
97 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "vertical-alignment", STRING, VERTICAL_ALIGNMENT )
99 DALI_TYPE_REGISTRATION_END()
103 Toolkit::TextField TextField::New()
105 // Create the implementation, temporarily owned by this handle on stack
106 IntrusivePtr< TextField > impl = new TextField();
108 // Pass ownership to CustomActor handle
109 Toolkit::TextField handle( *impl );
111 // Second-phase init of the implementation
112 // This can only be done after the CustomActor connection has been made...
118 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
120 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
124 TextField& impl( GetImpl( textField ) );
128 case Toolkit::TextField::Property::RENDERING_BACKEND:
130 int backend = value.Get< int >();
132 if( impl.mRenderingBackend != backend )
134 impl.mRenderingBackend = backend;
135 impl.mRenderer.Reset();
139 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
141 if( impl.mController )
143 //impl.mController->SetPlaceholderText( value.Get< std::string >() ); TODO
147 case Toolkit::TextField::Property::TEXT:
149 if( impl.mController )
151 impl.mController->SetText( value.Get< std::string >() );
155 case Toolkit::TextField::Property::FONT_FAMILY:
157 if( impl.mController )
159 std::string fontFamily = value.Get< std::string >();
161 if( impl.mController->GetDefaultFontFamily() != fontFamily )
163 impl.mController->SetDefaultFontFamily( fontFamily );
164 impl.RequestTextRelayout();
169 case Toolkit::TextField::Property::FONT_STYLE:
171 if( impl.mController )
173 std::string fontStyle = value.Get< std::string >();
175 if( impl.mController->GetDefaultFontStyle() != fontStyle )
177 impl.mController->SetDefaultFontStyle( fontStyle );
178 impl.RequestTextRelayout();
183 case Toolkit::TextField::Property::POINT_SIZE:
185 if( impl.mController )
187 float pointSize = value.Get< float >();
189 if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
191 impl.mController->SetDefaultPointSize( pointSize );
192 impl.RequestTextRelayout();
197 case Toolkit::TextField::Property::EXCEED_POLICY:
199 impl.mExceedPolicy = value.Get< int >();
202 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
204 if( impl.mDecorator )
206 impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
210 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
212 if( impl.mDecorator )
214 impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
218 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
220 if( impl.mController )
222 impl.mController->SetEnableCursorBlink( value.Get< bool >() );
226 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
228 if( impl.mDecorator )
230 impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
234 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
236 if( impl.mDecorator )
238 impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
242 case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
244 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
246 if( impl.mDecorator )
248 impl.mDecorator->SetGrabHandleImage( image );
252 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
254 if( impl.mDecorator )
256 impl.mDecorator->SetBoundingBox( value.Get< Rect<int> >() );
260 case Toolkit::TextField::Property::HORIZONTAL_ALIGNMENT:
262 LayoutEngine& engine = impl.mController->GetLayoutEngine();
263 const LayoutEngine::HorizontalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
264 HORIZONTAL_ALIGNMENT_STRING_TABLE,
265 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
267 if( engine.GetHorizontalAlignment() != alignment )
269 engine.SetHorizontalAlignment( alignment );
270 impl.RequestTextRelayout();
274 case Toolkit::TextField::Property::VERTICAL_ALIGNMENT:
276 LayoutEngine& engine = impl.mController->GetLayoutEngine();
277 const LayoutEngine::VerticalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
278 VERTICAL_ALIGNMENT_STRING_TABLE,
279 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
281 if( engine.GetVerticalAlignment() != alignment )
283 engine.SetVerticalAlignment( alignment );
284 impl.RequestTextRelayout();
292 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
294 Property::Value value;
296 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
300 TextField& impl( GetImpl( textField ) );
304 case Toolkit::TextField::Property::RENDERING_BACKEND:
306 value = impl.mRenderingBackend;
309 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
311 if( impl.mController )
314 impl.mController->GetPlaceholderText( text );
319 case Toolkit::TextField::Property::TEXT:
321 if( impl.mController )
324 impl.mController->GetText( text );
329 case Toolkit::TextField::Property::EXCEED_POLICY:
331 value = impl.mExceedPolicy;
334 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
336 if( impl.mDecorator )
338 value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
342 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
344 if( impl.mDecorator )
346 value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
350 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
352 value = impl.mController->GetEnableCursorBlink();
355 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
357 if( impl.mDecorator )
359 value = impl.mDecorator->GetCursorBlinkInterval();
363 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
365 if( impl.mDecorator )
367 value = impl.mDecorator->GetCursorBlinkDuration();
371 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
373 if( impl.mDecorator )
375 value = impl.mDecorator->GetBoundingBox();
379 case Toolkit::TextField::Property::HORIZONTAL_ALIGNMENT:
381 if( impl.mController )
383 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetLayoutEngine().GetHorizontalAlignment(),
384 HORIZONTAL_ALIGNMENT_STRING_TABLE,
385 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT ) );
389 case Toolkit::TextField::Property::VERTICAL_ALIGNMENT:
391 if( impl.mController )
393 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetLayoutEngine().GetVerticalAlignment(),
394 VERTICAL_ALIGNMENT_STRING_TABLE,
395 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT ) );
405 void TextField::OnInitialize()
409 mController = Text::Controller::New( *this );
411 mDecorator = Text::Decorator::New( *this, *mController );
413 mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
415 mController->EnableTextInput( mDecorator );
417 // Forward input events to controller
418 EnableGestureDetection(Gesture::Tap);
419 GetTapGestureDetector().SetMaximumTapsRequired( 2 );
420 EnableGestureDetection(Gesture::Pan);
422 // Set BoundingBox to stage size if not already set.
423 if ( mDecorator->GetBoundingBox().IsEmpty() )
425 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
426 mDecorator->SetBoundingBox( Rect<int>( 0.0f, 0.0f, stageSize.width, stageSize.height ) );
429 // Fill-parent area by default
430 self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
431 self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
434 void TextField::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
436 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
439 Vector3 TextField::GetNaturalSize()
441 return mController->GetNaturalSize();
444 float TextField::GetHeightForWidth( float width )
446 return mController->GetHeightForWidth( width );
449 void TextField::OnRelayout( const Vector2& size, RelayoutContainer& container )
451 if( mController->Relayout( size ) ||
454 const Vector2& scrollPosition = mController->GetScrollPosition();
455 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
457 Vector2 offset = scrollPosition + alignmentOffset;
461 mDecorator->Relayout( size, offset );
466 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
469 RenderableActor renderableActor;
472 renderableActor = mRenderer->Render( mController->GetView() );
475 EnableClipping( (Dali::Toolkit::TextField::EXCEED_POLICY_CLIP == mExceedPolicy), size );
477 if( renderableActor != mRenderableActor )
479 UnparentAndReset( mRenderableActor );
480 mRenderableActor = renderableActor;
483 if( mRenderableActor )
485 mRenderableActor.SetPosition( offset.x, offset.y );
487 // Make sure the actor is parented correctly with/without clipping
490 mClipper->GetRootActor().Add( mRenderableActor );
494 Self().Add( mRenderableActor );
500 void TextField::OnKeyInputFocusGained()
502 VirtualKeyboard::StatusChangedSignal().Connect( this, &TextField::KeyboardStatusChanged );
504 ImfManager imfManager = ImfManager::Get();
508 imfManager.EventReceivedSignal().Connect( this, &TextField::OnImfEvent );
510 // Notify that the text editing start.
511 imfManager.Activate();
513 // When window gain lost focus, the imf manager is deactivated. Thus when window gain focus again, the imf manager must be activated.
514 imfManager.SetRestoreAfterFocusLost( true );
517 mController->KeyboardFocusGainEvent();
519 EmitKeyInputFocusSignal( true ); // Calls back into the Control hence done last.
522 void TextField::OnKeyInputFocusLost()
524 VirtualKeyboard::StatusChangedSignal().Disconnect( this, &TextField::KeyboardStatusChanged );
526 ImfManager imfManager = ImfManager::Get();
529 // The text editing is finished. Therefore the imf manager don't have restore activation.
530 imfManager.SetRestoreAfterFocusLost( false );
532 // Notify that the text editing finish.
533 imfManager.Deactivate();
535 imfManager.EventReceivedSignal().Disconnect( this, &TextField::OnImfEvent );
538 mController->KeyboardFocusLostEvent();
540 EmitKeyInputFocusSignal( false ); // Calls back into the Control hence done last.
543 void TextField::OnTap( const TapGesture& gesture )
545 // Show the keyboard if it was hidden.
546 if (!VirtualKeyboard::IsVisible())
548 VirtualKeyboard::Show();
553 mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
556 void TextField::OnPan( const PanGesture& gesture )
558 mController->PanEvent( gesture.state, gesture.displacement );
561 bool TextField::OnKeyEvent( const KeyEvent& event )
563 if( Dali::DALI_KEY_ESCAPE == event.keyCode )
565 ClearKeyInputFocus();
568 return mController->KeyEvent( event );
571 ImfManager::ImfCallbackData TextField::OnImfEvent( Dali::ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
573 switch ( imfEvent.eventName )
575 case ImfManager::COMMIT:
577 KeyEvent event( "", imfEvent.predictiveString, 0, 0, 0, KeyEvent::Down );
578 mController->KeyEvent( event );
581 case ImfManager::PREEDIT: // fall through
582 case ImfManager::DELETESURROUNDING:
583 case ImfManager::GETSURROUNDING:
584 case ImfManager::VOID:
590 return ImfManager::ImfCallbackData();
593 void TextField::RequestTextRelayout()
598 void TextField::EnableClipping( bool clipping, const Vector2& size )
602 // Not worth to created clip actor if width or height is equal to zero.
603 if( size.width > Math::MACHINE_EPSILON_1000 && size.height > Math::MACHINE_EPSILON_1000 )
609 mClipper = Clipper::New( size );
610 self.Add( mClipper->GetRootActor() );
611 self.Add( mClipper->GetImageActor() );
615 mClipper->Refresh( size );
621 // Note - this will automatically remove the root & image actors
626 void TextField::KeyboardStatusChanged(bool keyboardShown)
628 // Just hide the grab handle when keyboard is hidden.
631 mController->KeyboardFocusLostEvent();
635 TextField::TextField()
636 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
637 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
638 mExceedPolicy( Dali::Toolkit::TextField::EXCEED_POLICY_CLIP )
642 TextField::~TextField()
647 } // namespace Internal
649 } // namespace Toolkit