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 if( impl.mController->GetDefaultFontFamily() != fontFamily )
152 impl.mController->SetDefaultFontFamily( fontFamily, true );
157 case Toolkit::TextLabel::Property::FONT_STYLE:
159 SetFontStyleProperty( impl.mController, value );
162 case Toolkit::TextLabel::Property::POINT_SIZE:
164 if( impl.mController )
166 const float pointSize = value.Get< float >();
168 if( !Equals( impl.mController->GetDefaultPointSize(), pointSize ) )
170 impl.mController->SetDefaultPointSize( pointSize );
175 case Toolkit::TextLabel::Property::MULTI_LINE:
177 if( impl.mController )
179 impl.mController->SetMultiLineEnabled( value.Get< bool >() );
183 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
185 if( impl.mController )
187 LayoutEngine::HorizontalAlignment alignment( LayoutEngine::HORIZONTAL_ALIGN_BEGIN );
188 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
189 HORIZONTAL_ALIGNMENT_STRING_TABLE,
190 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
193 impl.mController->SetHorizontalAlignment( alignment );
198 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
200 if( impl.mController )
202 LayoutEngine::VerticalAlignment alignment( LayoutEngine::VERTICAL_ALIGN_BOTTOM );
203 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
204 VERTICAL_ALIGNMENT_STRING_TABLE,
205 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
208 impl.mController->SetVerticalAlignment( alignment );
214 case Toolkit::TextLabel::Property::TEXT_COLOR:
216 if( impl.mController )
218 const Vector4 textColor = value.Get< Vector4 >();
219 if( impl.mController->GetTextColor() != textColor )
221 impl.mController->SetTextColor( textColor );
222 impl.mRenderer.Reset();
228 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
230 if( impl.mController )
232 const Vector2 shadowOffset = value.Get< Vector2 >();
233 if ( impl.mController->GetShadowOffset() != shadowOffset )
235 impl.mController->SetShadowOffset( shadowOffset );
236 impl.mRenderer.Reset();
241 case Toolkit::TextLabel::Property::SHADOW_COLOR:
243 if( impl.mController )
245 const Vector4 shadowColor = value.Get< Vector4 >();
246 if ( impl.mController->GetShadowColor() != shadowColor )
248 impl.mController->SetShadowColor( shadowColor );
249 impl.mRenderer.Reset();
254 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
256 if( impl.mController )
258 const Vector4 color = value.Get< Vector4 >();
259 if ( impl.mController->GetUnderlineColor() != color )
261 impl.mController->SetUnderlineColor( color );
262 impl.mRenderer.Reset();
267 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
269 if( impl.mController )
271 const bool enabled = value.Get< bool >();
272 if ( impl.mController->IsUnderlineEnabled() != enabled )
274 impl.mController->SetUnderlineEnabled( enabled );
275 impl.mRenderer.Reset();
281 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
283 if( impl.mController )
285 float height = value.Get< float >();
286 if ( impl.mController->GetUnderlineHeight() != height )
288 impl.mController->SetUnderlineHeight( height );
289 impl.mRenderer.Reset();
298 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
300 Property::Value value;
302 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
306 TextLabel& impl( GetImpl( label ) );
309 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
311 value = impl.mRenderingBackend;
314 case Toolkit::TextLabel::Property::TEXT:
316 if( impl.mController )
319 impl.mController->GetText( text );
324 case Toolkit::TextLabel::Property::FONT_FAMILY:
326 if( impl.mController )
328 value = impl.mController->GetDefaultFontFamily();
332 case Toolkit::TextLabel::Property::FONT_STYLE:
334 GetFontStyleProperty( impl.mController, value );
337 case Toolkit::TextLabel::Property::POINT_SIZE:
339 if( impl.mController )
341 value = impl.mController->GetDefaultPointSize();
345 case Toolkit::TextLabel::Property::MULTI_LINE:
347 if( impl.mController )
349 value = impl.mController->IsMultiLineEnabled();
353 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
355 if( impl.mController )
357 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetHorizontalAlignment(),
358 HORIZONTAL_ALIGNMENT_STRING_TABLE,
359 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
362 value = std::string( name );
367 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
369 if( impl.mController )
371 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetVerticalAlignment(),
372 VERTICAL_ALIGNMENT_STRING_TABLE,
373 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
376 value = std::string( name );
381 case Toolkit::TextLabel::Property::TEXT_COLOR:
383 if ( impl.mController )
385 value = impl.mController->GetTextColor();
389 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
391 if ( impl.mController )
393 value = impl.mController->GetShadowOffset();
397 case Toolkit::TextLabel::Property::SHADOW_COLOR:
399 if ( impl.mController )
401 value = impl.mController->GetShadowColor();
405 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
407 if ( impl.mController )
409 value = impl.mController->GetUnderlineColor();
413 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
415 if ( impl.mController )
417 value = impl.mController->IsUnderlineEnabled();
421 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
423 if ( impl.mController )
425 value = impl.mController->GetUnderlineHeight();
435 void TextLabel::OnInitialize()
439 mController = Text::Controller::New( *this );
441 // Use height-for-width negotiation by default
442 self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
443 self.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
445 // Enable the text ellipsis.
446 LayoutEngine& engine = mController->GetLayoutEngine();
447 engine.SetTextEllipsisEnabled( true );
448 self.OnStageSignal().Connect( this, &TextLabel::OnStageConnect );
451 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
454 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextLabel::OnStyleChange\n");
458 case StyleChange::DEFAULT_FONT_CHANGE:
460 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_CHANGE\n");
461 // Property system did not set the font so should update it.
462 std::string newFont = styleManager.GetDefaultFontFamily();
463 mController->UpdateAfterFontChange( newFont );
467 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
469 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_SIZE_CHANGE (%f)\n", mController->GetDefaultPointSize() );
471 if ( (mController->GetDefaultPointSize() <= 0.0f) ) // If DefaultPointSize not set by Property system it will be 0.0f
473 // Property system did not set the PointSize so should update it.
474 // todo instruct text-controller to update model
478 case StyleChange::THEME_CHANGE:
480 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
486 Vector3 TextLabel::GetNaturalSize()
488 return mController->GetNaturalSize();
491 float TextLabel::GetHeightForWidth( float width )
493 return mController->GetHeightForWidth( width );
496 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
498 if( mController->Relayout( size ) ||
503 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
509 void TextLabel::RequestTextRelayout()
514 void TextLabel::RenderText()
517 Actor renderableActor;
520 renderableActor = mRenderer->Render( mController->GetView(), self.GetHierarchyDepth() );
523 if( renderableActor != mRenderableActor )
525 UnparentAndReset( mRenderableActor );
527 if( renderableActor )
529 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
530 renderableActor.SetPosition( alignmentOffset.x, alignmentOffset.y );
532 self.Add( renderableActor );
534 mRenderableActor = renderableActor;
538 void TextLabel::OnStageConnect( Dali::Actor actor )
540 if ( mHasBeenStaged )
546 mHasBeenStaged = true;
550 void TextLabel::AddDecoration( Actor& actor, bool needsClipping )
552 // TextLabel does not show decorations
555 void TextLabel::OnStageConnection( int depth )
557 // Call the Control::OnStageConnection() to set the depth of the background.
558 Control::OnStageConnection( depth );
560 // The depth of the text renderer is set in the RenderText() called from OnRelayout().
563 void TextLabel::TextChanged()
565 // TextLabel does not provide a signal for this
568 void TextLabel::MaxLengthReached()
570 // Pure Virtual from TextController Interface, only needed when inputting text
573 TextLabel::TextLabel()
574 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
575 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
576 mHasBeenStaged( false )
580 TextLabel::~TextLabel()
584 } // namespace Internal
586 } // namespace Toolkit