TextLabel::GetHeightForWidth() implementation.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-label-impl.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/text-controls/text-label-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/integration-api/debug.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/public-api/text/layouts/layout-engine.h>
27 #include <dali-toolkit/public-api/text/rendering/basic/text-basic-renderer.h> // TODO - Get from RendererFactory
28
29 using Dali::Toolkit::Text::LayoutEngine;
30
31 namespace
32 {
33
34 } // namespace
35
36 namespace Dali
37 {
38
39 namespace Toolkit
40 {
41
42 const Property::Index TextLabel::PROPERTY_TEXT(       Internal::TextLabel::TEXTLABEL_PROPERTY_START_INDEX );
43 const Property::Index TextLabel::PROPERTY_MULTI_LINE( Internal::TextLabel::TEXTLABEL_PROPERTY_START_INDEX + 1 );
44
45 namespace Internal
46 {
47
48 namespace
49 {
50
51 // Type registration
52 BaseHandle Create()
53 {
54   return Toolkit::TextLabel::New();
55 }
56
57 TypeRegistration mType( typeid(Toolkit::TextLabel), typeid(Toolkit::Control), Create );
58
59 PropertyRegistration property1( mType, "text",       Toolkit::TextLabel::PROPERTY_TEXT,       Property::STRING, &TextLabel::SetProperty, &TextLabel::GetProperty );
60 PropertyRegistration property2( mType, "multi-line", Toolkit::TextLabel::PROPERTY_MULTI_LINE, Property::STRING, &TextLabel::SetProperty, &TextLabel::GetProperty );
61
62 } // namespace
63
64 Toolkit::TextLabel TextLabel::New()
65 {
66   // Create the implementation, temporarily owned by this handle on stack
67   IntrusivePtr< TextLabel > impl = new TextLabel();
68
69   // Pass ownership to CustomActor handle
70   Toolkit::TextLabel handle( *impl );
71
72   // Second-phase init of the implementation
73   // This can only be done after the CustomActor connection has been made...
74   impl->Initialize();
75
76   return handle;
77 }
78
79 void TextLabel::SetRenderer( Text::RendererPtr renderer )
80 {
81   mRenderer = renderer;
82 }
83
84 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
85 {
86   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
87
88   if( label )
89   {
90     TextLabel& labelImpl( GetImpl( label ) );
91     switch( index )
92     {
93       case Toolkit::TextLabel::PROPERTY_TEXT:
94       {
95         labelImpl.SetText( value.Get< std::string >() );
96         break;
97       }
98       case Toolkit::TextLabel::PROPERTY_MULTI_LINE:
99       {
100         labelImpl.SetMultiLine( value.Get< bool >() );
101         break;
102       }
103     }
104   }
105 }
106
107 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
108 {
109   Property::Value value;
110
111   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
112
113   if( label )
114   {
115     switch( index )
116     {
117       case Toolkit::TextLabel::PROPERTY_TEXT:
118       {
119         DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
120         break;
121       }
122     }
123   }
124
125   return value;
126 }
127
128 void TextLabel::OnInitialize()
129 {
130   mController = Text::Controller::New();
131 }
132
133 Vector3 TextLabel::GetNaturalSize()
134 {
135   return mController->GetNaturalSize();
136 }
137
138 float TextLabel::GetHeightForWidth( float width )
139 {
140   return mController->GetHeightForWidth( width );
141 }
142
143 void TextLabel::OnRelayout( const Vector2& size, ActorSizeContainer& container )
144 {
145   if( mController->Relayout( size ) )
146   {
147     if( !mRenderer )
148     {
149       // TODO - Get from RendererFactory
150       mRenderer = Dali::Toolkit::Text::BasicRenderer::New();
151     }
152
153     if( mRenderer )
154     {
155       Actor renderableActor = mRenderer->Render( mController->GetView() );
156
157       if( renderableActor )
158       {
159         Self().Add( renderableActor );
160       }
161     }
162   }
163 }
164
165 void TextLabel::SetText( const std::string& text )
166 {
167   if( mController )
168   {
169     // The Controller updates the View for the renderer
170     mController->SetText( text );
171   }
172 }
173
174 void TextLabel::SetMultiLine( bool multiLine )
175 {
176   if( mController )
177   {
178     if( multiLine )
179     {
180       mController->GetLayoutEngine().SetLayout( LayoutEngine::MULTI_LINE_BOX );
181     }
182     else
183     {
184       mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
185     }
186   }
187 }
188
189 TextLabel::TextLabel()
190 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) )
191 {
192 }
193
194 TextLabel::~TextLabel()
195 {
196 }
197
198 } // namespace Internal
199
200 } // namespace Toolkit
201
202 } // namespace Dali