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/integration-api/debug.h>
29 #include <dali/public-api/adaptor-framework/virtual-keyboard.h>
32 #include <dali-toolkit/public-api/text/rendering-backend.h>
33 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
34 #include <dali-toolkit/internal/text/rendering/text-backend.h>
35 #include <dali-toolkit/internal/styling/style-manager-impl.h>
37 using namespace Dali::Toolkit::Text;
50 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
59 return Toolkit::TextField::New();
62 // Setup properties, signals and actions using the type-registry.
63 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextField, Toolkit::Control, Create );
65 DALI_PROPERTY_REGISTRATION( TextField, "rendering-backend", INTEGER, RENDERING_BACKEND )
66 DALI_PROPERTY_REGISTRATION( TextField, "placeholder-text", STRING, PLACEHOLDER_TEXT )
67 DALI_PROPERTY_REGISTRATION( TextField, "text", STRING, TEXT )
68 DALI_PROPERTY_REGISTRATION( TextField, "font-family", STRING, FONT_FAMILY )
69 DALI_PROPERTY_REGISTRATION( TextField, "font-style", STRING, FONT_STYLE )
70 DALI_PROPERTY_REGISTRATION( TextField, "point-size", FLOAT, POINT_SIZE )
71 DALI_PROPERTY_REGISTRATION( TextField, "exceed-policy", INTEGER, EXCEED_POLICY )
72 DALI_PROPERTY_REGISTRATION( TextField, "cursor-image", STRING, CURSOR_IMAGE )
73 DALI_PROPERTY_REGISTRATION( TextField, "primary-cursor-color", VECTOR4, PRIMARY_CURSOR_COLOR )
74 DALI_PROPERTY_REGISTRATION( TextField, "secondary-cursor-color", VECTOR4, SECONDARY_CURSOR_COLOR )
75 DALI_PROPERTY_REGISTRATION( TextField, "enable-cursor-blink", BOOLEAN, ENABLE_CURSOR_BLINK )
76 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-interval", FLOAT, CURSOR_BLINK_INTERVAL )
77 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-duration", FLOAT, CURSOR_BLINK_DURATION )
78 DALI_PROPERTY_REGISTRATION( TextField, "grab-handle-image", STRING, GRAB_HANDLE_IMAGE )
79 DALI_PROPERTY_REGISTRATION( TextField, "decoration bounding-box", RECTANGLE, DECORATION_BOUNDING_BOX )
81 DALI_TYPE_REGISTRATION_END()
85 Toolkit::TextField TextField::New()
87 // Create the implementation, temporarily owned by this handle on stack
88 IntrusivePtr< TextField > impl = new TextField();
90 // Pass ownership to CustomActor handle
91 Toolkit::TextField handle( *impl );
93 // Second-phase init of the implementation
94 // This can only be done after the CustomActor connection has been made...
100 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
102 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
106 TextField& impl( GetImpl( textField ) );
110 case Toolkit::TextField::Property::RENDERING_BACKEND:
112 int backend = value.Get< int >();
114 if( impl.mRenderingBackend != backend )
116 impl.mRenderingBackend = backend;
117 impl.mRenderer.Reset();
121 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
123 if( impl.mController )
125 //impl.mController->SetPlaceholderText( value.Get< std::string >() ); TODO
129 case Toolkit::TextField::Property::TEXT:
131 if( impl.mController )
133 impl.mController->SetText( value.Get< std::string >() );
137 case Toolkit::TextField::Property::FONT_FAMILY:
139 if( impl.mController )
141 std::string fontFamily = value.Get< std::string >();
143 if( impl.mController->GetDefaultFontFamily() != fontFamily )
145 impl.mController->SetDefaultFontFamily( fontFamily );
146 impl.RequestTextRelayout();
151 case Toolkit::TextField::Property::FONT_STYLE:
153 if( impl.mController )
155 std::string fontStyle = value.Get< std::string >();
157 if( impl.mController->GetDefaultFontStyle() != fontStyle )
159 impl.mController->SetDefaultFontStyle( fontStyle );
160 impl.RequestTextRelayout();
165 case Toolkit::TextField::Property::POINT_SIZE:
167 if( impl.mController )
169 float pointSize = value.Get< float >();
171 if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
173 impl.mController->SetDefaultPointSize( pointSize );
174 impl.RequestTextRelayout();
179 case Toolkit::TextField::Property::EXCEED_POLICY:
181 impl.mExceedPolicy = value.Get< int >();
184 case Toolkit::TextField::Property::CURSOR_IMAGE:
186 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
188 if( impl.mDecorator )
190 impl.mDecorator->SetCursorImage( image );
194 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
196 if( impl.mDecorator )
198 impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
202 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
204 if( impl.mDecorator )
206 impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
210 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
212 if( impl.mController )
214 impl.mController->SetEnableCursorBlink( value.Get< bool >() );
218 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
220 if( impl.mDecorator )
222 impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
226 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
228 if( impl.mDecorator )
230 impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
234 case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
236 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
238 if( impl.mDecorator )
240 impl.mDecorator->SetGrabHandleImage( image );
244 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
246 if( impl.mDecorator )
248 impl.mDecorator->SetBoundingBox( value.Get< Rect<int> >() );
256 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
258 Property::Value value;
260 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
264 TextField& impl( GetImpl( textField ) );
268 case Toolkit::TextField::Property::RENDERING_BACKEND:
270 value = impl.mRenderingBackend;
273 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
275 if( impl.mController )
278 impl.mController->GetPlaceholderText( text );
283 case Toolkit::TextField::Property::TEXT:
285 if( impl.mController )
288 impl.mController->GetText( text );
293 case Toolkit::TextField::Property::EXCEED_POLICY:
295 value = impl.mExceedPolicy;
298 case Toolkit::TextField::Property::CURSOR_IMAGE:
300 if( impl.mDecorator )
302 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
305 value = image.GetUrl();
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::GRAB_HANDLE_IMAGE:
349 if( impl.mDecorator )
351 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
354 value = image.GetUrl();
359 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
361 if( impl.mDecorator )
363 value = impl.mDecorator->GetBoundingBox();
373 void TextField::OnInitialize()
375 mController = Text::Controller::New( *this );
377 mDecorator = Text::Decorator::New( *this, *mController );
379 mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
381 mController->EnableTextInput( mDecorator );
383 // Forward input events to controller
384 EnableGestureDetection(Gesture::Tap);
385 GetTapGestureDetector().SetMaximumTapsRequired( 2 );
387 // Set BoundingBox to stage size if not already set.
388 if ( mDecorator->GetBoundingBox().IsEmpty() )
390 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
391 mDecorator->SetBoundingBox( Rect<int>( 0.0f, 0.0f, stageSize.width, stageSize.height ) );
395 void TextField::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
397 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
400 Vector3 TextField::GetNaturalSize()
402 return mController->GetNaturalSize();
405 float TextField::GetHeightForWidth( float width )
407 return mController->GetHeightForWidth( width );
410 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
412 if( mController->Relayout( size ) ||
417 mDecorator->Relayout( size );
422 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
425 RenderableActor renderableActor;
428 renderableActor = mRenderer->Render( mController->GetView() );
431 EnableClipping( (Dali::Toolkit::TextField::EXCEED_POLICY_CLIP == mExceedPolicy), size );
433 if( renderableActor != mRenderableActor )
435 UnparentAndReset( mRenderableActor );
436 mRenderableActor = renderableActor;
439 if( mRenderableActor )
441 // Make sure the actor is parented correctly with/without clipping
444 mClipper->GetRootActor().Add( mRenderableActor );
448 Self().Add( mRenderableActor );
454 void TextField::OnKeyInputFocusGained()
456 VirtualKeyboard::StatusChangedSignal().Connect( this, &TextField::KeyboardStatusChanged );
458 ImfManager imfManager = ImfManager::Get();
462 imfManager.EventReceivedSignal().Connect( this, &TextField::OnImfEvent );
464 // Notify that the text editing start.
465 imfManager.Activate();
467 // When window gain lost focus, the imf manager is deactivated. Thus when window gain focus again, the imf manager must be activated.
468 imfManager.SetRestoreAfterFocusLost( true );
471 mController->KeyboardFocusGainEvent();
474 void TextField::OnKeyInputFocusLost()
476 VirtualKeyboard::StatusChangedSignal().Disconnect( this, &TextField::KeyboardStatusChanged );
478 ImfManager imfManager = ImfManager::Get();
481 // The text editing is finished. Therefore the imf manager don't have restore activation.
482 imfManager.SetRestoreAfterFocusLost( false );
484 // Notify that the text editing finish.
485 imfManager.Deactivate();
487 imfManager.EventReceivedSignal().Disconnect( this, &TextField::OnImfEvent );
490 mController->KeyboardFocusLostEvent();
493 void TextField::OnTap( const TapGesture& gesture )
495 // Show the keyboard if it was hidden.
496 if (!VirtualKeyboard::IsVisible())
498 VirtualKeyboard::Show();
503 mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
506 bool TextField::OnKeyEvent( const KeyEvent& event )
508 if( Dali::DALI_KEY_ESCAPE == event.keyCode )
510 ClearKeyInputFocus();
513 return mController->KeyEvent( event );
516 ImfManager::ImfCallbackData TextField::OnImfEvent( Dali::ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
518 switch ( imfEvent.eventName )
520 case ImfManager::PREEDIT:
525 case ImfManager::COMMIT:
530 case ImfManager::DELETESURROUNDING:
535 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