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::Alignment > ALIGNMENT_STRING_TABLE[] =
59 { "BEGIN", Toolkit::Text::LayoutEngine::ALIGN_BEGIN },
60 { "CENTER", Toolkit::Text::LayoutEngine::ALIGN_CENTER },
61 { "END", Toolkit::Text::LayoutEngine::ALIGN_END },
63 const unsigned int ALIGNMENT_STRING_TABLE_COUNT = sizeof( ALIGNMENT_STRING_TABLE ) / sizeof( ALIGNMENT_STRING_TABLE[0] );
68 return Toolkit::TextField::New();
71 // Setup properties, signals and actions using the type-registry.
72 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextField, Toolkit::Control, Create );
74 DALI_PROPERTY_REGISTRATION( TextField, "rendering-backend", INTEGER, RENDERING_BACKEND )
75 DALI_PROPERTY_REGISTRATION( TextField, "placeholder-text", STRING, PLACEHOLDER_TEXT )
76 DALI_PROPERTY_REGISTRATION( TextField, "text", STRING, TEXT )
77 DALI_PROPERTY_REGISTRATION( TextField, "font-family", STRING, FONT_FAMILY )
78 DALI_PROPERTY_REGISTRATION( TextField, "font-style", STRING, FONT_STYLE )
79 DALI_PROPERTY_REGISTRATION( TextField, "point-size", FLOAT, POINT_SIZE )
80 DALI_PROPERTY_REGISTRATION( TextField, "exceed-policy", INTEGER, EXCEED_POLICY )
81 DALI_PROPERTY_REGISTRATION( TextField, "primary-cursor-color", VECTOR4, PRIMARY_CURSOR_COLOR )
82 DALI_PROPERTY_REGISTRATION( TextField, "secondary-cursor-color", VECTOR4, SECONDARY_CURSOR_COLOR )
83 DALI_PROPERTY_REGISTRATION( TextField, "enable-cursor-blink", BOOLEAN, ENABLE_CURSOR_BLINK )
84 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-interval", FLOAT, CURSOR_BLINK_INTERVAL )
85 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-duration", FLOAT, CURSOR_BLINK_DURATION )
86 DALI_PROPERTY_REGISTRATION( TextField, "grab-handle-image", STRING, GRAB_HANDLE_IMAGE )
87 DALI_PROPERTY_REGISTRATION( TextField, "decoration bounding-box", RECTANGLE, DECORATION_BOUNDING_BOX )
88 DALI_PROPERTY_REGISTRATION( TextField, "alignment", STRING, ALIGNMENT )
89 DALI_TYPE_REGISTRATION_END()
93 Toolkit::TextField TextField::New()
95 // Create the implementation, temporarily owned by this handle on stack
96 IntrusivePtr< TextField > impl = new TextField();
98 // Pass ownership to CustomActor handle
99 Toolkit::TextField handle( *impl );
101 // Second-phase init of the implementation
102 // This can only be done after the CustomActor connection has been made...
108 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
110 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
114 TextField& impl( GetImpl( textField ) );
118 case Toolkit::TextField::Property::RENDERING_BACKEND:
120 int backend = value.Get< int >();
122 if( impl.mRenderingBackend != backend )
124 impl.mRenderingBackend = backend;
125 impl.mRenderer.Reset();
129 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
131 if( impl.mController )
133 //impl.mController->SetPlaceholderText( value.Get< std::string >() ); TODO
137 case Toolkit::TextField::Property::TEXT:
139 if( impl.mController )
141 impl.mController->SetText( value.Get< std::string >() );
145 case Toolkit::TextField::Property::FONT_FAMILY:
147 if( impl.mController )
149 std::string fontFamily = value.Get< std::string >();
151 if( impl.mController->GetDefaultFontFamily() != fontFamily )
153 impl.mController->SetDefaultFontFamily( fontFamily );
154 impl.RequestTextRelayout();
159 case Toolkit::TextField::Property::FONT_STYLE:
161 if( impl.mController )
163 std::string fontStyle = value.Get< std::string >();
165 if( impl.mController->GetDefaultFontStyle() != fontStyle )
167 impl.mController->SetDefaultFontStyle( fontStyle );
168 impl.RequestTextRelayout();
173 case Toolkit::TextField::Property::POINT_SIZE:
175 if( impl.mController )
177 float pointSize = value.Get< float >();
179 if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
181 impl.mController->SetDefaultPointSize( pointSize );
182 impl.RequestTextRelayout();
187 case Toolkit::TextField::Property::EXCEED_POLICY:
189 impl.mExceedPolicy = value.Get< int >();
192 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
194 if( impl.mDecorator )
196 impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
200 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
202 if( impl.mDecorator )
204 impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
208 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
210 if( impl.mController )
212 impl.mController->SetEnableCursorBlink( value.Get< bool >() );
216 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
218 if( impl.mDecorator )
220 impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
224 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
226 if( impl.mDecorator )
228 impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
232 case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
234 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
236 if( impl.mDecorator )
238 impl.mDecorator->SetGrabHandleImage( image );
242 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
244 if( impl.mDecorator )
246 impl.mDecorator->SetBoundingBox( value.Get< Rect<int> >() );
250 case Toolkit::TextField::Property::ALIGNMENT:
252 LayoutEngine& engine = impl.mController->GetLayoutEngine();
253 const LayoutEngine::Alignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::Alignment >( value.Get< std::string >().c_str(),
254 ALIGNMENT_STRING_TABLE,
255 ALIGNMENT_STRING_TABLE_COUNT );
257 if( engine.GetAlignment() != alignment )
259 engine.SetAlignment( alignment );
260 impl.RequestTextRelayout();
268 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
270 Property::Value value;
272 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
276 TextField& impl( GetImpl( textField ) );
280 case Toolkit::TextField::Property::RENDERING_BACKEND:
282 value = impl.mRenderingBackend;
285 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
287 if( impl.mController )
290 impl.mController->GetPlaceholderText( text );
295 case Toolkit::TextField::Property::TEXT:
297 if( impl.mController )
300 impl.mController->GetText( text );
305 case Toolkit::TextField::Property::EXCEED_POLICY:
307 value = impl.mExceedPolicy;
310 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
312 if( impl.mDecorator )
314 value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
318 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
320 if( impl.mDecorator )
322 value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
326 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
328 value = impl.mController->GetEnableCursorBlink();
331 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
333 if( impl.mDecorator )
335 value = impl.mDecorator->GetCursorBlinkInterval();
339 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
341 if( impl.mDecorator )
343 value = impl.mDecorator->GetCursorBlinkDuration();
347 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
349 if( impl.mDecorator )
351 value = impl.mDecorator->GetBoundingBox();
355 case Toolkit::TextField::Property::ALIGNMENT:
357 if( impl.mController )
359 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::Alignment >( impl.mController->GetLayoutEngine().GetAlignment(),
360 ALIGNMENT_STRING_TABLE,
361 ALIGNMENT_STRING_TABLE_COUNT ) );
371 void TextField::OnInitialize()
373 mController = Text::Controller::New( *this );
375 mDecorator = Text::Decorator::New( *this, *mController );
377 mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
379 mController->EnableTextInput( mDecorator );
381 // Forward input events to controller
382 EnableGestureDetection(Gesture::Tap);
383 GetTapGestureDetector().SetMaximumTapsRequired( 2 );
384 EnableGestureDetection(Gesture::Pan);
386 // Set BoundingBox to stage size if not already set.
387 if ( mDecorator->GetBoundingBox().IsEmpty() )
389 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
390 mDecorator->SetBoundingBox( Rect<int>( 0.0f, 0.0f, stageSize.width, stageSize.height ) );
394 void TextField::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
396 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
399 Vector3 TextField::GetNaturalSize()
401 return mController->GetNaturalSize();
404 float TextField::GetHeightForWidth( float width )
406 return mController->GetHeightForWidth( width );
409 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
411 if( mController->Relayout( size ) ||
414 const Vector2& scrollPosition = mController->GetScrollPosition();
415 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
417 Vector2 offset = scrollPosition + alignmentOffset;
421 mDecorator->Relayout( size, offset );
426 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
429 RenderableActor renderableActor;
432 renderableActor = mRenderer->Render( mController->GetView() );
435 EnableClipping( (Dali::Toolkit::TextField::EXCEED_POLICY_CLIP == mExceedPolicy), size );
437 if( renderableActor != mRenderableActor )
439 UnparentAndReset( mRenderableActor );
440 mRenderableActor = renderableActor;
443 if( mRenderableActor )
445 mRenderableActor.SetPosition( offset.x, offset.y );
447 // Make sure the actor is parented correctly with/without clipping
450 mClipper->GetRootActor().Add( mRenderableActor );
454 Self().Add( mRenderableActor );
460 void TextField::OnKeyInputFocusGained()
462 VirtualKeyboard::StatusChangedSignal().Connect( this, &TextField::KeyboardStatusChanged );
464 ImfManager imfManager = ImfManager::Get();
468 imfManager.EventReceivedSignal().Connect( this, &TextField::OnImfEvent );
470 // Notify that the text editing start.
471 imfManager.Activate();
473 // When window gain lost focus, the imf manager is deactivated. Thus when window gain focus again, the imf manager must be activated.
474 imfManager.SetRestoreAfterFocusLost( true );
477 mController->KeyboardFocusGainEvent();
480 void TextField::OnKeyInputFocusLost()
482 VirtualKeyboard::StatusChangedSignal().Disconnect( this, &TextField::KeyboardStatusChanged );
484 ImfManager imfManager = ImfManager::Get();
487 // The text editing is finished. Therefore the imf manager don't have restore activation.
488 imfManager.SetRestoreAfterFocusLost( false );
490 // Notify that the text editing finish.
491 imfManager.Deactivate();
493 imfManager.EventReceivedSignal().Disconnect( this, &TextField::OnImfEvent );
496 mController->KeyboardFocusLostEvent();
499 void TextField::OnTap( const TapGesture& gesture )
501 // Show the keyboard if it was hidden.
502 if (!VirtualKeyboard::IsVisible())
504 VirtualKeyboard::Show();
509 mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
512 void TextField::OnPan( const PanGesture& gesture )
514 mController->PanEvent( gesture.state, gesture.displacement );
517 bool TextField::OnKeyEvent( const KeyEvent& event )
519 if( Dali::DALI_KEY_ESCAPE == event.keyCode )
521 ClearKeyInputFocus();
524 return mController->KeyEvent( event );
527 ImfManager::ImfCallbackData TextField::OnImfEvent( Dali::ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
529 switch ( imfEvent.eventName )
531 case ImfManager::COMMIT:
533 KeyEvent event( "", imfEvent.predictiveString, 0, 0, 0, KeyEvent::Down );
534 mController->KeyEvent( event );
537 case ImfManager::PREEDIT: // fall through
538 case ImfManager::DELETESURROUNDING:
539 case ImfManager::GETSURROUNDING:
540 case ImfManager::VOID:
546 return ImfManager::ImfCallbackData();
549 void TextField::RequestTextRelayout()
554 void TextField::EnableClipping( bool clipping, const Vector2& size )
558 // Not worth to created clip actor if width or height is equal to zero.
559 if( size.width > Math::MACHINE_EPSILON_1000 && size.height > Math::MACHINE_EPSILON_1000 )
565 mClipper = Clipper::New( size );
566 self.Add( mClipper->GetRootActor() );
567 self.Add( mClipper->GetImageActor() );
571 mClipper->Refresh( size );
577 // Note - this will automatically remove the root & image actors
582 void TextField::KeyboardStatusChanged(bool keyboardShown)
584 // Just hide the grab handle when keyboard is hidden.
587 mController->KeyboardFocusLostEvent();
591 TextField::TextField()
592 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
593 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
594 mExceedPolicy( Dali::Toolkit::TextField::EXCEED_POLICY_CLIP )
598 TextField::~TextField()
603 } // namespace Internal
605 } // namespace Toolkit