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 ) ||
456 mDecorator->Relayout( size );
461 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
464 RenderableActor renderableActor;
467 renderableActor = mRenderer->Render( mController->GetView() );
470 EnableClipping( (Dali::Toolkit::TextField::EXCEED_POLICY_CLIP == mExceedPolicy), size );
472 if( renderableActor != mRenderableActor )
474 UnparentAndReset( mRenderableActor );
475 mRenderableActor = renderableActor;
478 if( mRenderableActor )
480 const Vector2 offset = mController->GetScrollPosition() + mController->GetAlignmentOffset();
482 mRenderableActor.SetPosition( offset.x, offset.y );
484 // Make sure the actor is parented correctly with/without clipping
487 mClipper->GetRootActor().Add( mRenderableActor );
491 Self().Add( mRenderableActor );
497 void TextField::OnKeyInputFocusGained()
499 VirtualKeyboard::StatusChangedSignal().Connect( this, &TextField::KeyboardStatusChanged );
501 ImfManager imfManager = ImfManager::Get();
505 imfManager.EventReceivedSignal().Connect( this, &TextField::OnImfEvent );
507 // Notify that the text editing start.
508 imfManager.Activate();
510 // When window gain lost focus, the imf manager is deactivated. Thus when window gain focus again, the imf manager must be activated.
511 imfManager.SetRestoreAfterFocusLost( true );
514 mController->KeyboardFocusGainEvent();
516 EmitKeyInputFocusSignal( true ); // Calls back into the Control hence done last.
519 void TextField::OnKeyInputFocusLost()
521 VirtualKeyboard::StatusChangedSignal().Disconnect( this, &TextField::KeyboardStatusChanged );
523 ImfManager imfManager = ImfManager::Get();
526 // The text editing is finished. Therefore the imf manager don't have restore activation.
527 imfManager.SetRestoreAfterFocusLost( false );
529 // Notify that the text editing finish.
530 imfManager.Deactivate();
532 imfManager.EventReceivedSignal().Disconnect( this, &TextField::OnImfEvent );
535 mController->KeyboardFocusLostEvent();
537 EmitKeyInputFocusSignal( false ); // Calls back into the Control hence done last.
540 void TextField::OnTap( const TapGesture& gesture )
542 // Show the keyboard if it was hidden.
543 if (!VirtualKeyboard::IsVisible())
545 VirtualKeyboard::Show();
550 mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
553 void TextField::OnPan( const PanGesture& gesture )
555 mController->PanEvent( gesture.state, gesture.displacement );
558 bool TextField::OnKeyEvent( const KeyEvent& event )
560 if( Dali::DALI_KEY_ESCAPE == event.keyCode )
562 ClearKeyInputFocus();
565 return mController->KeyEvent( event );
568 ImfManager::ImfCallbackData TextField::OnImfEvent( Dali::ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
570 switch ( imfEvent.eventName )
572 case ImfManager::COMMIT:
574 KeyEvent event( "", imfEvent.predictiveString, 0, 0, 0, KeyEvent::Down );
575 mController->KeyEvent( event );
578 case ImfManager::PREEDIT: // fall through
579 case ImfManager::DELETESURROUNDING:
580 case ImfManager::GETSURROUNDING:
581 case ImfManager::VOID:
587 return ImfManager::ImfCallbackData();
590 void TextField::RequestTextRelayout()
595 void TextField::EnableClipping( bool clipping, const Vector2& size )
599 // Not worth to created clip actor if width or height is equal to zero.
600 if( size.width > Math::MACHINE_EPSILON_1000 && size.height > Math::MACHINE_EPSILON_1000 )
606 mClipper = Clipper::New( size );
607 self.Add( mClipper->GetRootActor() );
608 self.Add( mClipper->GetImageActor() );
612 mClipper->Refresh( size );
618 // Note - this will automatically remove the root & image actors
623 void TextField::KeyboardStatusChanged(bool keyboardShown)
625 // Just hide the grab handle when keyboard is hidden.
628 mController->KeyboardFocusLostEvent();
632 TextField::TextField()
633 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
634 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
635 mExceedPolicy( Dali::Toolkit::TextField::EXCEED_POLICY_CLIP )
639 TextField::~TextField()
644 } // namespace Internal
646 } // namespace Toolkit