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/integration-api/debug.h>
26 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
27 #include <dali-toolkit/internal/text/rendering/text-backend.h>
29 using Dali::Toolkit::Text::LayoutEngine;
30 using Dali::Toolkit::Text::Backend;
35 const unsigned int DEFAULT_RENDERING_BACKEND = 0;
45 const Property::Index TextLabel::PROPERTY_RENDERING_BACKEND( Internal::TextLabel::TEXTLABEL_PROPERTY_START_INDEX );
46 const Property::Index TextLabel::PROPERTY_TEXT( Internal::TextLabel::TEXTLABEL_PROPERTY_START_INDEX + 1 );
47 const Property::Index TextLabel::PROPERTY_MULTI_LINE( Internal::TextLabel::TEXTLABEL_PROPERTY_START_INDEX + 2 );
58 return Toolkit::TextLabel::New();
61 TypeRegistration mType( typeid(Toolkit::TextLabel), typeid(Toolkit::Control), Create );
63 PropertyRegistration property1( mType, "rendering-backend", Toolkit::TextLabel::PROPERTY_RENDERING_BACKEND, Property::INTEGER, &TextLabel::SetProperty, &TextLabel::GetProperty );
64 PropertyRegistration property2( mType, "text", Toolkit::TextLabel::PROPERTY_TEXT, Property::STRING, &TextLabel::SetProperty, &TextLabel::GetProperty );
65 PropertyRegistration property3( mType, "multi-line", Toolkit::TextLabel::PROPERTY_MULTI_LINE, Property::STRING, &TextLabel::SetProperty, &TextLabel::GetProperty );
69 Toolkit::TextLabel TextLabel::New()
71 // Create the implementation, temporarily owned by this handle on stack
72 IntrusivePtr< TextLabel > impl = new TextLabel();
74 // Pass ownership to CustomActor handle
75 Toolkit::TextLabel handle( *impl );
77 // Second-phase init of the implementation
78 // This can only be done after the CustomActor connection has been made...
84 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
86 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
90 TextLabel& impl( GetImpl( label ) );
93 case Toolkit::TextLabel::PROPERTY_RENDERING_BACKEND:
95 unsigned int backend = value.Get< int >();
97 if( impl.mRenderingBackend != backend )
99 impl.mRenderingBackend = static_cast< unsigned int >( backend );
100 impl.mRenderer.Reset();
101 impl.RequestTextRelayout();
105 case Toolkit::TextLabel::PROPERTY_TEXT:
107 if( impl.mController )
109 impl.mController->SetText( value.Get< std::string >() );
113 case Toolkit::TextLabel::PROPERTY_MULTI_LINE:
115 if( impl.mController )
117 LayoutEngine::Layout layout = value.Get< bool >() ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
118 impl.mController->GetLayoutEngine().SetLayout( layout );
119 impl.RequestTextRelayout();
127 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
129 Property::Value value;
131 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
135 TextLabel& impl( GetImpl( label ) );
138 case Toolkit::TextLabel::PROPERTY_RENDERING_BACKEND:
140 value = impl.mRenderingBackend;
144 case Toolkit::TextLabel::PROPERTY_TEXT:
146 if( impl.mController )
149 impl.mController->GetText( text );
153 DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
157 case Toolkit::TextLabel::PROPERTY_MULTI_LINE:
159 if( impl.mController )
161 value = static_cast<bool>( LayoutEngine::MULTI_LINE_BOX == impl.mController->GetLayoutEngine().GetLayout() );
171 void TextLabel::OnInitialize()
173 mController = Text::Controller::New( *this );
176 Vector3 TextLabel::GetNaturalSize()
178 return mController->GetNaturalSize();
181 float TextLabel::GetHeightForWidth( float width )
183 return mController->GetHeightForWidth( width );
186 void TextLabel::OnRelayout( const Vector2& size, ActorSizeContainer& container )
188 if( mController->Relayout( size ) ||
193 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
196 RenderableActor renderableActor;
199 renderableActor = mRenderer->Render( mController->GetView() );
202 if( renderableActor != mRenderableActor )
204 UnparentAndReset( mRenderableActor );
206 if( renderableActor )
208 Self().Add( renderableActor );
211 mRenderableActor = renderableActor;
216 void TextLabel::RequestTextRelayout()
221 TextLabel::TextLabel()
222 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) ),
223 mRenderingBackend( DEFAULT_RENDERING_BACKEND )
227 TextLabel::~TextLabel()
231 } // namespace Internal
233 } // namespace Toolkit