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, "cursor-image", STRING, CURSOR_IMAGE )
82 DALI_PROPERTY_REGISTRATION( TextField, "primary-cursor-color", VECTOR4, PRIMARY_CURSOR_COLOR )
83 DALI_PROPERTY_REGISTRATION( TextField, "secondary-cursor-color", VECTOR4, SECONDARY_CURSOR_COLOR )
84 DALI_PROPERTY_REGISTRATION( TextField, "enable-cursor-blink", BOOLEAN, ENABLE_CURSOR_BLINK )
85 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-interval", FLOAT, CURSOR_BLINK_INTERVAL )
86 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-duration", FLOAT, CURSOR_BLINK_DURATION )
87 DALI_PROPERTY_REGISTRATION( TextField, "grab-handle-image", STRING, GRAB_HANDLE_IMAGE )
88 DALI_PROPERTY_REGISTRATION( TextField, "decoration bounding-box", RECTANGLE, DECORATION_BOUNDING_BOX )
89 DALI_PROPERTY_REGISTRATION( TextField, "alignment", STRING, ALIGNMENT )
91 DALI_TYPE_REGISTRATION_END()
95 Toolkit::TextField TextField::New()
97 // Create the implementation, temporarily owned by this handle on stack
98 IntrusivePtr< TextField > impl = new TextField();
100 // Pass ownership to CustomActor handle
101 Toolkit::TextField handle( *impl );
103 // Second-phase init of the implementation
104 // This can only be done after the CustomActor connection has been made...
110 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
112 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
116 TextField& impl( GetImpl( textField ) );
120 case Toolkit::TextField::Property::RENDERING_BACKEND:
122 int backend = value.Get< int >();
124 if( impl.mRenderingBackend != backend )
126 impl.mRenderingBackend = backend;
127 impl.mRenderer.Reset();
131 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
133 if( impl.mController )
135 //impl.mController->SetPlaceholderText( value.Get< std::string >() ); TODO
139 case Toolkit::TextField::Property::TEXT:
141 if( impl.mController )
143 impl.mController->SetText( value.Get< std::string >() );
147 case Toolkit::TextField::Property::FONT_FAMILY:
149 if( impl.mController )
151 std::string fontFamily = value.Get< std::string >();
153 if( impl.mController->GetDefaultFontFamily() != fontFamily )
155 impl.mController->SetDefaultFontFamily( fontFamily );
156 impl.RequestTextRelayout();
161 case Toolkit::TextField::Property::FONT_STYLE:
163 if( impl.mController )
165 std::string fontStyle = value.Get< std::string >();
167 if( impl.mController->GetDefaultFontStyle() != fontStyle )
169 impl.mController->SetDefaultFontStyle( fontStyle );
170 impl.RequestTextRelayout();
175 case Toolkit::TextField::Property::POINT_SIZE:
177 if( impl.mController )
179 float pointSize = value.Get< float >();
181 if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
183 impl.mController->SetDefaultPointSize( pointSize );
184 impl.RequestTextRelayout();
189 case Toolkit::TextField::Property::EXCEED_POLICY:
191 impl.mExceedPolicy = value.Get< int >();
194 case Toolkit::TextField::Property::CURSOR_IMAGE:
196 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
198 if( impl.mDecorator )
200 impl.mDecorator->SetCursorImage( image );
204 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
206 if( impl.mDecorator )
208 impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
212 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
214 if( impl.mDecorator )
216 impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
220 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
222 if( impl.mController )
224 impl.mController->SetEnableCursorBlink( value.Get< bool >() );
228 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
230 if( impl.mDecorator )
232 impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
236 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
238 if( impl.mDecorator )
240 impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
244 case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
246 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
248 if( impl.mDecorator )
250 impl.mDecorator->SetGrabHandleImage( image );
254 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
256 if( impl.mDecorator )
258 impl.mDecorator->SetBoundingBox( value.Get< Rect<int> >() );
262 case Toolkit::TextField::Property::ALIGNMENT:
264 LayoutEngine& engine = impl.mController->GetLayoutEngine();
265 const LayoutEngine::Alignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::Alignment >( value.Get< std::string >().c_str(),
266 ALIGNMENT_STRING_TABLE,
267 ALIGNMENT_STRING_TABLE_COUNT );
269 if( engine.GetAlignment() != alignment )
271 engine.SetAlignment( alignment );
272 impl.RequestTextRelayout();
280 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
282 Property::Value value;
284 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
288 TextField& impl( GetImpl( textField ) );
292 case Toolkit::TextField::Property::RENDERING_BACKEND:
294 value = impl.mRenderingBackend;
297 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
299 if( impl.mController )
302 impl.mController->GetPlaceholderText( text );
307 case Toolkit::TextField::Property::TEXT:
309 if( impl.mController )
312 impl.mController->GetText( text );
317 case Toolkit::TextField::Property::EXCEED_POLICY:
319 value = impl.mExceedPolicy;
322 case Toolkit::TextField::Property::CURSOR_IMAGE:
324 if( impl.mDecorator )
326 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
329 value = image.GetUrl();
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::GRAB_HANDLE_IMAGE:
373 if( impl.mDecorator )
375 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
378 value = image.GetUrl();
383 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
385 if( impl.mDecorator )
387 value = impl.mDecorator->GetBoundingBox();
391 case Toolkit::TextField::Property::ALIGNMENT:
393 if( impl.mController )
395 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::Alignment >( impl.mController->GetLayoutEngine().GetAlignment(),
396 ALIGNMENT_STRING_TABLE,
397 ALIGNMENT_STRING_TABLE_COUNT ) );
407 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 );
421 // Set BoundingBox to stage size if not already set.
422 if ( mDecorator->GetBoundingBox().IsEmpty() )
424 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
425 mDecorator->SetBoundingBox( Rect<int>( 0.0f, 0.0f, stageSize.width, stageSize.height ) );
429 void TextField::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
431 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
434 Vector3 TextField::GetNaturalSize()
436 return mController->GetNaturalSize();
439 float TextField::GetHeightForWidth( float width )
441 return mController->GetHeightForWidth( width );
444 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
446 if( mController->Relayout( size ) ||
451 mDecorator->Relayout( size );
456 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
459 RenderableActor renderableActor;
462 renderableActor = mRenderer->Render( mController->GetView() );
465 EnableClipping( (Dali::Toolkit::TextField::EXCEED_POLICY_CLIP == mExceedPolicy), size );
467 if( renderableActor != mRenderableActor )
469 UnparentAndReset( mRenderableActor );
470 mRenderableActor = renderableActor;
473 if( mRenderableActor )
475 // Make sure the actor is parented correctly with/without clipping
478 mClipper->GetRootActor().Add( mRenderableActor );
482 Self().Add( mRenderableActor );
488 void TextField::OnKeyInputFocusGained()
490 VirtualKeyboard::StatusChangedSignal().Connect( this, &TextField::KeyboardStatusChanged );
492 ImfManager imfManager = ImfManager::Get();
496 imfManager.EventReceivedSignal().Connect( this, &TextField::OnImfEvent );
498 // Notify that the text editing start.
499 imfManager.Activate();
501 // When window gain lost focus, the imf manager is deactivated. Thus when window gain focus again, the imf manager must be activated.
502 imfManager.SetRestoreAfterFocusLost( true );
505 mController->KeyboardFocusGainEvent();
508 void TextField::OnKeyInputFocusLost()
510 VirtualKeyboard::StatusChangedSignal().Disconnect( this, &TextField::KeyboardStatusChanged );
512 ImfManager imfManager = ImfManager::Get();
515 // The text editing is finished. Therefore the imf manager don't have restore activation.
516 imfManager.SetRestoreAfterFocusLost( false );
518 // Notify that the text editing finish.
519 imfManager.Deactivate();
521 imfManager.EventReceivedSignal().Disconnect( this, &TextField::OnImfEvent );
524 mController->KeyboardFocusLostEvent();
527 void TextField::OnTap( const TapGesture& gesture )
529 // Show the keyboard if it was hidden.
530 if (!VirtualKeyboard::IsVisible())
532 VirtualKeyboard::Show();
537 mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
540 bool TextField::OnKeyEvent( const KeyEvent& event )
542 if( Dali::DALI_KEY_ESCAPE == event.keyCode )
544 ClearKeyInputFocus();
547 return mController->KeyEvent( event );
550 ImfManager::ImfCallbackData TextField::OnImfEvent( Dali::ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
552 switch ( imfEvent.eventName )
554 case ImfManager::PREEDIT:
559 case ImfManager::COMMIT:
564 case ImfManager::DELETESURROUNDING:
569 case ImfManager::GETSURROUNDING:
574 case ImfManager::VOID:
580 return ImfManager::ImfCallbackData();
583 void TextField::RequestTextRelayout()
588 void TextField::EnableClipping( bool clipping, const Vector2& size )
592 // Not worth to created clip actor if width or height is equal to zero.
593 if( size.width > Math::MACHINE_EPSILON_1000 && size.height > Math::MACHINE_EPSILON_1000 )
599 mClipper = Clipper::New( size );
600 self.Add( mClipper->GetRootActor() );
601 self.Add( mClipper->GetImageActor() );
605 mClipper->Refresh( size );
611 // Note - this will automatically remove the root & image actors
616 void TextField::KeyboardStatusChanged(bool keyboardShown)
618 // Just hide the grab handle when keyboard is hidden.
621 mController->KeyboardFocusLostEvent();
625 TextField::TextField()
626 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
627 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
628 mExceedPolicy( Dali::Toolkit::TextField::EXCEED_POLICY_CLIP )
632 TextField::~TextField()
637 } // namespace Internal
639 } // namespace Toolkit