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/controls/control-depth-index-ranges.h>
27 #include <dali-toolkit/public-api/text/rendering-backend.h>
28 #include <dali-toolkit/internal/controls/text-controls/text-font-style.h>
29 #include <dali-toolkit/internal/text/rendering/text-backend.h>
30 #include <dali-toolkit/internal/text/text-view.h>
31 #include <dali-toolkit/internal/styling/style-manager-impl.h>
33 using Dali::Toolkit::Text::LayoutEngine;
34 using Dali::Toolkit::Text::Backend;
47 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
53 #if defined(DEBUG_ENABLED)
54 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_CONTROLS");
57 const Scripting::StringEnum 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 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::TextLabel::New();
79 // Setup properties, signals and actions using the type-registry.
80 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextLabel, Toolkit::Control, Create );
82 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "renderingBackend", INTEGER, RENDERING_BACKEND )
83 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "text", STRING, TEXT )
84 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "fontFamily", STRING, FONT_FAMILY )
85 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "fontStyle", STRING, FONT_STYLE )
86 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "pointSize", FLOAT, POINT_SIZE )
87 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "multiLine", BOOLEAN, MULTI_LINE )
88 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "horizontalAlignment", STRING, HORIZONTAL_ALIGNMENT )
89 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "verticalAlignment", STRING, VERTICAL_ALIGNMENT )
90 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "textColor", VECTOR4, TEXT_COLOR )
91 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "shadowOffset", VECTOR2, SHADOW_OFFSET )
92 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "shadowColor", VECTOR4, SHADOW_COLOR )
93 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underlineEnabled", BOOLEAN, UNDERLINE_ENABLED )
94 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underlineColor", VECTOR4, UNDERLINE_COLOR )
95 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underlineHeight", FLOAT, UNDERLINE_HEIGHT )
97 DALI_TYPE_REGISTRATION_END()
101 Toolkit::TextLabel TextLabel::New()
103 // Create the implementation, temporarily owned by this handle on stack
104 IntrusivePtr< TextLabel > impl = new TextLabel();
106 // Pass ownership to CustomActor handle
107 Toolkit::TextLabel handle( *impl );
109 // Second-phase init of the implementation
110 // This can only be done after the CustomActor connection has been made...
116 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
118 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
122 TextLabel& impl( GetImpl( label ) );
125 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
127 const int backend = value.Get< int >();
129 if( impl.mRenderingBackend != backend )
131 impl.mRenderingBackend = backend;
132 impl.mRenderer.Reset();
133 impl.RequestTextRelayout();
137 case Toolkit::TextLabel::Property::TEXT:
139 if( impl.mController )
141 impl.mController->SetText( value.Get< std::string >() );
145 case Toolkit::TextLabel::Property::FONT_FAMILY:
147 if( impl.mController )
149 const std::string fontFamily = value.Get< std::string >();
151 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::SetProperty Property::FONT_FAMILY newFont(%s)\n", fontFamily.c_str() );
152 impl.mController->SetDefaultFontFamily( fontFamily );
156 case Toolkit::TextLabel::Property::FONT_STYLE:
158 SetFontStyleProperty( impl.mController, value );
161 case Toolkit::TextLabel::Property::POINT_SIZE:
163 if( impl.mController )
165 const float pointSize = value.Get< float >();
167 if( !Equals( impl.mController->GetDefaultPointSize(), pointSize ) )
169 impl.mController->SetDefaultPointSize( pointSize );
174 case Toolkit::TextLabel::Property::MULTI_LINE:
176 if( impl.mController )
178 impl.mController->SetMultiLineEnabled( value.Get< bool >() );
182 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
184 if( impl.mController )
186 LayoutEngine::HorizontalAlignment alignment( LayoutEngine::HORIZONTAL_ALIGN_BEGIN );
187 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
188 HORIZONTAL_ALIGNMENT_STRING_TABLE,
189 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
192 impl.mController->SetHorizontalAlignment( alignment );
197 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
199 if( impl.mController )
201 LayoutEngine::VerticalAlignment alignment( LayoutEngine::VERTICAL_ALIGN_BOTTOM );
202 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
203 VERTICAL_ALIGNMENT_STRING_TABLE,
204 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
207 impl.mController->SetVerticalAlignment( alignment );
213 case Toolkit::TextLabel::Property::TEXT_COLOR:
215 if( impl.mController )
217 const Vector4 textColor = value.Get< Vector4 >();
218 if( impl.mController->GetTextColor() != textColor )
220 impl.mController->SetTextColor( textColor );
221 impl.mRenderer.Reset();
227 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
229 if( impl.mController )
231 const Vector2 shadowOffset = value.Get< Vector2 >();
232 if ( impl.mController->GetShadowOffset() != shadowOffset )
234 impl.mController->SetShadowOffset( shadowOffset );
235 impl.mRenderer.Reset();
240 case Toolkit::TextLabel::Property::SHADOW_COLOR:
242 if( impl.mController )
244 const Vector4 shadowColor = value.Get< Vector4 >();
245 if ( impl.mController->GetShadowColor() != shadowColor )
247 impl.mController->SetShadowColor( shadowColor );
248 impl.mRenderer.Reset();
253 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
255 if( impl.mController )
257 const Vector4 color = value.Get< Vector4 >();
258 if ( impl.mController->GetUnderlineColor() != color )
260 impl.mController->SetUnderlineColor( color );
261 impl.mRenderer.Reset();
266 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
268 if( impl.mController )
270 const bool enabled = value.Get< bool >();
271 if ( impl.mController->IsUnderlineEnabled() != enabled )
273 impl.mController->SetUnderlineEnabled( enabled );
274 impl.mRenderer.Reset();
280 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
282 if( impl.mController )
284 float height = value.Get< float >();
285 if( fabsf( impl.mController->GetUnderlineHeight() - height ) > Math::MACHINE_EPSILON_1000 )
287 impl.mController->SetUnderlineHeight( height );
288 impl.mRenderer.Reset();
297 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
299 Property::Value value;
301 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
305 TextLabel& impl( GetImpl( label ) );
308 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
310 value = impl.mRenderingBackend;
313 case Toolkit::TextLabel::Property::TEXT:
315 if( impl.mController )
318 impl.mController->GetText( text );
323 case Toolkit::TextLabel::Property::FONT_FAMILY:
325 if( impl.mController )
327 value = impl.mController->GetDefaultFontFamily();
331 case Toolkit::TextLabel::Property::FONT_STYLE:
333 GetFontStyleProperty( impl.mController, value );
336 case Toolkit::TextLabel::Property::POINT_SIZE:
338 if( impl.mController )
340 value = impl.mController->GetDefaultPointSize();
344 case Toolkit::TextLabel::Property::MULTI_LINE:
346 if( impl.mController )
348 value = impl.mController->IsMultiLineEnabled();
352 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
354 if( impl.mController )
356 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetHorizontalAlignment(),
357 HORIZONTAL_ALIGNMENT_STRING_TABLE,
358 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
361 value = std::string( name );
366 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
368 if( impl.mController )
370 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetVerticalAlignment(),
371 VERTICAL_ALIGNMENT_STRING_TABLE,
372 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
375 value = std::string( name );
380 case Toolkit::TextLabel::Property::TEXT_COLOR:
382 if ( impl.mController )
384 value = impl.mController->GetTextColor();
388 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
390 if ( impl.mController )
392 value = impl.mController->GetShadowOffset();
396 case Toolkit::TextLabel::Property::SHADOW_COLOR:
398 if ( impl.mController )
400 value = impl.mController->GetShadowColor();
404 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
406 if ( impl.mController )
408 value = impl.mController->GetUnderlineColor();
412 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
414 if ( impl.mController )
416 value = impl.mController->IsUnderlineEnabled();
420 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
422 if ( impl.mController )
424 value = impl.mController->GetUnderlineHeight();
434 void TextLabel::OnInitialize()
438 mController = Text::Controller::New( *this );
440 // Use height-for-width negotiation by default
441 self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
442 self.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
444 // Enable the text ellipsis.
445 LayoutEngine& engine = mController->GetLayoutEngine();
447 engine.SetTextEllipsisEnabled( true );
448 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
450 self.OnStageSignal().Connect( this, &TextLabel::OnStageConnect );
453 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
456 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextLabel::OnStyleChange\n");
460 case StyleChange::DEFAULT_FONT_CHANGE:
462 // Property system did not set the font so should update it.
463 std::string newFont = styleManager.GetDefaultFontFamily();
464 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_CHANGE newFont(%s)\n", newFont.c_str() );
465 mController->UpdateAfterFontChange( newFont );
469 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
471 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_SIZE_CHANGE (%f)\n", mController->GetDefaultPointSize() );
473 if ( (mController->GetDefaultPointSize() <= 0.0f) ) // If DefaultPointSize not set by Property system it will be 0.0f
475 // Property system did not set the PointSize so should update it.
476 // todo instruct text-controller to update model
480 case StyleChange::THEME_CHANGE:
482 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
488 Vector3 TextLabel::GetNaturalSize()
490 return mController->GetNaturalSize();
493 float TextLabel::GetHeightForWidth( float width )
495 return mController->GetHeightForWidth( width );
498 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
500 if( mController->Relayout( size ) ||
505 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
511 void TextLabel::RequestTextRelayout()
516 void TextLabel::RenderText()
519 Actor renderableActor;
522 renderableActor = mRenderer->Render( mController->GetView(), TEXT_DEPTH_INDEX );
525 if( renderableActor != mRenderableActor )
527 UnparentAndReset( mRenderableActor );
529 if( renderableActor )
531 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
532 renderableActor.SetPosition( alignmentOffset.x, alignmentOffset.y );
534 self.Add( renderableActor );
536 mRenderableActor = renderableActor;
540 void TextLabel::OnStageConnect( Dali::Actor actor )
542 if ( mHasBeenStaged )
548 mHasBeenStaged = true;
552 void TextLabel::AddDecoration( Actor& actor, bool needsClipping )
554 // TextLabel does not show decorations
557 void TextLabel::OnStageConnection( int depth )
559 // Call the Control::OnStageConnection() to set the depth of the background.
560 Control::OnStageConnection( depth );
562 // The depth of the text renderer is set in the RenderText() called from OnRelayout().
565 void TextLabel::TextChanged()
567 // TextLabel does not provide a signal for this
570 void TextLabel::MaxLengthReached()
572 // Pure Virtual from TextController Interface, only needed when inputting text
575 TextLabel::TextLabel()
576 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
577 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
578 mHasBeenStaged( false )
582 TextLabel::~TextLabel()
586 } // namespace Internal
588 } // namespace Toolkit