753d34743f8fabc07fd1130732b65a60bc7018c7
[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 <string>
23 #include <iostream>
24 #include <cstring>
25 #include <dali/public-api/adaptor-framework/key.h>
26 #include <dali/public-api/common/stage.h>
27 #include <dali/public-api/images/resource-image.h>
28 #include <dali/public-api/object/type-registry.h>
29 #include <dali/devel-api/object/type-registry-helper.h>
30 #include <dali/devel-api/scripting/scripting.h>
31 #include <dali/devel-api/adaptor-framework/virtual-keyboard.h>
32 #include <dali/integration-api/debug.h>
33
34 // INTERNAL INCLUDES
35 #include <dali-toolkit/public-api/text/rendering-backend.h>
36 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
37 #include <dali-toolkit/internal/text/rendering/text-backend.h>
38 #include <dali-toolkit/internal/styling/style-manager-impl.h>
39
40 using namespace Dali::Toolkit::Text;
41
42 namespace Dali
43 {
44
45 namespace Toolkit
46 {
47
48 namespace Internal
49 {
50
51 namespace // unnamed namespace
52 {
53
54 #if defined(DEBUG_ENABLED)
55   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_CONTROLS");
56 #endif
57
58   const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
59
60 } // unnamed namespace
61
62 namespace
63 {
64
65 const Scripting::StringEnum< Toolkit::Text::LayoutEngine::HorizontalAlignment > HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
66 {
67   { "BEGIN",  Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_BEGIN  },
68   { "CENTER", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_CENTER },
69   { "END",    Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_END    },
70 };
71 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
72
73 const Scripting::StringEnum< Toolkit::Text::LayoutEngine::VerticalAlignment > VERTICAL_ALIGNMENT_STRING_TABLE[] =
74 {
75   { "TOP",    Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_TOP    },
76   { "CENTER", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_CENTER },
77   { "BOTTOM", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_BOTTOM },
78 };
79 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
80
81 // Type registration
82 BaseHandle Create()
83 {
84   return Toolkit::TextField::New();
85 }
86
87 // Setup properties, signals and actions using the type-registry.
88 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextField, Toolkit::Control, Create );
89
90 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "rendering-backend",                    INTEGER,   RENDERING_BACKEND                    )
91 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "text",                                 STRING,    TEXT                                 )
92 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "placeholder-text",                     STRING,    PLACEHOLDER_TEXT                     )
93 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "placeholder-text-focused",             STRING,    PLACEHOLDER_TEXT_FOCUSED             )
94 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "font-family",                          STRING,    FONT_FAMILY                          )
95 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "font-style",                           STRING,    FONT_STYLE                           )
96 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "point-size",                           FLOAT,     POINT_SIZE                           )
97 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "max-length",                           INTEGER,   MAX_LENGTH                           )
98 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "exceed-policy",                        INTEGER,   EXCEED_POLICY                        )
99 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "horizontal-alignment",                 STRING,    HORIZONTAL_ALIGNMENT                 )
100 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "vertical-alignment",                   STRING,    VERTICAL_ALIGNMENT                   )
101 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "text-color",                           VECTOR4,   TEXT_COLOR                           )
102 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "placeholder-text-color",               VECTOR4,   PLACEHOLDER_TEXT_COLOR               )
103 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "shadow-offset",                        VECTOR2,   SHADOW_OFFSET                        )
104 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "shadow-color",                         VECTOR4,   SHADOW_COLOR                         )
105 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "primary-cursor-color",                 VECTOR4,   PRIMARY_CURSOR_COLOR                 )
106 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "secondary-cursor-color",               VECTOR4,   SECONDARY_CURSOR_COLOR               )
107 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "enable-cursor-blink",                  BOOLEAN,   ENABLE_CURSOR_BLINK                  )
108 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "cursor-blink-interval",                FLOAT,     CURSOR_BLINK_INTERVAL                )
109 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "cursor-blink-duration",                FLOAT,     CURSOR_BLINK_DURATION                )
110 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "grab-handle-image",                    STRING,    GRAB_HANDLE_IMAGE                    )
111 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "grab-handle-pressed-image",            VECTOR4,   GRAB_HANDLE_PRESSED_IMAGE            )
112 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "scroll-threshold",                     FLOAT,     SCROLL_THRESHOLD                     )
113 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "scroll-speed",                         FLOAT,     SCROLL_SPEED                         )
114 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "selection-handle-image-left",          STRING,    SELECTION_HANDLE_IMAGE_LEFT          )
115 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "selection-handle-image-right",         STRING,    SELECTION_HANDLE_IMAGE_RIGHT         )
116 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "selection-handle-pressed-image-left",  STRING,    SELECTION_HANDLE_PRESSED_IMAGE_LEFT  )
117 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "selection-handle-pressed-image-right", STRING,    SELECTION_HANDLE_PRESSED_IMAGE_RIGHT )
118 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "selection-highlight-color",            STRING,    SELECTION_HIGHLIGHT_COLOR            )
119 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "decoration-bounding-box",              RECTANGLE, DECORATION_BOUNDING_BOX              )
120 DALI_PROPERTY_REGISTRATION( Toolkit, TextField, "input-method-settings",                MAP,       INPUT_METHOD_SETTINGS                )
121
122 DALI_SIGNAL_REGISTRATION( Toolkit, TextField, "text-changed",       SIGNAL_TEXT_CHANGED )
123 DALI_SIGNAL_REGISTRATION( Toolkit, TextField, "max-length-reached", SIGNAL_MAX_LENGTH_REACHED )
124
125 DALI_TYPE_REGISTRATION_END()
126
127 } // namespace
128
129 Toolkit::TextField TextField::New()
130 {
131   // Create the implementation, temporarily owned by this handle on stack
132   IntrusivePtr< TextField > impl = new TextField();
133
134   // Pass ownership to CustomActor handle
135   Toolkit::TextField handle( *impl );
136
137   // Second-phase init of the implementation
138   // This can only be done after the CustomActor connection has been made...
139   impl->Initialize();
140
141   return handle;
142 }
143
144 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
145 {
146   Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
147
148   if( textField )
149   {
150     TextField& impl( GetImpl( textField ) );
151
152     switch( index )
153     {
154       case Toolkit::TextField::Property::RENDERING_BACKEND:
155       {
156         int backend = value.Get< int >();
157         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p RENDERING_BACKEND %d\n", impl.mController.Get(), backend );
158
159         if( impl.mRenderingBackend != backend )
160         {
161           impl.mRenderingBackend = backend;
162           impl.mRenderer.Reset();
163           impl.RequestTextRelayout();
164         }
165         break;
166       }
167       case Toolkit::TextField::Property::TEXT:
168       {
169         if( impl.mController )
170         {
171           const std::string text = value.Get< std::string >();
172           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p TEXT %s\n", impl.mController.Get(), text.c_str() );
173
174           impl.mController->SetText( text );
175         }
176         break;
177       }
178       case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
179       {
180         if( impl.mController )
181         {
182           const std::string text = value.Get< std::string >();
183           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p PLACEHOLDER_TEXT %s\n", impl.mController.Get(), text.c_str() );
184
185           impl.mController->SetPlaceholderText( PLACEHOLDER_TYPE_INACTIVE, text );
186         }
187         break;
188       }
189       case Toolkit::TextField::Property::PLACEHOLDER_TEXT_FOCUSED:
190       {
191         if( impl.mController )
192         {
193           const std::string text = value.Get< std::string >();
194           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p PLACEHOLDER_TEXT_FOCUSED %s\n", impl.mController.Get(), text.c_str() );
195
196           impl.mController->SetPlaceholderText( PLACEHOLDER_TYPE_ACTIVE, text );
197         }
198         break;
199       }
200       case Toolkit::TextField::Property::FONT_FAMILY:
201       {
202         if( impl.mController )
203         {
204           const std::string fontFamily = value.Get< std::string >();
205           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p FONT_FAMILY %s\n", impl.mController.Get(), fontFamily.c_str() );
206
207           if( impl.mController->GetDefaultFontFamily() != fontFamily )
208           {
209             impl.mController->SetDefaultFontFamily( fontFamily );
210           }
211         }
212         break;
213       }
214       case Toolkit::TextField::Property::FONT_STYLE:
215       {
216         if( impl.mController )
217         {
218           const std::string fontStyle = value.Get< std::string >();
219           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p FONT_STYLE %s\n", impl.mController.Get(), fontStyle.c_str() );
220
221           if( impl.mController->GetDefaultFontStyle() != fontStyle )
222           {
223             impl.mController->SetDefaultFontStyle( fontStyle );
224           }
225         }
226         break;
227       }
228       case Toolkit::TextField::Property::POINT_SIZE:
229       {
230         if( impl.mController )
231         {
232           const float pointSize = value.Get< float >();
233           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p FONT_STYLE %f\n", impl.mController.Get(), pointSize );
234
235           if( !Equals( impl.mController->GetDefaultPointSize(), pointSize ) )
236           {
237             impl.mController->SetDefaultPointSize( pointSize );
238           }
239         }
240         break;
241       }
242       case Toolkit::TextField::Property::EXCEED_POLICY:
243       {
244         // TODO
245         break;
246       }
247       case Toolkit::TextField::Property::HORIZONTAL_ALIGNMENT:
248       {
249         if( impl.mController )
250         {
251           const std::string alignStr = value.Get< std::string >();
252           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p HORIZONTAL_ALIGNMENT %f\n", impl.mController.Get(), alignStr.c_str() );
253
254           const LayoutEngine::HorizontalAlignment alignment = Scripting::GetEnumeration< LayoutEngine::HorizontalAlignment >( alignStr.c_str(),
255                                                                                                                         HORIZONTAL_ALIGNMENT_STRING_TABLE,
256                                                                                                                         HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
257
258           impl.mController->SetHorizontalAlignment( alignment );
259         }
260         break;
261       }
262       case Toolkit::TextField::Property::VERTICAL_ALIGNMENT:
263       {
264         if( impl.mController )
265         {
266           const std::string alignStr = value.Get< std::string >();
267           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p VERTICAL_ALIGNMENT %f\n", impl.mController.Get(), alignStr.c_str() );
268
269           LayoutEngine::VerticalAlignment alignment = Scripting::GetEnumeration< LayoutEngine::VerticalAlignment >( alignStr.c_str(),
270                                                                                                                     VERTICAL_ALIGNMENT_STRING_TABLE,
271                                                                                                                     VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
272
273           impl.mController->SetVerticalAlignment( alignment );
274         }
275         break;
276       }
277       case Toolkit::TextField::Property::TEXT_COLOR:
278       {
279         if( impl.mController )
280         {
281           const Vector4 textColor = value.Get< Vector4 >();
282           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p TEXT_COLOR %f,%f,%f,%f\n", impl.mController.Get(), textColor.r, textColor.g, textColor.b, textColor.a );
283
284           if( impl.mController->GetTextColor() != textColor )
285           {
286             impl.mController->SetTextColor( textColor );
287             impl.mRenderer.Reset();
288           }
289         }
290         break;
291       }
292       case Toolkit::TextField::Property::PLACEHOLDER_TEXT_COLOR:
293       {
294         if( impl.mController )
295         {
296           const Vector4 textColor = value.Get< Vector4 >();
297           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p PLACEHOLDER_TEXT_COLOR %f,%f,%f,%f\n", impl.mController.Get(), textColor.r, textColor.g, textColor.b, textColor.a );
298
299           if( impl.mController->GetPlaceholderTextColor() != textColor )
300           {
301             impl.mController->SetPlaceholderTextColor( textColor );
302             impl.mRenderer.Reset();
303           }
304         }
305         break;
306       }
307       case Toolkit::TextField::Property::SHADOW_OFFSET:
308       {
309         if( impl.mController )
310         {
311           const Vector2 shadowOffset = value.Get< Vector2 >();
312           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p SHADOW_OFFSET %f,%f\n", impl.mController.Get(), shadowOffset.x, shadowOffset.y );
313
314           if ( impl.mController->GetShadowOffset() != shadowOffset )
315           {
316             impl.mController->SetShadowOffset( shadowOffset );
317             impl.mRenderer.Reset();
318           }
319         }
320         break;
321       }
322       case Toolkit::TextField::Property::SHADOW_COLOR:
323       {
324         if( impl.mController )
325         {
326           const Vector4 shadowColor = value.Get< Vector4 >();
327           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p SHADOW_COLOR %f,%f,%f,%f\n", impl.mController.Get(), shadowColor.r, shadowColor.g, shadowColor.b, shadowColor.a );
328
329           if ( impl.mController->GetShadowColor() != shadowColor )
330           {
331             impl.mController->SetShadowColor( shadowColor );
332             impl.mRenderer.Reset();
333           }
334         }
335         break;
336       }
337       case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
338       {
339         if( impl.mDecorator )
340         {
341           const Vector4 color = value.Get< Vector4 >();
342           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p PRIMARY_CURSOR_COLOR %f,%f\n", impl.mController.Get(), color.r, color.g, color.b, color.a );
343
344           impl.mDecorator->SetColor( PRIMARY_CURSOR, color );
345           impl.RequestTextRelayout();
346         }
347         break;
348       }
349       case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
350       {
351         if( impl.mDecorator )
352         {
353           const Vector4 color = value.Get< Vector4 >();
354           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p SECONDARY_CURSOR_COLOR %f,%f\n", impl.mController.Get(), color.r, color.g, color.b, color.a );
355
356           impl.mDecorator->SetColor( SECONDARY_CURSOR, color );
357           impl.RequestTextRelayout();
358         }
359         break;
360       }
361       case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
362       {
363         if( impl.mController )
364         {
365           const bool enable = value.Get< bool >();
366           DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p ENABLE_CURSOR_BLINK %d\n", impl.mController.Get(), enable );
367
368           impl.mController->SetEnableCursorBlink( enable );
369           impl.RequestTextRelayout();
370         }
371         break;
372       }
373       case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
374       {
375         if( impl.mDecorator )
376         {
377           const float interval = value.Get< float >();
378           DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p CURSOR_BLINK_INTERVAL %f\n", impl.mController.Get(), interval );
379
380           impl.mDecorator->SetCursorBlinkInterval( interval );
381         }
382         break;
383       }
384       case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
385       {
386         if( impl.mDecorator )
387         {
388           const float duration = value.Get< float >();
389           DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p CURSOR_BLINK_INTERVAL %f\n", impl.mController.Get(), duration );
390
391           impl.mDecorator->SetCursorBlinkDuration( duration );
392         }
393         break;
394       }
395       case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
396       {
397         const ResourceImage image = ResourceImage::New( value.Get< std::string >() );
398         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p GRAB_HANDLE_IMAGE %s\n", impl.mController.Get(), image.GetUrl().c_str() );
399
400         if( impl.mDecorator )
401         {
402           impl.mDecorator->SetHandleImage( GRAB_HANDLE, HANDLE_IMAGE_RELEASED, image );
403           impl.RequestTextRelayout();
404         }
405         break;
406       }
407       case Toolkit::TextField::Property::GRAB_HANDLE_PRESSED_IMAGE:
408       {
409         const ResourceImage image = ResourceImage::New( value.Get< std::string >() );
410         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p GRAB_HANDLE_PRESSED_IMAGE %s\n", impl.mController.Get(), image.GetUrl().c_str() );
411
412         if( impl.mDecorator )
413         {
414           impl.mDecorator->SetHandleImage( GRAB_HANDLE, HANDLE_IMAGE_PRESSED, image );
415           impl.RequestTextRelayout();
416         }
417         break;
418       }
419       case Toolkit::TextField::Property::SCROLL_THRESHOLD:
420       {
421         const float threshold = value.Get< float >();
422         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p SCROLL_THRESHOLD %f\n", impl.mController.Get(), threshold );
423
424         if( impl.mDecorator )
425         {
426           impl.mDecorator->SetScrollThreshold( threshold );
427         }
428         break;
429       }
430       case Toolkit::TextField::Property::SCROLL_SPEED:
431       {
432         const float speed = value.Get< float >();
433         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p SCROLL_SPEED %f\n", impl.mController.Get(), speed );
434
435         if( impl.mDecorator )
436         {
437           impl.mDecorator->SetScrollSpeed( speed );
438         }
439         break;
440       }
441       case Toolkit::TextField::Property::SELECTION_HANDLE_IMAGE_LEFT:
442       {
443         const ResourceImage image = ResourceImage::New( value.Get< std::string >() );
444         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p SELECTION_HANDLE_IMAGE_LEFT %f\n", impl.mController.Get(), image.GetUrl().c_str() );
445
446         if( impl.mDecorator )
447         {
448           impl.mDecorator->SetHandleImage( LEFT_SELECTION_HANDLE, HANDLE_IMAGE_RELEASED, image );
449           impl.RequestTextRelayout();
450         }
451         break;
452       }
453       case Toolkit::TextField::Property::SELECTION_HANDLE_IMAGE_RIGHT:
454       {
455         const ResourceImage image = ResourceImage::New( value.Get< std::string >() );
456         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p SELECTION_HANDLE_IMAGE_RIGHT %f\n", impl.mController.Get(), image.GetUrl().c_str() );
457
458         if( impl.mDecorator )
459         {
460           impl.mDecorator->SetHandleImage( RIGHT_SELECTION_HANDLE, HANDLE_IMAGE_RELEASED, image );
461           impl.RequestTextRelayout();
462         }
463         break;
464       }
465       case Toolkit::TextField::Property::SELECTION_HANDLE_PRESSED_IMAGE_LEFT:
466       {
467         const ResourceImage image = ResourceImage::New( value.Get< std::string >() );
468         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p SELECTION_HANDLE_PRESSED_IMAGE_LEFT %f\n", impl.mController.Get(), image.GetUrl().c_str() );
469
470         if( impl.mDecorator )
471         {
472           impl.mDecorator->SetHandleImage( LEFT_SELECTION_HANDLE, HANDLE_IMAGE_PRESSED, image );
473           impl.RequestTextRelayout();
474         }
475         break;
476       }
477       case Toolkit::TextField::Property::SELECTION_HANDLE_PRESSED_IMAGE_RIGHT:
478       {
479         const ResourceImage image = ResourceImage::New( value.Get< std::string >() );
480         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p SELECTION_HANDLE_PRESSED_IMAGE_RIGHT %f\n", impl.mController.Get(), image.GetUrl().c_str() );
481
482         if( impl.mDecorator )
483         {
484           impl.mDecorator->SetHandleImage( RIGHT_SELECTION_HANDLE, HANDLE_IMAGE_PRESSED, image );
485           impl.RequestTextRelayout();
486         }
487         break;
488       }
489       case Toolkit::TextField::Property::SELECTION_HIGHLIGHT_COLOR:
490       {
491         const Vector4 color = value.Get< Vector4 >();
492         DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p SELECTION_HIGHLIGHT_COLOR %f,%f\n", impl.mController.Get(), color.r, color.g, color.b, color.a );
493
494         if( impl.mDecorator )
495         {
496           impl.mDecorator->SetHighlightColor( color );
497           impl.RequestTextRelayout();
498         }
499         break;
500       }
501       case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
502       {
503         if( impl.mDecorator )
504         {
505           const Rect<int> box = value.Get< Rect<int> >();
506           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p DECORATION_BOUNDING_BOX %d,%d %dx%d\n", impl.mController.Get(), box.x, box.y, box.width, box.height );
507
508           impl.mDecorator->SetBoundingBox( box );
509           impl.RequestTextRelayout();
510         }
511         break;
512       }
513       case Toolkit::TextField::Property::MAX_LENGTH:
514       {
515         if( impl.mController )
516         {
517           const int max = value.Get< int >();
518           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p MAX_LENGTH %d\n", impl.mController.Get(), max );
519
520           impl.mController->SetMaximumNumberOfCharacters( max );
521         }
522         break;
523       }
524       case Toolkit::TextField::Property::INPUT_METHOD_SETTINGS:
525       {
526         const Property::Map map = value.Get<Property::Map>();
527         VirtualKeyboard::ApplySettings( map );
528         break;
529       }
530     } // switch
531   } // textfield
532 }
533
534 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
535 {
536   Property::Value value;
537
538   Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
539
540   if( textField )
541   {
542     TextField& impl( GetImpl( textField ) );
543
544     switch( index )
545     {
546       case Toolkit::TextField::Property::RENDERING_BACKEND:
547       {
548         value = impl.mRenderingBackend;
549         break;
550       }
551       case Toolkit::TextField::Property::TEXT:
552       {
553         if( impl.mController )
554         {
555           std::string text;
556           impl.mController->GetText( text );
557           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p returning text: %s\n", impl.mController.Get(), text.c_str() );
558           value = text;
559         }
560         break;
561       }
562       case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
563       {
564         if( impl.mController )
565         {
566           std::string text;
567           impl.mController->GetPlaceholderText( PLACEHOLDER_TYPE_INACTIVE, text );
568           value = text;
569         }
570         break;
571       }
572       case Toolkit::TextField::Property::PLACEHOLDER_TEXT_FOCUSED:
573       {
574         if( impl.mController )
575         {
576           std::string text;
577           impl.mController->GetPlaceholderText( PLACEHOLDER_TYPE_ACTIVE, text );
578           value = text;
579         }
580         break;
581       }
582       case Toolkit::TextField::Property::FONT_FAMILY:
583       {
584         if( impl.mController )
585         {
586           value = impl.mController->GetDefaultFontFamily();
587         }
588         break;
589       }
590       case Toolkit::TextField::Property::FONT_STYLE:
591       {
592         if( impl.mController )
593         {
594           value = impl.mController->GetDefaultFontStyle();
595         }
596         break;
597       }
598       case Toolkit::TextField::Property::POINT_SIZE:
599       {
600         if( impl.mController )
601         {
602           value = impl.mController->GetDefaultPointSize();
603         }
604         break;
605       }
606       case Toolkit::TextField::Property::EXCEED_POLICY:
607       {
608         value = impl.mExceedPolicy;
609         break;
610       }
611       case Toolkit::TextField::Property::HORIZONTAL_ALIGNMENT:
612       {
613         if( impl.mController )
614         {
615           value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetLayoutEngine().GetHorizontalAlignment(),
616                                                                                                                   HORIZONTAL_ALIGNMENT_STRING_TABLE,
617                                                                                                                   HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT ) );
618         }
619         break;
620       }
621       case Toolkit::TextField::Property::VERTICAL_ALIGNMENT:
622       {
623         if( impl.mController )
624         {
625           value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetLayoutEngine().GetVerticalAlignment(),
626                                                                                                                   VERTICAL_ALIGNMENT_STRING_TABLE,
627                                                                                                                   VERTICAL_ALIGNMENT_STRING_TABLE_COUNT ) );
628         }
629         break;
630       }
631       case Toolkit::TextField::Property::TEXT_COLOR:
632       {
633         if ( impl.mController )
634         {
635           value = impl.mController->GetTextColor();
636         }
637         break;
638       }
639       case Toolkit::TextField::Property::PLACEHOLDER_TEXT_COLOR:
640       {
641         if ( impl.mController )
642         {
643           value = impl.mController->GetPlaceholderTextColor();
644         }
645         break;
646       }
647       case Toolkit::TextField::Property::SHADOW_OFFSET:
648       {
649         if ( impl.mController )
650         {
651           value = impl.mController->GetShadowOffset();
652         }
653         break;
654       }
655       case Toolkit::TextField::Property::SHADOW_COLOR:
656       {
657         if ( impl.mController )
658         {
659           value = impl.mController->GetShadowColor();
660         }
661         break;
662       }
663       case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
664       {
665         if( impl.mDecorator )
666         {
667           value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
668         }
669         break;
670       }
671       case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
672       {
673         if( impl.mDecorator )
674         {
675           value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
676         }
677         break;
678       }
679       case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
680       {
681         value = impl.mController->GetEnableCursorBlink();
682         break;
683       }
684       case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
685       {
686         if( impl.mDecorator )
687         {
688           value = impl.mDecorator->GetCursorBlinkInterval();
689         }
690         break;
691       }
692       case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
693       {
694         if( impl.mDecorator )
695         {
696           value = impl.mDecorator->GetCursorBlinkDuration();
697         }
698         break;
699       }
700       case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
701       {
702         if( impl.mDecorator )
703         {
704           ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetHandleImage( GRAB_HANDLE, HANDLE_IMAGE_RELEASED ) );
705           if( image )
706           {
707             value = image.GetUrl();
708           }
709         }
710         break;
711       }
712       case Toolkit::TextField::Property::GRAB_HANDLE_PRESSED_IMAGE:
713       {
714         if( impl.mDecorator )
715         {
716           ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetHandleImage( GRAB_HANDLE, HANDLE_IMAGE_PRESSED ) );
717           if( image )
718           {
719             value = image.GetUrl();
720           }
721         }
722         break;
723       }
724       case Toolkit::TextField::Property::SCROLL_THRESHOLD:
725       {
726         if( impl.mDecorator )
727         {
728           value = impl.mDecorator->GetScrollThreshold();
729         }
730         break;
731       }
732       case Toolkit::TextField::Property::SCROLL_SPEED:
733       {
734         if( impl.mDecorator )
735         {
736           value = impl.mDecorator->GetScrollSpeed();
737         }
738         break;
739       }
740       case Toolkit::TextField::Property::SELECTION_HANDLE_IMAGE_LEFT:
741       {
742         if( impl.mDecorator )
743         {
744           ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetHandleImage( LEFT_SELECTION_HANDLE, HANDLE_IMAGE_RELEASED ) );
745           if( image )
746           {
747             value = image.GetUrl();
748           }
749         }
750         break;
751       }
752       case Toolkit::TextField::Property::SELECTION_HANDLE_IMAGE_RIGHT:
753       {
754         if( impl.mDecorator )
755         {
756           ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetHandleImage( RIGHT_SELECTION_HANDLE, HANDLE_IMAGE_RELEASED ) );
757           if( image )
758           {
759             value = image.GetUrl();
760           }
761         }
762         break;
763       }
764       case Toolkit::TextField::Property::SELECTION_HANDLE_PRESSED_IMAGE_LEFT:
765       {
766         if( impl.mDecorator )
767         {
768           ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetHandleImage( LEFT_SELECTION_HANDLE, HANDLE_IMAGE_PRESSED ) );
769           if( image )
770           {
771             value = image.GetUrl();
772           }
773         }
774         break;
775       }
776       case Toolkit::TextField::Property::SELECTION_HANDLE_PRESSED_IMAGE_RIGHT:
777       {
778         if( impl.mDecorator )
779         {
780           ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetHandleImage( RIGHT_SELECTION_HANDLE, HANDLE_IMAGE_PRESSED ) );
781           if( image )
782           {
783             value = image.GetUrl();
784           }
785         }
786         break;
787       }
788       case Toolkit::TextField::Property::SELECTION_HIGHLIGHT_COLOR:
789       {
790         if( impl.mDecorator )
791         {
792           value = impl.mDecorator->GetHighlightColor();
793         }
794         break;
795       }
796       case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
797       {
798         if( impl.mDecorator )
799         {
800           value = impl.mDecorator->GetBoundingBox();
801         }
802         break;
803       }
804       case Toolkit::TextField::Property::MAX_LENGTH:
805       {
806         if( impl.mController )
807         {
808           value = impl.mController->GetMaximumNumberOfCharacters();
809         }
810         break;
811       }
812       case Toolkit::TextField::Property::INPUT_METHOD_SETTINGS:
813       {
814         break;
815       }
816     } //switch
817   }
818
819   return value;
820 }
821
822 bool TextField::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
823 {
824   Dali::BaseHandle handle( object );
825
826   bool connected( true );
827   Toolkit::TextField field = Toolkit::TextField::DownCast( handle );
828
829   if( 0 == strcmp( signalName.c_str(), SIGNAL_TEXT_CHANGED ) )
830   {
831     field.TextChangedSignal().Connect( tracker, functor );
832   }
833   else if( 0 == strcmp( signalName.c_str(), SIGNAL_MAX_LENGTH_REACHED ) )
834   {
835     field.MaxLengthReachedSignal().Connect( tracker, functor );
836   }
837   else
838   {
839     // signalName does not match any signal
840     connected = false;
841   }
842
843   return connected;
844 }
845
846 Toolkit::TextField::TextChangedSignalType& TextField::TextChangedSignal()
847 {
848   return mTextChangedSignal;
849 }
850
851 Toolkit::TextField::MaxLengthReachedSignalType& TextField::MaxLengthReachedSignal()
852 {
853   return mMaxLengthReachedSignal;
854 }
855
856 void TextField::OnInitialize()
857 {
858   Actor self = Self();
859
860   mController = Text::Controller::New( *this );
861
862   mDecorator = Text::Decorator::New( *this, *mController );
863
864   mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
865
866   mController->EnableTextInput( mDecorator );
867
868   // Forward input events to controller
869   EnableGestureDetection(Gesture::Tap);
870   GetTapGestureDetector().SetMaximumTapsRequired( 2 );
871   EnableGestureDetection(Gesture::Pan);
872
873   self.TouchedSignal().Connect( this, &TextField::OnTouched );
874
875   // Set BoundingBox to stage size if not already set.
876   if ( mDecorator->GetBoundingBox().IsEmpty() )
877   {
878     Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
879     mDecorator->SetBoundingBox( Rect<int>( 0.0f, 0.0f, stageSize.width, stageSize.height ) );
880   }
881
882   // Fill-parent area by default
883   self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
884   self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
885 }
886
887 void TextField::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
888 {
889   GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
890 }
891
892 Vector3 TextField::GetNaturalSize()
893 {
894   return mController->GetNaturalSize();
895 }
896
897 float TextField::GetHeightForWidth( float width )
898 {
899   return mController->GetHeightForWidth( width );
900 }
901
902 void TextField::OnRelayout( const Vector2& size, RelayoutContainer& container )
903 {
904   if( mController->Relayout( size ) ||
905       !mRenderer )
906   {
907     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField::OnRelayout %p Displaying new contents\n", mController.Get() );
908
909     if( mDecorator )
910     {
911       mDecorator->Relayout( size );
912     }
913
914     if( !mRenderer )
915     {
916       mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
917     }
918
919     RenderableActor renderableActor;
920     if( mRenderer )
921     {
922       renderableActor = mRenderer->Render( mController->GetView() );
923     }
924
925     EnableClipping( (Dali::Toolkit::TextField::EXCEED_POLICY_CLIP == mExceedPolicy), size );
926
927     if( renderableActor != mRenderableActor )
928     {
929       UnparentAndReset( mRenderableActor );
930       mRenderableActor = renderableActor;
931     }
932
933     if( mRenderableActor )
934     {
935       const Vector2 offset = mController->GetScrollPosition() + mController->GetAlignmentOffset();
936
937       mRenderableActor.SetPosition( offset.x, offset.y );
938
939       // Make sure the actor is parented correctly with/without clipping
940       if( mClipper )
941       {
942         mClipper->GetRootActor().Add( mRenderableActor );
943       }
944       else
945       {
946         Self().Add( mRenderableActor );
947       }
948     }
949   }
950 }
951
952 void TextField::OnKeyInputFocusGained()
953 {
954   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField::OnKeyInputFocusGained %p\n", mController.Get() );
955
956   VirtualKeyboard::StatusChangedSignal().Connect( this, &TextField::KeyboardStatusChanged );
957
958   ImfManager imfManager = ImfManager::Get();
959
960   if ( imfManager )
961   {
962     imfManager.EventReceivedSignal().Connect( this, &TextField::OnImfEvent );
963
964     // Notify that the text editing start.
965     imfManager.Activate();
966
967     // When window gain lost focus, the imf manager is deactivated. Thus when window gain focus again, the imf manager must be activated.
968     imfManager.SetRestoreAfterFocusLost( true );
969   }
970
971   mController->KeyboardFocusGainEvent();
972
973   EmitKeyInputFocusSignal( true ); // Calls back into the Control hence done last.
974 }
975
976 void TextField::OnKeyInputFocusLost()
977 {
978   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField:OnKeyInputFocusLost %p\n", mController.Get() );
979
980   VirtualKeyboard::StatusChangedSignal().Disconnect( this, &TextField::KeyboardStatusChanged );
981
982   ImfManager imfManager = ImfManager::Get();
983   if ( imfManager )
984   {
985     // The text editing is finished. Therefore the imf manager don't have restore activation.
986     imfManager.SetRestoreAfterFocusLost( false );
987
988     // Notify that the text editing finish.
989     imfManager.Deactivate();
990
991     imfManager.EventReceivedSignal().Disconnect( this, &TextField::OnImfEvent );
992   }
993
994   mController->KeyboardFocusLostEvent();
995
996   EmitKeyInputFocusSignal( false ); // Calls back into the Control hence done last.
997 }
998
999 void TextField::OnTap( const TapGesture& gesture )
1000 {
1001   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField::OnTap %p\n", mController.Get() );
1002
1003   // Show the keyboard if it was hidden.
1004   if (!VirtualKeyboard::IsVisible())
1005   {
1006     VirtualKeyboard::Show();
1007   }
1008
1009   // Deliver the tap before the focus event to controller; this allows us to detect when focus is gained due to tap-gestures
1010   mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
1011
1012   SetKeyInputFocus();
1013 }
1014
1015 void TextField::OnPan( const PanGesture& gesture )
1016 {
1017   mController->PanEvent( gesture.state, gesture.displacement );
1018 }
1019
1020 bool TextField::OnKeyEvent( const KeyEvent& event )
1021 {
1022   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField::OnKeyEvent %p keyCode %d\n", mController.Get(), event.keyCode );
1023
1024   if( Dali::DALI_KEY_ESCAPE == event.keyCode ||
1025       "Return" == event.keyPressedName ) // Make a Dali key code for this
1026   {
1027     ClearKeyInputFocus();
1028     return true;
1029   }
1030
1031   return mController->KeyEvent( event );
1032 }
1033
1034 ImfManager::ImfCallbackData TextField::OnImfEvent( Dali::ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
1035 {
1036   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField::OnImfEvent %p eventName %d\n", mController.Get(), imfEvent.eventName );
1037   return mController->OnImfEvent( imfManager, imfEvent );
1038 }
1039
1040 void TextField::RequestTextRelayout()
1041 {
1042   RelayoutRequest();
1043 }
1044
1045 void TextField::TextChanged()
1046 {
1047   Dali::Toolkit::TextField handle( GetOwner() );
1048   mTextChangedSignal.Emit( handle );
1049 }
1050
1051 void TextField::MaxLengthReached()
1052 {
1053   Dali::Toolkit::TextField handle( GetOwner() );
1054   mMaxLengthReachedSignal.Emit( handle );
1055 }
1056
1057 void TextField::EnableClipping( bool clipping, const Vector2& size )
1058 {
1059   if( clipping )
1060   {
1061     // Not worth to created clip actor if width or height is equal to zero.
1062     if( size.width > Math::MACHINE_EPSILON_1000 && size.height > Math::MACHINE_EPSILON_1000 )
1063     {
1064       if( !mClipper )
1065       {
1066         Actor self = Self();
1067
1068         mClipper = Clipper::New( size );
1069         self.Add( mClipper->GetRootActor() );
1070         self.Add( mClipper->GetImageActor() );
1071       }
1072       else if ( mClipper )
1073       {
1074         mClipper->Refresh( size );
1075       }
1076     }
1077   }
1078   else
1079   {
1080     // Note - this will automatically remove the root & image actors
1081     mClipper.Reset();
1082   }
1083 }
1084
1085 void TextField::KeyboardStatusChanged(bool keyboardShown)
1086 {
1087   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField::KeyboardStatusChanged %p keyboardShown %d\n", mController.Get(), keyboardShown );
1088
1089   // Just hide the grab handle when keyboard is hidden.
1090   if (!keyboardShown )
1091   {
1092     mController->KeyboardFocusLostEvent();
1093   }
1094 }
1095
1096 bool TextField::OnTouched( Actor actor, const TouchEvent& event )
1097 {
1098   return true;
1099 }
1100
1101 TextField::TextField()
1102 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
1103   mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
1104   mExceedPolicy( Dali::Toolkit::TextField::EXCEED_POLICY_CLIP )
1105 {
1106 }
1107
1108 TextField::~TextField()
1109 {
1110   mClipper.Reset();
1111 }
1112
1113 } // namespace Internal
1114
1115 } // namespace Toolkit
1116
1117 } // namespace Dali