Merge "Fix for grab/selection handle." into devel/master
[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 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 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           LayoutEngine::HorizontalAlignment alignment( LayoutEngine::HORIZONTAL_ALIGN_BEGIN );
255           if( Scripting::GetEnumeration< LayoutEngine::HorizontalAlignment >( alignStr.c_str(),
256                                                                               HORIZONTAL_ALIGNMENT_STRING_TABLE,
257                                                                               HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
258                                                                               alignment ) )
259           {
260             impl.mController->SetHorizontalAlignment( alignment );
261           }
262         }
263         break;
264       }
265       case Toolkit::TextField::Property::VERTICAL_ALIGNMENT:
266       {
267         if( impl.mController )
268         {
269           const std::string alignStr = value.Get< std::string >();
270           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p VERTICAL_ALIGNMENT %f\n", impl.mController.Get(), alignStr.c_str() );
271
272           LayoutEngine::VerticalAlignment alignment( LayoutEngine::VERTICAL_ALIGN_BOTTOM );
273           if( Scripting::GetEnumeration< LayoutEngine::VerticalAlignment >( alignStr.c_str(),
274                                                                             VERTICAL_ALIGNMENT_STRING_TABLE,
275                                                                             VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
276                                                                             alignment ) )
277           {
278             impl.mController->SetVerticalAlignment( alignment );
279           }
280         }
281         break;
282       }
283       case Toolkit::TextField::Property::TEXT_COLOR:
284       {
285         if( impl.mController )
286         {
287           const Vector4 textColor = value.Get< Vector4 >();
288           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 );
289
290           if( impl.mController->GetTextColor() != textColor )
291           {
292             impl.mController->SetTextColor( textColor );
293             impl.mRenderer.Reset();
294           }
295         }
296         break;
297       }
298       case Toolkit::TextField::Property::PLACEHOLDER_TEXT_COLOR:
299       {
300         if( impl.mController )
301         {
302           const Vector4 textColor = value.Get< Vector4 >();
303           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 );
304
305           if( impl.mController->GetPlaceholderTextColor() != textColor )
306           {
307             impl.mController->SetPlaceholderTextColor( textColor );
308             impl.mRenderer.Reset();
309           }
310         }
311         break;
312       }
313       case Toolkit::TextField::Property::SHADOW_OFFSET:
314       {
315         if( impl.mController )
316         {
317           const Vector2 shadowOffset = value.Get< Vector2 >();
318           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p SHADOW_OFFSET %f,%f\n", impl.mController.Get(), shadowOffset.x, shadowOffset.y );
319
320           if ( impl.mController->GetShadowOffset() != shadowOffset )
321           {
322             impl.mController->SetShadowOffset( shadowOffset );
323             impl.mRenderer.Reset();
324           }
325         }
326         break;
327       }
328       case Toolkit::TextField::Property::SHADOW_COLOR:
329       {
330         if( impl.mController )
331         {
332           const Vector4 shadowColor = value.Get< Vector4 >();
333           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 );
334
335           if ( impl.mController->GetShadowColor() != shadowColor )
336           {
337             impl.mController->SetShadowColor( shadowColor );
338             impl.mRenderer.Reset();
339           }
340         }
341         break;
342       }
343       case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
344       {
345         if( impl.mDecorator )
346         {
347           const Vector4 color = value.Get< Vector4 >();
348           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 );
349
350           impl.mDecorator->SetColor( PRIMARY_CURSOR, color );
351           impl.RequestTextRelayout();
352         }
353         break;
354       }
355       case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
356       {
357         if( impl.mDecorator )
358         {
359           const Vector4 color = value.Get< Vector4 >();
360           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 );
361
362           impl.mDecorator->SetColor( SECONDARY_CURSOR, color );
363           impl.RequestTextRelayout();
364         }
365         break;
366       }
367       case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
368       {
369         if( impl.mController )
370         {
371           const bool enable = value.Get< bool >();
372           DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p ENABLE_CURSOR_BLINK %d\n", impl.mController.Get(), enable );
373
374           impl.mController->SetEnableCursorBlink( enable );
375           impl.RequestTextRelayout();
376         }
377         break;
378       }
379       case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
380       {
381         if( impl.mDecorator )
382         {
383           const float interval = value.Get< float >();
384           DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p CURSOR_BLINK_INTERVAL %f\n", impl.mController.Get(), interval );
385
386           impl.mDecorator->SetCursorBlinkInterval( interval );
387         }
388         break;
389       }
390       case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
391       {
392         if( impl.mDecorator )
393         {
394           const float duration = value.Get< float >();
395           DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p CURSOR_BLINK_INTERVAL %f\n", impl.mController.Get(), duration );
396
397           impl.mDecorator->SetCursorBlinkDuration( duration );
398         }
399         break;
400       }
401       case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
402       {
403         const ResourceImage image = ResourceImage::New( value.Get< std::string >() );
404         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p GRAB_HANDLE_IMAGE %s\n", impl.mController.Get(), image.GetUrl().c_str() );
405
406         if( impl.mDecorator )
407         {
408           impl.mDecorator->SetHandleImage( GRAB_HANDLE, HANDLE_IMAGE_RELEASED, image );
409           impl.RequestTextRelayout();
410         }
411         break;
412       }
413       case Toolkit::TextField::Property::GRAB_HANDLE_PRESSED_IMAGE:
414       {
415         const ResourceImage image = ResourceImage::New( value.Get< std::string >() );
416         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p GRAB_HANDLE_PRESSED_IMAGE %s\n", impl.mController.Get(), image.GetUrl().c_str() );
417
418         if( impl.mDecorator )
419         {
420           impl.mDecorator->SetHandleImage( GRAB_HANDLE, HANDLE_IMAGE_PRESSED, image );
421           impl.RequestTextRelayout();
422         }
423         break;
424       }
425       case Toolkit::TextField::Property::SCROLL_THRESHOLD:
426       {
427         const float threshold = value.Get< float >();
428         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p SCROLL_THRESHOLD %f\n", impl.mController.Get(), threshold );
429
430         if( impl.mDecorator )
431         {
432           impl.mDecorator->SetScrollThreshold( threshold );
433         }
434         break;
435       }
436       case Toolkit::TextField::Property::SCROLL_SPEED:
437       {
438         const float speed = value.Get< float >();
439         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p SCROLL_SPEED %f\n", impl.mController.Get(), speed );
440
441         if( impl.mDecorator )
442         {
443           impl.mDecorator->SetScrollSpeed( speed );
444         }
445         break;
446       }
447       case Toolkit::TextField::Property::SELECTION_HANDLE_IMAGE_LEFT:
448       {
449         const ResourceImage image = ResourceImage::New( value.Get< std::string >() );
450         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p SELECTION_HANDLE_IMAGE_LEFT %f\n", impl.mController.Get(), image.GetUrl().c_str() );
451
452         if( impl.mDecorator )
453         {
454           impl.mDecorator->SetHandleImage( LEFT_SELECTION_HANDLE, HANDLE_IMAGE_RELEASED, image );
455           impl.RequestTextRelayout();
456         }
457         break;
458       }
459       case Toolkit::TextField::Property::SELECTION_HANDLE_IMAGE_RIGHT:
460       {
461         const ResourceImage image = ResourceImage::New( value.Get< std::string >() );
462         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p SELECTION_HANDLE_IMAGE_RIGHT %f\n", impl.mController.Get(), image.GetUrl().c_str() );
463
464         if( impl.mDecorator )
465         {
466           impl.mDecorator->SetHandleImage( RIGHT_SELECTION_HANDLE, HANDLE_IMAGE_RELEASED, image );
467           impl.RequestTextRelayout();
468         }
469         break;
470       }
471       case Toolkit::TextField::Property::SELECTION_HANDLE_PRESSED_IMAGE_LEFT:
472       {
473         const ResourceImage image = ResourceImage::New( value.Get< std::string >() );
474         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p SELECTION_HANDLE_PRESSED_IMAGE_LEFT %f\n", impl.mController.Get(), image.GetUrl().c_str() );
475
476         if( impl.mDecorator )
477         {
478           impl.mDecorator->SetHandleImage( LEFT_SELECTION_HANDLE, HANDLE_IMAGE_PRESSED, image );
479           impl.RequestTextRelayout();
480         }
481         break;
482       }
483       case Toolkit::TextField::Property::SELECTION_HANDLE_PRESSED_IMAGE_RIGHT:
484       {
485         const ResourceImage image = ResourceImage::New( value.Get< std::string >() );
486         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField %p SELECTION_HANDLE_PRESSED_IMAGE_RIGHT %f\n", impl.mController.Get(), image.GetUrl().c_str() );
487
488         if( impl.mDecorator )
489         {
490           impl.mDecorator->SetHandleImage( RIGHT_SELECTION_HANDLE, HANDLE_IMAGE_PRESSED, image );
491           impl.RequestTextRelayout();
492         }
493         break;
494       }
495       case Toolkit::TextField::Property::SELECTION_HIGHLIGHT_COLOR:
496       {
497         const Vector4 color = value.Get< Vector4 >();
498         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 );
499
500         if( impl.mDecorator )
501         {
502           impl.mDecorator->SetHighlightColor( color );
503           impl.RequestTextRelayout();
504         }
505         break;
506       }
507       case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
508       {
509         if( impl.mDecorator )
510         {
511           const Rect<int> box = value.Get< Rect<int> >();
512           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 );
513
514           impl.mDecorator->SetBoundingBox( box );
515           impl.RequestTextRelayout();
516         }
517         break;
518       }
519       case Toolkit::TextField::Property::MAX_LENGTH:
520       {
521         if( impl.mController )
522         {
523           const int max = value.Get< int >();
524           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p MAX_LENGTH %d\n", impl.mController.Get(), max );
525
526           impl.mController->SetMaximumNumberOfCharacters( max );
527         }
528         break;
529       }
530       case Toolkit::TextField::Property::INPUT_METHOD_SETTINGS:
531       {
532         const Property::Map map = value.Get<Property::Map>();
533         VirtualKeyboard::ApplySettings( map );
534         break;
535       }
536     } // switch
537   } // textfield
538 }
539
540 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
541 {
542   Property::Value value;
543
544   Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
545
546   if( textField )
547   {
548     TextField& impl( GetImpl( textField ) );
549
550     switch( index )
551     {
552       case Toolkit::TextField::Property::RENDERING_BACKEND:
553       {
554         value = impl.mRenderingBackend;
555         break;
556       }
557       case Toolkit::TextField::Property::TEXT:
558       {
559         if( impl.mController )
560         {
561           std::string text;
562           impl.mController->GetText( text );
563           DALI_LOG_INFO( gLogFilter, Debug::General, "TextField %p returning text: %s\n", impl.mController.Get(), text.c_str() );
564           value = text;
565         }
566         break;
567       }
568       case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
569       {
570         if( impl.mController )
571         {
572           std::string text;
573           impl.mController->GetPlaceholderText( PLACEHOLDER_TYPE_INACTIVE, text );
574           value = text;
575         }
576         break;
577       }
578       case Toolkit::TextField::Property::PLACEHOLDER_TEXT_FOCUSED:
579       {
580         if( impl.mController )
581         {
582           std::string text;
583           impl.mController->GetPlaceholderText( PLACEHOLDER_TYPE_ACTIVE, text );
584           value = text;
585         }
586         break;
587       }
588       case Toolkit::TextField::Property::FONT_FAMILY:
589       {
590         if( impl.mController )
591         {
592           value = impl.mController->GetDefaultFontFamily();
593         }
594         break;
595       }
596       case Toolkit::TextField::Property::FONT_STYLE:
597       {
598         if( impl.mController )
599         {
600           value = impl.mController->GetDefaultFontStyle();
601         }
602         break;
603       }
604       case Toolkit::TextField::Property::POINT_SIZE:
605       {
606         if( impl.mController )
607         {
608           value = impl.mController->GetDefaultPointSize();
609         }
610         break;
611       }
612       case Toolkit::TextField::Property::EXCEED_POLICY:
613       {
614         value = impl.mExceedPolicy;
615         break;
616       }
617       case Toolkit::TextField::Property::HORIZONTAL_ALIGNMENT:
618       {
619         if( impl.mController )
620         {
621           const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetLayoutEngine().GetHorizontalAlignment(),
622                                                                                                                 HORIZONTAL_ALIGNMENT_STRING_TABLE,
623                                                                                                                 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
624           if( name )
625           {
626             value = std::string( name );
627           }
628         }
629         break;
630       }
631       case Toolkit::TextField::Property::VERTICAL_ALIGNMENT:
632       {
633         if( impl.mController )
634         {
635           const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetLayoutEngine().GetVerticalAlignment(),
636                                                                                                               VERTICAL_ALIGNMENT_STRING_TABLE,
637                                                                                                               VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
638           if( name )
639           {
640             value = std::string( name );
641           }
642         }
643         break;
644       }
645       case Toolkit::TextField::Property::TEXT_COLOR:
646       {
647         if ( impl.mController )
648         {
649           value = impl.mController->GetTextColor();
650         }
651         break;
652       }
653       case Toolkit::TextField::Property::PLACEHOLDER_TEXT_COLOR:
654       {
655         if ( impl.mController )
656         {
657           value = impl.mController->GetPlaceholderTextColor();
658         }
659         break;
660       }
661       case Toolkit::TextField::Property::SHADOW_OFFSET:
662       {
663         if ( impl.mController )
664         {
665           value = impl.mController->GetShadowOffset();
666         }
667         break;
668       }
669       case Toolkit::TextField::Property::SHADOW_COLOR:
670       {
671         if ( impl.mController )
672         {
673           value = impl.mController->GetShadowColor();
674         }
675         break;
676       }
677       case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
678       {
679         if( impl.mDecorator )
680         {
681           value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
682         }
683         break;
684       }
685       case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
686       {
687         if( impl.mDecorator )
688         {
689           value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
690         }
691         break;
692       }
693       case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
694       {
695         value = impl.mController->GetEnableCursorBlink();
696         break;
697       }
698       case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
699       {
700         if( impl.mDecorator )
701         {
702           value = impl.mDecorator->GetCursorBlinkInterval();
703         }
704         break;
705       }
706       case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
707       {
708         if( impl.mDecorator )
709         {
710           value = impl.mDecorator->GetCursorBlinkDuration();
711         }
712         break;
713       }
714       case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
715       {
716         if( impl.mDecorator )
717         {
718           ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetHandleImage( GRAB_HANDLE, HANDLE_IMAGE_RELEASED ) );
719           if( image )
720           {
721             value = image.GetUrl();
722           }
723         }
724         break;
725       }
726       case Toolkit::TextField::Property::GRAB_HANDLE_PRESSED_IMAGE:
727       {
728         if( impl.mDecorator )
729         {
730           ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetHandleImage( GRAB_HANDLE, HANDLE_IMAGE_PRESSED ) );
731           if( image )
732           {
733             value = image.GetUrl();
734           }
735         }
736         break;
737       }
738       case Toolkit::TextField::Property::SCROLL_THRESHOLD:
739       {
740         if( impl.mDecorator )
741         {
742           value = impl.mDecorator->GetScrollThreshold();
743         }
744         break;
745       }
746       case Toolkit::TextField::Property::SCROLL_SPEED:
747       {
748         if( impl.mDecorator )
749         {
750           value = impl.mDecorator->GetScrollSpeed();
751         }
752         break;
753       }
754       case Toolkit::TextField::Property::SELECTION_HANDLE_IMAGE_LEFT:
755       {
756         if( impl.mDecorator )
757         {
758           ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetHandleImage( LEFT_SELECTION_HANDLE, HANDLE_IMAGE_RELEASED ) );
759           if( image )
760           {
761             value = image.GetUrl();
762           }
763         }
764         break;
765       }
766       case Toolkit::TextField::Property::SELECTION_HANDLE_IMAGE_RIGHT:
767       {
768         if( impl.mDecorator )
769         {
770           ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetHandleImage( RIGHT_SELECTION_HANDLE, HANDLE_IMAGE_RELEASED ) );
771           if( image )
772           {
773             value = image.GetUrl();
774           }
775         }
776         break;
777       }
778       case Toolkit::TextField::Property::SELECTION_HANDLE_PRESSED_IMAGE_LEFT:
779       {
780         if( impl.mDecorator )
781         {
782           ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetHandleImage( LEFT_SELECTION_HANDLE, HANDLE_IMAGE_PRESSED ) );
783           if( image )
784           {
785             value = image.GetUrl();
786           }
787         }
788         break;
789       }
790       case Toolkit::TextField::Property::SELECTION_HANDLE_PRESSED_IMAGE_RIGHT:
791       {
792         if( impl.mDecorator )
793         {
794           ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetHandleImage( RIGHT_SELECTION_HANDLE, HANDLE_IMAGE_PRESSED ) );
795           if( image )
796           {
797             value = image.GetUrl();
798           }
799         }
800         break;
801       }
802       case Toolkit::TextField::Property::SELECTION_HIGHLIGHT_COLOR:
803       {
804         if( impl.mDecorator )
805         {
806           value = impl.mDecorator->GetHighlightColor();
807         }
808         break;
809       }
810       case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
811       {
812         if( impl.mDecorator )
813         {
814           value = impl.mDecorator->GetBoundingBox();
815         }
816         break;
817       }
818       case Toolkit::TextField::Property::MAX_LENGTH:
819       {
820         if( impl.mController )
821         {
822           value = impl.mController->GetMaximumNumberOfCharacters();
823         }
824         break;
825       }
826       case Toolkit::TextField::Property::INPUT_METHOD_SETTINGS:
827       {
828         break;
829       }
830     } //switch
831   }
832
833   return value;
834 }
835
836 bool TextField::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
837 {
838   Dali::BaseHandle handle( object );
839
840   bool connected( true );
841   Toolkit::TextField field = Toolkit::TextField::DownCast( handle );
842
843   if( 0 == strcmp( signalName.c_str(), SIGNAL_TEXT_CHANGED ) )
844   {
845     field.TextChangedSignal().Connect( tracker, functor );
846   }
847   else if( 0 == strcmp( signalName.c_str(), SIGNAL_MAX_LENGTH_REACHED ) )
848   {
849     field.MaxLengthReachedSignal().Connect( tracker, functor );
850   }
851   else
852   {
853     // signalName does not match any signal
854     connected = false;
855   }
856
857   return connected;
858 }
859
860 Toolkit::TextField::TextChangedSignalType& TextField::TextChangedSignal()
861 {
862   return mTextChangedSignal;
863 }
864
865 Toolkit::TextField::MaxLengthReachedSignalType& TextField::MaxLengthReachedSignal()
866 {
867   return mMaxLengthReachedSignal;
868 }
869
870 void TextField::OnInitialize()
871 {
872   Actor self = Self();
873
874   mController = Text::Controller::New( *this );
875
876   mDecorator = Text::Decorator::New( *mController,
877                                      *mController );
878
879   mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
880
881   mController->EnableTextInput( mDecorator );
882
883   // Forward input events to controller
884   EnableGestureDetection(Gesture::Tap);
885   GetTapGestureDetector().SetMaximumTapsRequired( 2 );
886   EnableGestureDetection(Gesture::Pan);
887
888   self.TouchedSignal().Connect( this, &TextField::OnTouched );
889
890   // Set BoundingBox to stage size if not already set.
891   if ( mDecorator->GetBoundingBox().IsEmpty() )
892   {
893     Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
894     mDecorator->SetBoundingBox( Rect<int>( 0.0f, 0.0f, stageSize.width, stageSize.height ) );
895   }
896
897   // Fill-parent area by default
898   self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
899   self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
900   self.OnStageSignal().Connect( this, &TextField::OnStageConnect );
901 }
902
903 void TextField::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
904 {
905   GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
906 }
907
908 Vector3 TextField::GetNaturalSize()
909 {
910   return mController->GetNaturalSize();
911 }
912
913 float TextField::GetHeightForWidth( float width )
914 {
915   return mController->GetHeightForWidth( width );
916 }
917
918 void TextField::OnRelayout( const Vector2& size, RelayoutContainer& container )
919 {
920   if( mController->Relayout( size ) ||
921       !mRenderer )
922   {
923     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField::OnRelayout %p Displaying new contents\n", mController.Get() );
924
925     if( mDecorator )
926     {
927       mDecorator->Relayout( size );
928     }
929
930     if( !mRenderer )
931     {
932       mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
933     }
934
935     RenderText();
936     EnableClipping( (Dali::Toolkit::TextField::EXCEED_POLICY_CLIP == mExceedPolicy), size );
937   }
938 }
939
940 void TextField::RenderText()
941 {
942   Actor renderableActor;
943   if( mRenderer )
944   {
945     renderableActor = mRenderer->Render( mController->GetView(), mDepth );
946   }
947
948   if( renderableActor != mRenderableActor )
949   {
950     UnparentAndReset( mRenderableActor );
951     mRenderableActor = renderableActor;
952   }
953
954   if( mRenderableActor )
955   {
956     const Vector2 offset = mController->GetScrollPosition() + mController->GetAlignmentOffset();
957
958     mRenderableActor.SetPosition( offset.x, offset.y );
959
960     // Make sure the actor is parented correctly with/without clipping
961     if( mClipper )
962     {
963       mClipper->GetRootActor().Add( mRenderableActor );
964     }
965     else
966     {
967       Self().Add( mRenderableActor );
968     }
969
970     for( std::vector<Actor>::const_iterator it = mClippingDecorationActors.begin(),
971            endIt = mClippingDecorationActors.end();
972          it != endIt;
973          ++it )
974     {
975       Actor actor = *it;
976
977       if( mClipper )
978       {
979         mClipper->GetRootActor().Add( actor );
980       }
981       else
982       {
983         Self().Add( actor );
984       }
985     }
986     mClippingDecorationActors.clear();
987   }
988 }
989
990 void TextField::OnKeyInputFocusGained()
991 {
992   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField::OnKeyInputFocusGained %p\n", mController.Get() );
993
994   VirtualKeyboard::StatusChangedSignal().Connect( this, &TextField::KeyboardStatusChanged );
995
996   ImfManager imfManager = ImfManager::Get();
997
998   if ( imfManager )
999   {
1000     imfManager.EventReceivedSignal().Connect( this, &TextField::OnImfEvent );
1001
1002     // Notify that the text editing start.
1003     imfManager.Activate();
1004
1005     // When window gain lost focus, the imf manager is deactivated. Thus when window gain focus again, the imf manager must be activated.
1006     imfManager.SetRestoreAfterFocusLost( true );
1007   }
1008
1009    ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
1010
1011    if ( notifier )
1012    {
1013       notifier.ContentSelectedSignal().Connect( this, &TextField::OnClipboardTextSelected );
1014    }
1015
1016   mController->KeyboardFocusGainEvent();
1017
1018   EmitKeyInputFocusSignal( true ); // Calls back into the Control hence done last.
1019 }
1020
1021 void TextField::OnKeyInputFocusLost()
1022 {
1023   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField:OnKeyInputFocusLost %p\n", mController.Get() );
1024
1025   VirtualKeyboard::StatusChangedSignal().Disconnect( this, &TextField::KeyboardStatusChanged );
1026
1027   ImfManager imfManager = ImfManager::Get();
1028   if ( imfManager )
1029   {
1030     // The text editing is finished. Therefore the imf manager don't have restore activation.
1031     imfManager.SetRestoreAfterFocusLost( false );
1032
1033     // Notify that the text editing finish.
1034     imfManager.Deactivate();
1035
1036     imfManager.EventReceivedSignal().Disconnect( this, &TextField::OnImfEvent );
1037   }
1038
1039   ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
1040
1041   if ( notifier )
1042   {
1043     notifier.ContentSelectedSignal().Disconnect( this, &TextField::OnClipboardTextSelected );
1044   }
1045
1046   mController->KeyboardFocusLostEvent();
1047
1048   EmitKeyInputFocusSignal( false ); // Calls back into the Control hence done last.
1049 }
1050
1051 void TextField::OnTap( const TapGesture& gesture )
1052 {
1053   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField::OnTap %p\n", mController.Get() );
1054
1055   // Show the keyboard if it was hidden.
1056   if (!VirtualKeyboard::IsVisible())
1057   {
1058     VirtualKeyboard::Show();
1059   }
1060
1061   // Deliver the tap before the focus event to controller; this allows us to detect when focus is gained due to tap-gestures
1062   mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
1063
1064   SetKeyInputFocus();
1065 }
1066
1067 void TextField::OnPan( const PanGesture& gesture )
1068 {
1069   mController->PanEvent( gesture.state, gesture.displacement );
1070 }
1071
1072 bool TextField::OnKeyEvent( const KeyEvent& event )
1073 {
1074   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField::OnKeyEvent %p keyCode %d\n", mController.Get(), event.keyCode );
1075
1076   if( Dali::DALI_KEY_ESCAPE == event.keyCode ||
1077       "Return" == event.keyPressedName ) // Make a Dali key code for this
1078   {
1079     ClearKeyInputFocus();
1080     return true;
1081   }
1082
1083   return mController->KeyEvent( event );
1084 }
1085
1086 void TextField::AddDecoration( Actor& actor, bool needsClipping )
1087 {
1088   if( actor )
1089   {
1090     if( needsClipping )
1091     {
1092       mClippingDecorationActors.push_back( actor );
1093     }
1094     else
1095     {
1096       Self().Add( actor );
1097     }
1098   }
1099 }
1100
1101 void TextField::RequestTextRelayout()
1102 {
1103   RelayoutRequest();
1104 }
1105
1106 void TextField::TextChanged()
1107 {
1108   Dali::Toolkit::TextField handle( GetOwner() );
1109   mTextChangedSignal.Emit( handle );
1110 }
1111
1112 void TextField::OnStageConnect( Dali::Actor actor )
1113 {
1114   if ( mHasBeenStaged )
1115   {
1116     RenderText();
1117   }
1118   else
1119   {
1120     mHasBeenStaged = true;
1121   }
1122 }
1123
1124 void TextField::MaxLengthReached()
1125 {
1126   Dali::Toolkit::TextField handle( GetOwner() );
1127   mMaxLengthReachedSignal.Emit( handle );
1128 }
1129
1130 ImfManager::ImfCallbackData TextField::OnImfEvent( Dali::ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
1131 {
1132   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField::OnImfEvent %p eventName %d\n", mController.Get(), imfEvent.eventName );
1133   return mController->OnImfEvent( imfManager, imfEvent );
1134 }
1135
1136 void TextField::EnableClipping( bool clipping, const Vector2& size )
1137 {
1138   if( clipping )
1139   {
1140     // Not worth to created clip actor if width or height is equal to zero.
1141     if( size.width > Math::MACHINE_EPSILON_1000 && size.height > Math::MACHINE_EPSILON_1000 )
1142     {
1143       if( !mClipper )
1144       {
1145         Actor self = Self();
1146
1147         mClipper = Clipper::New( size );
1148         self.Add( mClipper->GetRootActor() );
1149         self.Add( mClipper->GetImageActor() );
1150       }
1151       else if ( mClipper )
1152       {
1153         mClipper->Refresh( size );
1154       }
1155     }
1156   }
1157   else
1158   {
1159     // Note - this will automatically remove the root & image actors
1160     mClipper.Reset();
1161   }
1162 }
1163
1164 void TextField::OnClipboardTextSelected( ClipboardEventNotifier& clipboard )
1165 {
1166   mController->PasteClipboardItemEvent();
1167 }
1168
1169 void TextField::KeyboardStatusChanged(bool keyboardShown)
1170 {
1171   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextField::KeyboardStatusChanged %p keyboardShown %d\n", mController.Get(), keyboardShown );
1172
1173   // Just hide the grab handle when keyboard is hidden.
1174   if (!keyboardShown )
1175   {
1176     mController->KeyboardFocusLostEvent();
1177   }
1178   else
1179   {
1180     mController->KeyboardFocusGainEvent();
1181   }
1182 }
1183
1184 void TextField::OnStageConnection( unsigned int depth )
1185 {
1186   mDepth = depth;
1187 }
1188
1189 bool TextField::OnTouched( Actor actor, const TouchEvent& event )
1190 {
1191   return true;
1192 }
1193
1194 TextField::TextField()
1195 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
1196   mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
1197   mExceedPolicy( Dali::Toolkit::TextField::EXCEED_POLICY_CLIP ),
1198   mDepth( 0 ),
1199   mHasBeenStaged( false )
1200 {
1201 }
1202
1203 TextField::~TextField()
1204 {
1205   mClipper.Reset();
1206 }
1207
1208 } // namespace Internal
1209
1210 } // namespace Toolkit
1211
1212 } // namespace Dali