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 )
90 DALI_TYPE_REGISTRATION_END()
94 Toolkit::TextField TextField::New()
96 // Create the implementation, temporarily owned by this handle on stack
97 IntrusivePtr< TextField > impl = new TextField();
99 // Pass ownership to CustomActor handle
100 Toolkit::TextField handle( *impl );
102 // Second-phase init of the implementation
103 // This can only be done after the CustomActor connection has been made...
109 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
111 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
115 TextField& impl( GetImpl( textField ) );
119 case Toolkit::TextField::Property::RENDERING_BACKEND:
121 int backend = value.Get< int >();
123 if( impl.mRenderingBackend != backend )
125 impl.mRenderingBackend = backend;
126 impl.mRenderer.Reset();
130 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
132 if( impl.mController )
134 //impl.mController->SetPlaceholderText( value.Get< std::string >() ); TODO
138 case Toolkit::TextField::Property::TEXT:
140 if( impl.mController )
142 impl.mController->SetText( value.Get< std::string >() );
146 case Toolkit::TextField::Property::FONT_FAMILY:
148 if( impl.mController )
150 std::string fontFamily = value.Get< std::string >();
152 if( impl.mController->GetDefaultFontFamily() != fontFamily )
154 impl.mController->SetDefaultFontFamily( fontFamily );
155 impl.RequestTextRelayout();
160 case Toolkit::TextField::Property::FONT_STYLE:
162 if( impl.mController )
164 std::string fontStyle = value.Get< std::string >();
166 if( impl.mController->GetDefaultFontStyle() != fontStyle )
168 impl.mController->SetDefaultFontStyle( fontStyle );
169 impl.RequestTextRelayout();
174 case Toolkit::TextField::Property::POINT_SIZE:
176 if( impl.mController )
178 float pointSize = value.Get< float >();
180 if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
182 impl.mController->SetDefaultPointSize( pointSize );
183 impl.RequestTextRelayout();
188 case Toolkit::TextField::Property::EXCEED_POLICY:
190 impl.mExceedPolicy = value.Get< int >();
193 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
195 if( impl.mDecorator )
197 impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
201 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
203 if( impl.mDecorator )
205 impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
209 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
211 if( impl.mController )
213 impl.mController->SetEnableCursorBlink( value.Get< bool >() );
217 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
219 if( impl.mDecorator )
221 impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
225 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
227 if( impl.mDecorator )
229 impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
233 case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
235 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
237 if( impl.mDecorator )
239 impl.mDecorator->SetGrabHandleImage( image );
243 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
245 if( impl.mDecorator )
247 impl.mDecorator->SetBoundingBox( value.Get< Rect<int> >() );
251 case Toolkit::TextField::Property::ALIGNMENT:
253 LayoutEngine& engine = impl.mController->GetLayoutEngine();
254 const LayoutEngine::Alignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::Alignment >( value.Get< std::string >().c_str(),
255 ALIGNMENT_STRING_TABLE,
256 ALIGNMENT_STRING_TABLE_COUNT );
258 if( engine.GetAlignment() != alignment )
260 engine.SetAlignment( alignment );
261 impl.RequestTextRelayout();
269 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
271 Property::Value value;
273 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
277 TextField& impl( GetImpl( textField ) );
281 case Toolkit::TextField::Property::RENDERING_BACKEND:
283 value = impl.mRenderingBackend;
286 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
288 if( impl.mController )
291 impl.mController->GetPlaceholderText( text );
296 case Toolkit::TextField::Property::TEXT:
298 if( impl.mController )
301 impl.mController->GetText( text );
306 case Toolkit::TextField::Property::EXCEED_POLICY:
308 value = impl.mExceedPolicy;
311 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
313 if( impl.mDecorator )
315 value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
319 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
321 if( impl.mDecorator )
323 value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
327 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
329 value = impl.mController->GetEnableCursorBlink();
332 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
334 if( impl.mDecorator )
336 value = impl.mDecorator->GetCursorBlinkInterval();
340 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
342 if( impl.mDecorator )
344 value = impl.mDecorator->GetCursorBlinkDuration();
348 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
350 if( impl.mDecorator )
352 value = impl.mDecorator->GetBoundingBox();
356 case Toolkit::TextField::Property::ALIGNMENT:
358 if( impl.mController )
360 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::Alignment >( impl.mController->GetLayoutEngine().GetAlignment(),
361 ALIGNMENT_STRING_TABLE,
362 ALIGNMENT_STRING_TABLE_COUNT ) );
372 void TextField::OnInitialize()
374 mController = Text::Controller::New( *this );
376 mDecorator = Text::Decorator::New( *this, *mController );
378 mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
380 mController->EnableTextInput( mDecorator );
382 // Forward input events to controller
383 EnableGestureDetection(Gesture::Tap);
384 GetTapGestureDetector().SetMaximumTapsRequired( 2 );
385 EnableGestureDetection(Gesture::Pan);
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, mController->GetScrollPosition() );
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 const Vector2& scrollPosition = mController->GetScrollPosition();
442 mRenderableActor.SetPosition( scrollPosition.x, scrollPosition.y );
444 // Make sure the actor is parented correctly with/without clipping
447 mClipper->GetRootActor().Add( mRenderableActor );
451 Self().Add( mRenderableActor );
457 void TextField::OnKeyInputFocusGained()
459 VirtualKeyboard::StatusChangedSignal().Connect( this, &TextField::KeyboardStatusChanged );
461 ImfManager imfManager = ImfManager::Get();
465 imfManager.EventReceivedSignal().Connect( this, &TextField::OnImfEvent );
467 // Notify that the text editing start.
468 imfManager.Activate();
470 // When window gain lost focus, the imf manager is deactivated. Thus when window gain focus again, the imf manager must be activated.
471 imfManager.SetRestoreAfterFocusLost( true );
474 mController->KeyboardFocusGainEvent();
477 void TextField::OnKeyInputFocusLost()
479 VirtualKeyboard::StatusChangedSignal().Disconnect( this, &TextField::KeyboardStatusChanged );
481 ImfManager imfManager = ImfManager::Get();
484 // The text editing is finished. Therefore the imf manager don't have restore activation.
485 imfManager.SetRestoreAfterFocusLost( false );
487 // Notify that the text editing finish.
488 imfManager.Deactivate();
490 imfManager.EventReceivedSignal().Disconnect( this, &TextField::OnImfEvent );
493 mController->KeyboardFocusLostEvent();
496 void TextField::OnTap( const TapGesture& gesture )
498 // Show the keyboard if it was hidden.
499 if (!VirtualKeyboard::IsVisible())
501 VirtualKeyboard::Show();
506 mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
509 void TextField::OnPan( const PanGesture& gesture )
511 mController->PanEvent( gesture.state, gesture.displacement );
514 bool TextField::OnKeyEvent( const KeyEvent& event )
516 if( Dali::DALI_KEY_ESCAPE == event.keyCode )
518 ClearKeyInputFocus();
521 return mController->KeyEvent( event );
524 ImfManager::ImfCallbackData TextField::OnImfEvent( Dali::ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
526 switch ( imfEvent.eventName )
528 case ImfManager::COMMIT:
530 KeyEvent event( "", imfEvent.predictiveString, 0, 0, 0, KeyEvent::Down );
531 mController->KeyEvent( event );
534 case ImfManager::PREEDIT: // fall through
535 case ImfManager::DELETESURROUNDING:
536 case ImfManager::GETSURROUNDING:
537 case ImfManager::VOID:
543 return ImfManager::ImfCallbackData();
546 void TextField::RequestTextRelayout()
551 void TextField::EnableClipping( bool clipping, const Vector2& size )
555 // Not worth to created clip actor if width or height is equal to zero.
556 if( size.width > Math::MACHINE_EPSILON_1000 && size.height > Math::MACHINE_EPSILON_1000 )
562 mClipper = Clipper::New( size );
563 self.Add( mClipper->GetRootActor() );
564 self.Add( mClipper->GetImageActor() );
568 mClipper->Refresh( size );
574 // Note - this will automatically remove the root & image actors
579 void TextField::KeyboardStatusChanged(bool keyboardShown)
581 // Just hide the grab handle when keyboard is hidden.
584 mController->KeyboardFocusLostEvent();
588 TextField::TextField()
589 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
590 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
591 mExceedPolicy( Dali::Toolkit::TextField::EXCEED_POLICY_CLIP )
595 TextField::~TextField()
600 } // namespace Internal
602 } // namespace Toolkit