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-label-impl.h>
22 #include <dali/devel-api/object/type-registry-helper.h>
23 #include <dali/integration-api/debug.h>
26 #include <dali-toolkit/public-api/text/rendering-backend.h>
27 #include <dali-toolkit/internal/controls/text-controls/text-font-style.h>
28 #include <dali-toolkit/internal/text/rendering/text-backend.h>
29 #include <dali-toolkit/internal/text/text-view.h>
30 #include <dali-toolkit/internal/styling/style-manager-impl.h>
32 using Dali::Toolkit::Text::LayoutEngine;
33 using Dali::Toolkit::Text::Backend;
46 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
52 #if defined(DEBUG_ENABLED)
53 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_CONTROLS");
56 const Scripting::StringEnum HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
58 { "BEGIN", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_BEGIN },
59 { "CENTER", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_CENTER },
60 { "END", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_END },
62 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
64 const Scripting::StringEnum VERTICAL_ALIGNMENT_STRING_TABLE[] =
66 { "TOP", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_TOP },
67 { "CENTER", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_CENTER },
68 { "BOTTOM", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_BOTTOM },
70 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
75 return Toolkit::TextLabel::New();
78 // Setup properties, signals and actions using the type-registry.
79 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextLabel, Toolkit::Control, Create );
81 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "rendering-backend", INTEGER, RENDERING_BACKEND )
82 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "text", STRING, TEXT )
83 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "font-family", STRING, FONT_FAMILY )
84 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "font-style", STRING, FONT_STYLE )
85 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "point-size", FLOAT, POINT_SIZE )
86 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "multi-line", BOOLEAN, MULTI_LINE )
87 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "horizontal-alignment", STRING, HORIZONTAL_ALIGNMENT )
88 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "vertical-alignment", STRING, VERTICAL_ALIGNMENT )
89 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "text-color", VECTOR4, TEXT_COLOR )
90 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "shadow-offset", VECTOR2, SHADOW_OFFSET )
91 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "shadow-color", VECTOR4, SHADOW_COLOR )
92 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underline-enabled", BOOLEAN, UNDERLINE_ENABLED )
93 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underline-color", VECTOR4, UNDERLINE_COLOR )
94 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underline-height", FLOAT, UNDERLINE_HEIGHT )
96 DALI_TYPE_REGISTRATION_END()
100 Toolkit::TextLabel TextLabel::New()
102 // Create the implementation, temporarily owned by this handle on stack
103 IntrusivePtr< TextLabel > impl = new TextLabel();
105 // Pass ownership to CustomActor handle
106 Toolkit::TextLabel handle( *impl );
108 // Second-phase init of the implementation
109 // This can only be done after the CustomActor connection has been made...
115 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
117 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
121 TextLabel& impl( GetImpl( label ) );
124 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
126 const int backend = value.Get< int >();
128 if( impl.mRenderingBackend != backend )
130 impl.mRenderingBackend = backend;
131 impl.mRenderer.Reset();
132 impl.RequestTextRelayout();
136 case Toolkit::TextLabel::Property::TEXT:
138 if( impl.mController )
140 impl.mController->SetText( value.Get< std::string >() );
144 case Toolkit::TextLabel::Property::FONT_FAMILY:
146 if( impl.mController )
148 const std::string fontFamily = value.Get< std::string >();
150 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::SetProperty Property::FONT_FAMILY newFont(%s)\n", fontFamily.c_str() );
151 if( impl.mController->GetDefaultFontFamily() != fontFamily )
153 impl.mController->SetDefaultFontFamily( fontFamily, true );
158 case Toolkit::TextLabel::Property::FONT_STYLE:
160 SetFontStyleProperty( impl.mController, value );
163 case Toolkit::TextLabel::Property::POINT_SIZE:
165 if( impl.mController )
167 const float pointSize = value.Get< float >();
169 if( !Equals( impl.mController->GetDefaultPointSize(), pointSize ) )
171 impl.mController->SetDefaultPointSize( pointSize );
176 case Toolkit::TextLabel::Property::MULTI_LINE:
178 if( impl.mController )
180 impl.mController->SetMultiLineEnabled( value.Get< bool >() );
184 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
186 if( impl.mController )
188 LayoutEngine::HorizontalAlignment alignment( LayoutEngine::HORIZONTAL_ALIGN_BEGIN );
189 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
190 HORIZONTAL_ALIGNMENT_STRING_TABLE,
191 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
194 impl.mController->SetHorizontalAlignment( alignment );
199 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
201 if( impl.mController )
203 LayoutEngine::VerticalAlignment alignment( LayoutEngine::VERTICAL_ALIGN_BOTTOM );
204 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
205 VERTICAL_ALIGNMENT_STRING_TABLE,
206 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
209 impl.mController->SetVerticalAlignment( alignment );
215 case Toolkit::TextLabel::Property::TEXT_COLOR:
217 if( impl.mController )
219 const Vector4 textColor = value.Get< Vector4 >();
220 if( impl.mController->GetTextColor() != textColor )
222 impl.mController->SetTextColor( textColor );
223 impl.mRenderer.Reset();
229 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
231 if( impl.mController )
233 const Vector2 shadowOffset = value.Get< Vector2 >();
234 if ( impl.mController->GetShadowOffset() != shadowOffset )
236 impl.mController->SetShadowOffset( shadowOffset );
237 impl.mRenderer.Reset();
242 case Toolkit::TextLabel::Property::SHADOW_COLOR:
244 if( impl.mController )
246 const Vector4 shadowColor = value.Get< Vector4 >();
247 if ( impl.mController->GetShadowColor() != shadowColor )
249 impl.mController->SetShadowColor( shadowColor );
250 impl.mRenderer.Reset();
255 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
257 if( impl.mController )
259 const Vector4 color = value.Get< Vector4 >();
260 if ( impl.mController->GetUnderlineColor() != color )
262 impl.mController->SetUnderlineColor( color );
263 impl.mRenderer.Reset();
268 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
270 if( impl.mController )
272 const bool enabled = value.Get< bool >();
273 if ( impl.mController->IsUnderlineEnabled() != enabled )
275 impl.mController->SetUnderlineEnabled( enabled );
276 impl.mRenderer.Reset();
282 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
284 if( impl.mController )
286 float height = value.Get< float >();
287 if ( impl.mController->GetUnderlineHeight() != height )
289 impl.mController->SetUnderlineHeight( height );
290 impl.mRenderer.Reset();
299 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
301 Property::Value value;
303 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
307 TextLabel& impl( GetImpl( label ) );
310 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
312 value = impl.mRenderingBackend;
315 case Toolkit::TextLabel::Property::TEXT:
317 if( impl.mController )
320 impl.mController->GetText( text );
325 case Toolkit::TextLabel::Property::FONT_FAMILY:
327 if( impl.mController )
329 value = impl.mController->GetDefaultFontFamily();
333 case Toolkit::TextLabel::Property::FONT_STYLE:
335 GetFontStyleProperty( impl.mController, value );
338 case Toolkit::TextLabel::Property::POINT_SIZE:
340 if( impl.mController )
342 value = impl.mController->GetDefaultPointSize();
346 case Toolkit::TextLabel::Property::MULTI_LINE:
348 if( impl.mController )
350 value = impl.mController->IsMultiLineEnabled();
354 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
356 if( impl.mController )
358 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetHorizontalAlignment(),
359 HORIZONTAL_ALIGNMENT_STRING_TABLE,
360 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
363 value = std::string( name );
368 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
370 if( impl.mController )
372 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetVerticalAlignment(),
373 VERTICAL_ALIGNMENT_STRING_TABLE,
374 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
377 value = std::string( name );
382 case Toolkit::TextLabel::Property::TEXT_COLOR:
384 if ( impl.mController )
386 value = impl.mController->GetTextColor();
390 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
392 if ( impl.mController )
394 value = impl.mController->GetShadowOffset();
398 case Toolkit::TextLabel::Property::SHADOW_COLOR:
400 if ( impl.mController )
402 value = impl.mController->GetShadowColor();
406 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
408 if ( impl.mController )
410 value = impl.mController->GetUnderlineColor();
414 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
416 if ( impl.mController )
418 value = impl.mController->IsUnderlineEnabled();
422 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
424 if ( impl.mController )
426 value = impl.mController->GetUnderlineHeight();
436 void TextLabel::OnInitialize()
440 mController = Text::Controller::New( *this );
442 // Use height-for-width negotiation by default
443 self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
444 self.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
446 // Enable the text ellipsis.
447 LayoutEngine& engine = mController->GetLayoutEngine();
448 engine.SetTextEllipsisEnabled( true );
449 self.OnStageSignal().Connect( this, &TextLabel::OnStageConnect );
452 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
455 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextLabel::OnStyleChange\n");
459 case StyleChange::DEFAULT_FONT_CHANGE:
461 // Property system did not set the font so should update it.
462 std::string newFont = styleManager.GetDefaultFontFamily();
463 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_CHANGE newFont(%s)\n", newFont.c_str() );
464 mController->UpdateAfterFontChange( newFont );
468 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
470 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_SIZE_CHANGE (%f)\n", mController->GetDefaultPointSize() );
472 if ( (mController->GetDefaultPointSize() <= 0.0f) ) // If DefaultPointSize not set by Property system it will be 0.0f
474 // Property system did not set the PointSize so should update it.
475 // todo instruct text-controller to update model
479 case StyleChange::THEME_CHANGE:
481 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
487 Vector3 TextLabel::GetNaturalSize()
489 return mController->GetNaturalSize();
492 float TextLabel::GetHeightForWidth( float width )
494 return mController->GetHeightForWidth( width );
497 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
499 if( mController->Relayout( size ) ||
504 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
510 void TextLabel::RequestTextRelayout()
515 void TextLabel::RenderText()
518 Actor renderableActor;
521 renderableActor = mRenderer->Render( mController->GetView(), self.GetHierarchyDepth() );
524 if( renderableActor != mRenderableActor )
526 UnparentAndReset( mRenderableActor );
528 if( renderableActor )
530 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
531 renderableActor.SetPosition( alignmentOffset.x, alignmentOffset.y );
533 self.Add( renderableActor );
535 mRenderableActor = renderableActor;
539 void TextLabel::OnStageConnect( Dali::Actor actor )
541 if ( mHasBeenStaged )
547 mHasBeenStaged = true;
551 void TextLabel::AddDecoration( Actor& actor, bool needsClipping )
553 // TextLabel does not show decorations
556 void TextLabel::OnStageConnection( int depth )
558 // Call the Control::OnStageConnection() to set the depth of the background.
559 Control::OnStageConnection( depth );
561 // The depth of the text renderer is set in the RenderText() called from OnRelayout().
564 void TextLabel::TextChanged()
566 // TextLabel does not provide a signal for this
569 void TextLabel::MaxLengthReached()
571 // Pure Virtual from TextController Interface, only needed when inputting text
574 TextLabel::TextLabel()
575 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
576 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
577 mHasBeenStaged( false )
581 TextLabel::~TextLabel()
585 } // namespace Internal
587 } // namespace Toolkit