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( TextField, "rendering-backend", INTEGER, RENDERING_BACKEND )
83 DALI_PROPERTY_REGISTRATION( TextField, "placeholder-text", STRING, PLACEHOLDER_TEXT )
84 DALI_PROPERTY_REGISTRATION( TextField, "text", STRING, TEXT )
85 DALI_PROPERTY_REGISTRATION( TextField, "font-family", STRING, FONT_FAMILY )
86 DALI_PROPERTY_REGISTRATION( TextField, "font-style", STRING, FONT_STYLE )
87 DALI_PROPERTY_REGISTRATION( TextField, "point-size", FLOAT, POINT_SIZE )
88 DALI_PROPERTY_REGISTRATION( TextField, "exceed-policy", INTEGER, EXCEED_POLICY )
89 DALI_PROPERTY_REGISTRATION( TextField, "primary-cursor-color", VECTOR4, PRIMARY_CURSOR_COLOR )
90 DALI_PROPERTY_REGISTRATION( TextField, "secondary-cursor-color", VECTOR4, SECONDARY_CURSOR_COLOR )
91 DALI_PROPERTY_REGISTRATION( TextField, "enable-cursor-blink", BOOLEAN, ENABLE_CURSOR_BLINK )
92 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-interval", FLOAT, CURSOR_BLINK_INTERVAL )
93 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-duration", FLOAT, CURSOR_BLINK_DURATION )
94 DALI_PROPERTY_REGISTRATION( TextField, "grab-handle-image", STRING, GRAB_HANDLE_IMAGE )
95 DALI_PROPERTY_REGISTRATION( TextField, "decoration-bounding-box", RECTANGLE, DECORATION_BOUNDING_BOX )
96 DALI_PROPERTY_REGISTRATION( TextField, "horizontal-alignment", STRING, HORIZONTAL_ALIGNMENT )
97 DALI_PROPERTY_REGISTRATION( TextField, "vertical-alignment", STRING, VERTICAL_ALIGNMENT )
98 DALI_TYPE_REGISTRATION_END()
102 Toolkit::TextField TextField::New()
104 // Create the implementation, temporarily owned by this handle on stack
105 IntrusivePtr< TextField > impl = new TextField();
107 // Pass ownership to CustomActor handle
108 Toolkit::TextField handle( *impl );
110 // Second-phase init of the implementation
111 // This can only be done after the CustomActor connection has been made...
117 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
119 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
123 TextField& impl( GetImpl( textField ) );
127 case Toolkit::TextField::Property::RENDERING_BACKEND:
129 int backend = value.Get< int >();
131 if( impl.mRenderingBackend != backend )
133 impl.mRenderingBackend = backend;
134 impl.mRenderer.Reset();
138 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
140 if( impl.mController )
142 //impl.mController->SetPlaceholderText( value.Get< std::string >() ); TODO
146 case Toolkit::TextField::Property::TEXT:
148 if( impl.mController )
150 impl.mController->SetText( value.Get< std::string >() );
154 case Toolkit::TextField::Property::FONT_FAMILY:
156 if( impl.mController )
158 std::string fontFamily = value.Get< std::string >();
160 if( impl.mController->GetDefaultFontFamily() != fontFamily )
162 impl.mController->SetDefaultFontFamily( fontFamily );
163 impl.RequestTextRelayout();
168 case Toolkit::TextField::Property::FONT_STYLE:
170 if( impl.mController )
172 std::string fontStyle = value.Get< std::string >();
174 if( impl.mController->GetDefaultFontStyle() != fontStyle )
176 impl.mController->SetDefaultFontStyle( fontStyle );
177 impl.RequestTextRelayout();
182 case Toolkit::TextField::Property::POINT_SIZE:
184 if( impl.mController )
186 float pointSize = value.Get< float >();
188 if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
190 impl.mController->SetDefaultPointSize( pointSize );
191 impl.RequestTextRelayout();
196 case Toolkit::TextField::Property::EXCEED_POLICY:
198 impl.mExceedPolicy = value.Get< int >();
201 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
203 if( impl.mDecorator )
205 impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
209 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
211 if( impl.mDecorator )
213 impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
217 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
219 if( impl.mController )
221 impl.mController->SetEnableCursorBlink( value.Get< bool >() );
225 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
227 if( impl.mDecorator )
229 impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
233 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
235 if( impl.mDecorator )
237 impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
241 case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
243 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
245 if( impl.mDecorator )
247 impl.mDecorator->SetGrabHandleImage( image );
251 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
253 if( impl.mDecorator )
255 impl.mDecorator->SetBoundingBox( value.Get< Rect<int> >() );
259 case Toolkit::TextField::Property::HORIZONTAL_ALIGNMENT:
261 LayoutEngine& engine = impl.mController->GetLayoutEngine();
262 const LayoutEngine::HorizontalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
263 HORIZONTAL_ALIGNMENT_STRING_TABLE,
264 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
266 if( engine.GetHorizontalAlignment() != alignment )
268 engine.SetHorizontalAlignment( alignment );
269 impl.RequestTextRelayout();
273 case Toolkit::TextField::Property::VERTICAL_ALIGNMENT:
275 LayoutEngine& engine = impl.mController->GetLayoutEngine();
276 const LayoutEngine::VerticalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
277 VERTICAL_ALIGNMENT_STRING_TABLE,
278 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
280 if( engine.GetVerticalAlignment() != alignment )
282 engine.SetVerticalAlignment( alignment );
283 impl.RequestTextRelayout();
291 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
293 Property::Value value;
295 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
299 TextField& impl( GetImpl( textField ) );
303 case Toolkit::TextField::Property::RENDERING_BACKEND:
305 value = impl.mRenderingBackend;
308 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
310 if( impl.mController )
313 impl.mController->GetPlaceholderText( text );
318 case Toolkit::TextField::Property::TEXT:
320 if( impl.mController )
323 impl.mController->GetText( text );
328 case Toolkit::TextField::Property::EXCEED_POLICY:
330 value = impl.mExceedPolicy;
333 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
335 if( impl.mDecorator )
337 value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
341 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
343 if( impl.mDecorator )
345 value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
349 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
351 value = impl.mController->GetEnableCursorBlink();
354 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
356 if( impl.mDecorator )
358 value = impl.mDecorator->GetCursorBlinkInterval();
362 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
364 if( impl.mDecorator )
366 value = impl.mDecorator->GetCursorBlinkDuration();
370 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
372 if( impl.mDecorator )
374 value = impl.mDecorator->GetBoundingBox();
378 case Toolkit::TextField::Property::HORIZONTAL_ALIGNMENT:
380 if( impl.mController )
382 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetLayoutEngine().GetHorizontalAlignment(),
383 HORIZONTAL_ALIGNMENT_STRING_TABLE,
384 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT ) );
388 case Toolkit::TextField::Property::VERTICAL_ALIGNMENT:
390 if( impl.mController )
392 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetLayoutEngine().GetVerticalAlignment(),
393 VERTICAL_ALIGNMENT_STRING_TABLE,
394 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT ) );
404 void TextField::OnInitialize()
408 mController = Text::Controller::New( *this );
410 mDecorator = Text::Decorator::New( *this, *mController );
412 mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
414 mController->EnableTextInput( mDecorator );
416 // Forward input events to controller
417 EnableGestureDetection(Gesture::Tap);
418 GetTapGestureDetector().SetMaximumTapsRequired( 2 );
419 EnableGestureDetection(Gesture::Pan);
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 ) );
428 // Fill-parent area by default
429 self.SetResizePolicy( FILL_TO_PARENT, WIDTH );
430 self.SetResizePolicy( FILL_TO_PARENT, HEIGHT );
433 void TextField::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
435 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
438 Vector3 TextField::GetNaturalSize()
440 return mController->GetNaturalSize();
443 float TextField::GetHeightForWidth( float width )
445 return mController->GetHeightForWidth( width );
448 void TextField::OnRelayout( const Vector2& size, RelayoutContainer& container )
450 if( mController->Relayout( size ) ||
453 const Vector2& scrollPosition = mController->GetScrollPosition();
454 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
456 Vector2 offset = scrollPosition + alignmentOffset;
460 mDecorator->Relayout( size, offset );
465 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
468 RenderableActor renderableActor;
471 renderableActor = mRenderer->Render( mController->GetView() );
474 EnableClipping( (Dali::Toolkit::TextField::EXCEED_POLICY_CLIP == mExceedPolicy), size );
476 if( renderableActor != mRenderableActor )
478 UnparentAndReset( mRenderableActor );
479 mRenderableActor = renderableActor;
482 if( mRenderableActor )
484 mRenderableActor.SetPosition( offset.x, offset.y );
486 // Make sure the actor is parented correctly with/without clipping
489 mClipper->GetRootActor().Add( mRenderableActor );
493 Self().Add( mRenderableActor );
499 void TextField::OnKeyInputFocusGained()
501 VirtualKeyboard::StatusChangedSignal().Connect( this, &TextField::KeyboardStatusChanged );
503 ImfManager imfManager = ImfManager::Get();
507 imfManager.EventReceivedSignal().Connect( this, &TextField::OnImfEvent );
509 // Notify that the text editing start.
510 imfManager.Activate();
512 // When window gain lost focus, the imf manager is deactivated. Thus when window gain focus again, the imf manager must be activated.
513 imfManager.SetRestoreAfterFocusLost( true );
516 mController->KeyboardFocusGainEvent();
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();
538 void TextField::OnTap( const TapGesture& gesture )
540 // Show the keyboard if it was hidden.
541 if (!VirtualKeyboard::IsVisible())
543 VirtualKeyboard::Show();
548 mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
551 void TextField::OnPan( const PanGesture& gesture )
553 mController->PanEvent( gesture.state, gesture.displacement );
556 bool TextField::OnKeyEvent( const KeyEvent& event )
558 if( Dali::DALI_KEY_ESCAPE == event.keyCode )
560 ClearKeyInputFocus();
563 return mController->KeyEvent( event );
566 ImfManager::ImfCallbackData TextField::OnImfEvent( Dali::ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
568 switch ( imfEvent.eventName )
570 case ImfManager::COMMIT:
572 KeyEvent event( "", imfEvent.predictiveString, 0, 0, 0, KeyEvent::Down );
573 mController->KeyEvent( event );
576 case ImfManager::PREEDIT: // fall through
577 case ImfManager::DELETESURROUNDING:
578 case ImfManager::GETSURROUNDING:
579 case ImfManager::VOID:
585 return ImfManager::ImfCallbackData();
588 void TextField::RequestTextRelayout()
593 void TextField::EnableClipping( bool clipping, const Vector2& size )
597 // Not worth to created clip actor if width or height is equal to zero.
598 if( size.width > Math::MACHINE_EPSILON_1000 && size.height > Math::MACHINE_EPSILON_1000 )
604 mClipper = Clipper::New( size );
605 self.Add( mClipper->GetRootActor() );
606 self.Add( mClipper->GetImageActor() );
610 mClipper->Refresh( size );
616 // Note - this will automatically remove the root & image actors
621 void TextField::KeyboardStatusChanged(bool keyboardShown)
623 // Just hide the grab handle when keyboard is hidden.
626 mController->KeyboardFocusLostEvent();
630 TextField::TextField()
631 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
632 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
633 mExceedPolicy( Dali::Toolkit::TextField::EXCEED_POLICY_CLIP )
637 TextField::~TextField()
642 } // namespace Internal
644 } // namespace Toolkit