Added some TextField boilerplate code
[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/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         Image image = Image::New( value.Get< std::string >() );
126         //ResourceImage image = ResourceImage::New( value.Get< std::string >() );
127
128         if( impl.mDecorator )
129         {
130           impl.mDecorator->SetCursorImage( image );
131         }
132         break;
133       }
134       case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
135       {
136         if( impl.mDecorator )
137         {
138           impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
139         }
140         break;
141       }
142       case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
143       {
144         if( impl.mDecorator )
145         {
146           impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
147         }
148         break;
149       }
150       case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
151       {
152         if( impl.mController )
153         {
154           //impl.mController->SetEnableCursorBlink( value.Get< bool >() ); TODO
155         }
156         break;
157       }
158       case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
159       {
160         if( impl.mDecorator )
161         {
162           impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
163         }
164         break;
165       }
166       case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
167       {
168         if( impl.mDecorator )
169         {
170           impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
171         }
172         break;
173       }
174     }
175   }
176 }
177
178 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
179 {
180   Property::Value value;
181
182   Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
183
184   if( textField )
185   {
186     TextField& impl( GetImpl( textField ) );
187
188     switch( index )
189     {
190       case Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT:
191       {
192         DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
193         break;
194       }
195       case Toolkit::TextField::PROPERTY_TEXT:
196       {
197         DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
198         break;
199       }
200       case Toolkit::TextField::PROPERTY_CURSOR_IMAGE:
201       {
202         if( impl.mDecorator )
203         {
204           Image image = impl.mDecorator->GetCursorImage();
205           //ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
206           if( image )
207           {
208             value = image.GetFilename();
209             //value = image.GetUrl();
210           }
211         }
212         break;
213       }
214       case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
215       {
216         if( impl.mDecorator )
217         {
218           value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
219         }
220         break;
221       }
222       case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
223       {
224         if( impl.mDecorator )
225         {
226           value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
227         }
228         break;
229       }
230       case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
231       {
232         //value = impl.mController->GetEnableCursorBlink(); TODO
233         break;
234       }
235       case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
236       {
237         if( impl.mDecorator )
238         {
239           value = impl.mDecorator->GetCursorBlinkInterval();
240         }
241         break;
242       }
243       case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
244       {
245         if( impl.mDecorator )
246         {
247           value = impl.mDecorator->GetCursorBlinkDuration();
248         }
249         break;
250       }
251     }
252   }
253
254   return value;
255 }
256
257 void TextField::OnInitialize()
258 {
259   mDecorator = Text::Decorator::New( *this );
260
261   mController = Text::Controller::New();
262   mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
263   //mController->EnableTextInput( mDecorator ); TODO
264 }
265
266 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
267 {
268   if( mController->Relayout( size ) )
269   {
270     if( !mRenderer )
271     {
272       // TODO - Get from RendererFactory
273       mRenderer = Dali::Toolkit::Text::BasicRenderer::New();
274     }
275
276     if( mRenderer )
277     {
278       Actor renderableActor = mRenderer->Render( mController->GetView() );
279
280       if( renderableActor )
281       {
282         Self().Add( renderableActor );
283       }
284     }
285   }
286 }
287
288 TextField::TextField()
289 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) )
290 {
291 }
292
293 TextField::~TextField()
294 {
295 }
296
297 } // namespace Internal
298
299 } // namespace Toolkit
300
301 } // namespace Dali