Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-field-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-field-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/images/resource-image.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/integration-api/debug.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/public-api/text/layouts/layout-engine.h>
28 #include <dali-toolkit/public-api/text/rendering/basic/text-basic-renderer.h> // TODO - Get from RendererFactory
29
30 using namespace Dali::Toolkit::Text;
31
32 namespace
33 {
34
35 } // namespace
36
37 namespace Dali
38 {
39
40 namespace Toolkit
41 {
42
43 const Property::Index TextField::PROPERTY_PLACEHOLDER_TEXT(       Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX );
44 const Property::Index TextField::PROPERTY_TEXT(                   Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 1 );
45 const Property::Index TextField::PROPERTY_CURSOR_IMAGE(           Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 2 );
46 const Property::Index TextField::PROPERTY_PRIMARY_CURSOR_COLOR(   Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 3 );
47 const Property::Index TextField::PROPERTY_SECONDARY_CURSOR_COLOR( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 4 );
48 const Property::Index TextField::PROPERTY_ENABLE_CURSOR_BLINK(    Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 5 );
49 const Property::Index TextField::PROPERTY_CURSOR_BLINK_INTERVAL(  Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 6 );
50 const Property::Index TextField::PROPERTY_CURSOR_BLINK_DURATION(  Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 7 );
51
52 namespace Internal
53 {
54
55 namespace
56 {
57
58 // Type registration
59 BaseHandle Create()
60 {
61   return Toolkit::TextField::New();
62 }
63
64 TypeRegistration mType( typeid(Toolkit::TextField), typeid(Toolkit::Control), Create );
65
66 PropertyRegistration property1( mType, "placeholder-text",       Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT,       Property::STRING,  &TextField::SetProperty, &TextField::GetProperty );
67 PropertyRegistration property2( mType, "text",                   Toolkit::TextField::PROPERTY_TEXT,                   Property::STRING,  &TextField::SetProperty, &TextField::GetProperty );
68 PropertyRegistration property3( mType, "cursor-image",           Toolkit::TextField::PROPERTY_CURSOR_IMAGE,           Property::STRING,  &TextField::SetProperty, &TextField::GetProperty );
69 PropertyRegistration property4( mType, "primary-cursor-color",   Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR,   Property::VECTOR4, &TextField::SetProperty, &TextField::GetProperty );
70 PropertyRegistration property5( mType, "secondary-cursor-color", Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR, Property::VECTOR4, &TextField::SetProperty, &TextField::GetProperty );
71 PropertyRegistration property6( mType, "enable-cursor-blink",    Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK,    Property::BOOLEAN, &TextField::SetProperty, &TextField::GetProperty );
72 PropertyRegistration property7( mType, "cursor-blink-interval",  Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL,  Property::FLOAT,   &TextField::SetProperty, &TextField::GetProperty );
73 PropertyRegistration property8( mType, "cursor-blink-duration",  Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION,  Property::FLOAT,   &TextField::SetProperty, &TextField::GetProperty );
74
75 } // namespace
76
77 Toolkit::TextField TextField::New()
78 {
79   // Create the implementation, temporarily owned by this handle on stack
80   IntrusivePtr< TextField > impl = new TextField();
81
82   // Pass ownership to CustomActor handle
83   Toolkit::TextField handle( *impl );
84
85   // Second-phase init of the implementation
86   // This can only be done after the CustomActor connection has been made...
87   impl->Initialize();
88
89   return handle;
90 }
91
92 void TextField::SetRenderer( Text::RendererPtr renderer )
93 {
94   mRenderer = renderer;
95 }
96
97 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
98 {
99   Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
100
101   if( textField )
102   {
103     TextField& impl( GetImpl( textField ) );
104
105     switch( index )
106     {
107       case Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT:
108       {
109         if( impl.mController )
110         {
111           //impl.mController->SetPlaceholderText( value.Get< std::string >() ); TODO
112         }
113         break;
114       }
115       case Toolkit::TextField::PROPERTY_TEXT:
116       {
117         if( impl.mController )
118         {
119           impl.mController->SetText( value.Get< std::string >() );
120         }
121         break;
122       }
123       case Toolkit::TextField::PROPERTY_CURSOR_IMAGE:
124       {
125         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
126
127         if( impl.mDecorator )
128         {
129           impl.mDecorator->SetCursorImage( image );
130         }
131         break;
132       }
133       case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
134       {
135         if( impl.mDecorator )
136         {
137           impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
138         }
139         break;
140       }
141       case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
142       {
143         if( impl.mDecorator )
144         {
145           impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
146         }
147         break;
148       }
149       case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
150       {
151         if( impl.mController )
152         {
153           //impl.mController->SetEnableCursorBlink( value.Get< bool >() ); TODO
154         }
155         break;
156       }
157       case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
158       {
159         if( impl.mDecorator )
160         {
161           impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
162         }
163         break;
164       }
165       case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
166       {
167         if( impl.mDecorator )
168         {
169           impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
170         }
171         break;
172       }
173     }
174   }
175 }
176
177 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
178 {
179   Property::Value value;
180
181   Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
182
183   if( textField )
184   {
185     TextField& impl( GetImpl( textField ) );
186
187     switch( index )
188     {
189       case Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT:
190       {
191         DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
192         break;
193       }
194       case Toolkit::TextField::PROPERTY_TEXT:
195       {
196         DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
197         break;
198       }
199       case Toolkit::TextField::PROPERTY_CURSOR_IMAGE:
200       {
201         if( impl.mDecorator )
202         {
203           ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
204           if( image )
205           {
206             value = image.GetUrl();
207           }
208         }
209         break;
210       }
211       case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
212       {
213         if( impl.mDecorator )
214         {
215           value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
216         }
217         break;
218       }
219       case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
220       {
221         if( impl.mDecorator )
222         {
223           value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
224         }
225         break;
226       }
227       case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
228       {
229         //value = impl.mController->GetEnableCursorBlink(); TODO
230         break;
231       }
232       case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
233       {
234         if( impl.mDecorator )
235         {
236           value = impl.mDecorator->GetCursorBlinkInterval();
237         }
238         break;
239       }
240       case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
241       {
242         if( impl.mDecorator )
243         {
244           value = impl.mDecorator->GetCursorBlinkDuration();
245         }
246         break;
247       }
248     }
249   }
250
251   return value;
252 }
253
254 void TextField::OnInitialize()
255 {
256   mDecorator = Text::Decorator::New( *this );
257
258   mController = Text::Controller::New();
259   mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
260   //mController->EnableTextInput( mDecorator ); TODO
261 }
262
263 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
264 {
265   if( mController->Relayout( size ) )
266   {
267     if( !mRenderer )
268     {
269       // TODO - Get from RendererFactory
270       mRenderer = Dali::Toolkit::Text::BasicRenderer::New();
271     }
272
273     if( mRenderer )
274     {
275       Actor renderableActor = mRenderer->Render( mController->GetView() );
276
277       if( renderableActor )
278       {
279         Self().Add( renderableActor );
280       }
281     }
282   }
283 }
284
285 TextField::TextField()
286 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) )
287 {
288 }
289
290 TextField::~TextField()
291 {
292 }
293
294 } // namespace Internal
295
296 } // namespace Toolkit
297
298 } // namespace Dali