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 impl.mController->SetDefaultFontFamily( fontFamily );
155 case Toolkit::TextLabel::Property::FONT_STYLE:
157 SetFontStyleProperty( impl.mController, value );
160 case Toolkit::TextLabel::Property::POINT_SIZE:
162 if( impl.mController )
164 const float pointSize = value.Get< float >();
166 if( !Equals( impl.mController->GetDefaultPointSize(), pointSize ) )
168 impl.mController->SetDefaultPointSize( pointSize );
173 case Toolkit::TextLabel::Property::MULTI_LINE:
175 if( impl.mController )
177 impl.mController->SetMultiLineEnabled( value.Get< bool >() );
181 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
183 if( impl.mController )
185 LayoutEngine::HorizontalAlignment alignment( LayoutEngine::HORIZONTAL_ALIGN_BEGIN );
186 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
187 HORIZONTAL_ALIGNMENT_STRING_TABLE,
188 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
191 impl.mController->SetHorizontalAlignment( alignment );
196 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
198 if( impl.mController )
200 LayoutEngine::VerticalAlignment alignment( LayoutEngine::VERTICAL_ALIGN_BOTTOM );
201 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
202 VERTICAL_ALIGNMENT_STRING_TABLE,
203 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
206 impl.mController->SetVerticalAlignment( alignment );
212 case Toolkit::TextLabel::Property::TEXT_COLOR:
214 if( impl.mController )
216 const Vector4 textColor = value.Get< Vector4 >();
217 if( impl.mController->GetTextColor() != textColor )
219 impl.mController->SetTextColor( textColor );
220 impl.mRenderer.Reset();
226 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
228 if( impl.mController )
230 const Vector2 shadowOffset = value.Get< Vector2 >();
231 if ( impl.mController->GetShadowOffset() != shadowOffset )
233 impl.mController->SetShadowOffset( shadowOffset );
234 impl.mRenderer.Reset();
239 case Toolkit::TextLabel::Property::SHADOW_COLOR:
241 if( impl.mController )
243 const Vector4 shadowColor = value.Get< Vector4 >();
244 if ( impl.mController->GetShadowColor() != shadowColor )
246 impl.mController->SetShadowColor( shadowColor );
247 impl.mRenderer.Reset();
252 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
254 if( impl.mController )
256 const Vector4 color = value.Get< Vector4 >();
257 if ( impl.mController->GetUnderlineColor() != color )
259 impl.mController->SetUnderlineColor( color );
260 impl.mRenderer.Reset();
265 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
267 if( impl.mController )
269 const bool enabled = value.Get< bool >();
270 if ( impl.mController->IsUnderlineEnabled() != enabled )
272 impl.mController->SetUnderlineEnabled( enabled );
273 impl.mRenderer.Reset();
279 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
281 if( impl.mController )
283 float height = value.Get< float >();
284 if ( impl.mController->GetUnderlineHeight() != height )
286 impl.mController->SetUnderlineHeight( height );
287 impl.mRenderer.Reset();
296 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
298 Property::Value value;
300 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
304 TextLabel& impl( GetImpl( label ) );
307 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
309 value = impl.mRenderingBackend;
312 case Toolkit::TextLabel::Property::TEXT:
314 if( impl.mController )
317 impl.mController->GetText( text );
322 case Toolkit::TextLabel::Property::FONT_FAMILY:
324 if( impl.mController )
326 value = impl.mController->GetDefaultFontFamily();
330 case Toolkit::TextLabel::Property::FONT_STYLE:
332 GetFontStyleProperty( impl.mController, value );
335 case Toolkit::TextLabel::Property::POINT_SIZE:
337 if( impl.mController )
339 value = impl.mController->GetDefaultPointSize();
343 case Toolkit::TextLabel::Property::MULTI_LINE:
345 if( impl.mController )
347 value = impl.mController->IsMultiLineEnabled();
351 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
353 if( impl.mController )
355 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetHorizontalAlignment(),
356 HORIZONTAL_ALIGNMENT_STRING_TABLE,
357 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
360 value = std::string( name );
365 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
367 if( impl.mController )
369 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetVerticalAlignment(),
370 VERTICAL_ALIGNMENT_STRING_TABLE,
371 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
374 value = std::string( name );
379 case Toolkit::TextLabel::Property::TEXT_COLOR:
381 if ( impl.mController )
383 value = impl.mController->GetTextColor();
387 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
389 if ( impl.mController )
391 value = impl.mController->GetShadowOffset();
395 case Toolkit::TextLabel::Property::SHADOW_COLOR:
397 if ( impl.mController )
399 value = impl.mController->GetShadowColor();
403 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
405 if ( impl.mController )
407 value = impl.mController->GetUnderlineColor();
411 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
413 if ( impl.mController )
415 value = impl.mController->IsUnderlineEnabled();
419 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
421 if ( impl.mController )
423 value = impl.mController->GetUnderlineHeight();
433 void TextLabel::OnInitialize()
437 mController = Text::Controller::New( *this );
439 // Use height-for-width negotiation by default
440 self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
441 self.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
443 // Enable the text ellipsis.
444 LayoutEngine& engine = mController->GetLayoutEngine();
445 engine.SetTextEllipsisEnabled( true );
446 self.OnStageSignal().Connect( this, &TextLabel::OnStageConnect );
449 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
452 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextLabel::OnStyleChange\n");
456 case StyleChange::DEFAULT_FONT_CHANGE:
458 // Property system did not set the font so should update it.
459 std::string newFont = styleManager.GetDefaultFontFamily();
460 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_CHANGE newFont(%s)\n", newFont.c_str() );
461 mController->UpdateAfterFontChange( newFont );
465 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
467 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_SIZE_CHANGE (%f)\n", mController->GetDefaultPointSize() );
469 if ( (mController->GetDefaultPointSize() <= 0.0f) ) // If DefaultPointSize not set by Property system it will be 0.0f
471 // Property system did not set the PointSize so should update it.
472 // todo instruct text-controller to update model
476 case StyleChange::THEME_CHANGE:
478 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
484 Vector3 TextLabel::GetNaturalSize()
486 return mController->GetNaturalSize();
489 float TextLabel::GetHeightForWidth( float width )
491 return mController->GetHeightForWidth( width );
494 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
496 if( mController->Relayout( size ) ||
501 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
507 void TextLabel::RequestTextRelayout()
512 void TextLabel::RenderText()
515 Actor renderableActor;
518 renderableActor = mRenderer->Render( mController->GetView(), self.GetHierarchyDepth() );
521 if( renderableActor != mRenderableActor )
523 UnparentAndReset( mRenderableActor );
525 if( renderableActor )
527 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
528 renderableActor.SetPosition( alignmentOffset.x, alignmentOffset.y );
530 self.Add( renderableActor );
532 mRenderableActor = renderableActor;
536 void TextLabel::OnStageConnect( Dali::Actor actor )
538 if ( mHasBeenStaged )
544 mHasBeenStaged = true;
548 void TextLabel::AddDecoration( Actor& actor, bool needsClipping )
550 // TextLabel does not show decorations
553 void TextLabel::OnStageConnection( int depth )
555 // Call the Control::OnStageConnection() to set the depth of the background.
556 Control::OnStageConnection( depth );
558 // The depth of the text renderer is set in the RenderText() called from OnRelayout().
561 void TextLabel::TextChanged()
563 // TextLabel does not provide a signal for this
566 void TextLabel::MaxLengthReached()
568 // Pure Virtual from TextController Interface, only needed when inputting text
571 TextLabel::TextLabel()
572 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
573 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
574 mHasBeenStaged( false )
578 TextLabel::~TextLabel()
582 } // namespace Internal
584 } // namespace Toolkit