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/public-api/object/type-registry.h>
23 #include <dali/devel-api/object/type-registry-helper.h>
24 #include <dali/devel-api/scripting/scripting.h>
25 #include <dali/integration-api/debug.h>
28 #include <dali-toolkit/public-api/text/rendering-backend.h>
29 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
30 #include <dali-toolkit/internal/text/rendering/text-backend.h>
31 #include <dali-toolkit/internal/text/text-view.h>
32 #include <dali-toolkit/internal/styling/style-manager-impl.h>
34 using Dali::Toolkit::Text::LayoutEngine;
35 using Dali::Toolkit::Text::Backend;
48 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
54 const Scripting::StringEnum HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
56 { "BEGIN", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_BEGIN },
57 { "CENTER", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_CENTER },
58 { "END", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_END },
60 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
62 const Scripting::StringEnum VERTICAL_ALIGNMENT_STRING_TABLE[] =
64 { "TOP", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_TOP },
65 { "CENTER", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_CENTER },
66 { "BOTTOM", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_BOTTOM },
68 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
73 return Toolkit::TextLabel::New();
76 // Setup properties, signals and actions using the type-registry.
77 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextLabel, Toolkit::Control, Create );
79 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "rendering-backend", INTEGER, RENDERING_BACKEND )
80 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "text", STRING, TEXT )
81 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "font-family", STRING, FONT_FAMILY )
82 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "font-style", STRING, FONT_STYLE )
83 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "point-size", FLOAT, POINT_SIZE )
84 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "multi-line", BOOLEAN, MULTI_LINE )
85 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "horizontal-alignment", STRING, HORIZONTAL_ALIGNMENT )
86 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "vertical-alignment", STRING, VERTICAL_ALIGNMENT )
87 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "text-color", VECTOR4, TEXT_COLOR )
88 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "shadow-offset", VECTOR2, SHADOW_OFFSET )
89 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "shadow-color", VECTOR4, SHADOW_COLOR )
90 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underline-enabled", BOOLEAN, UNDERLINE_ENABLED )
91 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underline-color", VECTOR4, UNDERLINE_COLOR )
92 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underline-height", FLOAT, UNDERLINE_HEIGHT )
94 DALI_TYPE_REGISTRATION_END()
98 Toolkit::TextLabel TextLabel::New()
100 // Create the implementation, temporarily owned by this handle on stack
101 IntrusivePtr< TextLabel > impl = new TextLabel();
103 // Pass ownership to CustomActor handle
104 Toolkit::TextLabel handle( *impl );
106 // Second-phase init of the implementation
107 // This can only be done after the CustomActor connection has been made...
113 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
115 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
119 TextLabel& impl( GetImpl( label ) );
122 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
124 const int backend = value.Get< int >();
126 if( impl.mRenderingBackend != backend )
128 impl.mRenderingBackend = backend;
129 impl.mRenderer.Reset();
130 impl.RequestTextRelayout();
134 case Toolkit::TextLabel::Property::TEXT:
136 if( impl.mController )
138 impl.mController->SetText( value.Get< std::string >() );
142 case Toolkit::TextLabel::Property::FONT_FAMILY:
144 if( impl.mController )
146 const std::string fontFamily = value.Get< std::string >();
148 if( impl.mController->GetDefaultFontFamily() != fontFamily )
150 impl.mController->SetDefaultFontFamily( fontFamily );
155 case Toolkit::TextLabel::Property::FONT_STYLE:
157 if( impl.mController )
159 const std::string fontStyle = value.Get< std::string >();
161 if( impl.mController->GetDefaultFontStyle() != fontStyle )
163 impl.mController->SetDefaultFontStyle( fontStyle );
168 case Toolkit::TextLabel::Property::POINT_SIZE:
170 if( impl.mController )
172 const float pointSize = value.Get< float >();
174 if( !Equals( impl.mController->GetDefaultPointSize(), pointSize ) )
176 impl.mController->SetDefaultPointSize( pointSize );
181 case Toolkit::TextLabel::Property::MULTI_LINE:
183 if( impl.mController )
185 impl.mController->SetMultiLineEnabled( value.Get< bool >() );
189 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
191 if( impl.mController )
193 LayoutEngine::HorizontalAlignment alignment( LayoutEngine::HORIZONTAL_ALIGN_BEGIN );
194 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
195 HORIZONTAL_ALIGNMENT_STRING_TABLE,
196 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
199 impl.mController->SetHorizontalAlignment( alignment );
204 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
206 if( impl.mController )
208 LayoutEngine::VerticalAlignment alignment( LayoutEngine::VERTICAL_ALIGN_BOTTOM );
209 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
210 VERTICAL_ALIGNMENT_STRING_TABLE,
211 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
214 impl.mController->SetVerticalAlignment( alignment );
220 case Toolkit::TextLabel::Property::TEXT_COLOR:
222 if( impl.mController )
224 const Vector4 textColor = value.Get< Vector4 >();
225 if( impl.mController->GetTextColor() != textColor )
227 impl.mController->SetTextColor( textColor );
228 impl.mRenderer.Reset();
234 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
236 if( impl.mController )
238 const Vector2 shadowOffset = value.Get< Vector2 >();
239 if ( impl.mController->GetShadowOffset() != shadowOffset )
241 impl.mController->SetShadowOffset( shadowOffset );
242 impl.mRenderer.Reset();
247 case Toolkit::TextLabel::Property::SHADOW_COLOR:
249 if( impl.mController )
251 const Vector4 shadowColor = value.Get< Vector4 >();
252 if ( impl.mController->GetShadowColor() != shadowColor )
254 impl.mController->SetShadowColor( shadowColor );
255 impl.mRenderer.Reset();
260 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
262 if( impl.mController )
264 const Vector4 color = value.Get< Vector4 >();
265 if ( impl.mController->GetUnderlineColor() != color )
267 impl.mController->SetUnderlineColor( color );
268 impl.mRenderer.Reset();
273 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
275 if( impl.mController )
277 const bool enabled = value.Get< bool >();
278 if ( impl.mController->IsUnderlineEnabled() != enabled )
280 impl.mController->SetUnderlineEnabled( enabled );
281 impl.mRenderer.Reset();
287 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
289 if( impl.mController )
291 float height = value.Get< float >();
292 if ( impl.mController->GetUnderlineHeight() != height )
294 impl.mController->SetUnderlineHeight( height );
295 impl.mRenderer.Reset();
304 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
306 Property::Value value;
308 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
312 TextLabel& impl( GetImpl( label ) );
315 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
317 value = impl.mRenderingBackend;
320 case Toolkit::TextLabel::Property::TEXT:
322 if( impl.mController )
325 impl.mController->GetText( text );
330 case Toolkit::TextLabel::Property::FONT_FAMILY:
332 if( impl.mController )
334 value = impl.mController->GetDefaultFontFamily();
338 case Toolkit::TextLabel::Property::FONT_STYLE:
340 if( impl.mController )
342 value = impl.mController->GetDefaultFontStyle();
346 case Toolkit::TextLabel::Property::POINT_SIZE:
348 if( impl.mController )
350 value = impl.mController->GetDefaultPointSize();
354 case Toolkit::TextLabel::Property::MULTI_LINE:
356 if( impl.mController )
358 value = impl.mController->IsMultiLineEnabled();
362 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
364 if( impl.mController )
366 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetHorizontalAlignment(),
367 HORIZONTAL_ALIGNMENT_STRING_TABLE,
368 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
371 value = std::string( name );
376 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
378 if( impl.mController )
380 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetVerticalAlignment(),
381 VERTICAL_ALIGNMENT_STRING_TABLE,
382 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
385 value = std::string( name );
390 case Toolkit::TextLabel::Property::TEXT_COLOR:
392 if ( impl.mController )
394 value = impl.mController->GetTextColor();
398 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
400 if ( impl.mController )
402 value = impl.mController->GetShadowOffset();
406 case Toolkit::TextLabel::Property::SHADOW_COLOR:
408 if ( impl.mController )
410 value = impl.mController->GetShadowColor();
414 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
416 if ( impl.mController )
418 value = impl.mController->GetUnderlineColor();
422 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
424 if ( impl.mController )
426 value = impl.mController->IsUnderlineEnabled();
430 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
432 if ( impl.mController )
434 value = impl.mController->GetUnderlineHeight();
444 void TextLabel::OnInitialize()
448 mController = Text::Controller::New( *this );
450 // Use height-for-width negotiation by default
451 self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
452 self.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
454 // Enable the text ellipsis.
455 LayoutEngine& engine = mController->GetLayoutEngine();
456 engine.SetTextEllipsisEnabled( true );
457 self.OnStageSignal().Connect( this, &TextLabel::OnStageConnect );
460 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
462 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
465 Vector3 TextLabel::GetNaturalSize()
467 return mController->GetNaturalSize();
470 float TextLabel::GetHeightForWidth( float width )
472 return mController->GetHeightForWidth( width );
475 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
477 if( mController->Relayout( size ) ||
482 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
488 void TextLabel::RequestTextRelayout()
493 void TextLabel::RenderText()
496 Actor renderableActor;
499 renderableActor = mRenderer->Render( mController->GetView(), self.GetHierarchyDepth() );
502 if( renderableActor != mRenderableActor )
504 UnparentAndReset( mRenderableActor );
506 if( renderableActor )
508 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
509 renderableActor.SetPosition( alignmentOffset.x, alignmentOffset.y );
511 self.Add( renderableActor );
513 mRenderableActor = renderableActor;
517 void TextLabel::OnStageConnect( Dali::Actor actor )
519 if ( mHasBeenStaged )
525 mHasBeenStaged = true;
529 void TextLabel::AddDecoration( Actor& actor, bool needsClipping )
531 // TextLabel does not show decorations
534 void TextLabel::OnStageConnection( int depth )
536 // Call the Control::OnStageConnection() to set the depth of the background.
537 Control::OnStageConnection( depth );
539 // The depth of the text renderer is set in the RenderText() called from OnRelayout().
542 void TextLabel::TextChanged()
544 // TextLabel does not provide a signal for this
547 void TextLabel::MaxLengthReached()
549 // Pure Virtual from TextController Interface, only needed when inputting text
552 TextLabel::TextLabel()
553 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
554 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
555 mHasBeenStaged( false )
559 TextLabel::~TextLabel()
563 } // namespace Internal
565 } // namespace Toolkit