f189207ced3ba819f96a404be3090ae1ec121783
[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 <dali/public-api/adaptor-framework/key.h>
24 #include <dali/public-api/common/stage.h>
25 #include <dali/public-api/images/resource-image.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/public-api/object/type-registry-helper.h>
28 #include <dali/public-api/scripting/scripting.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/public-api/adaptor-framework/virtual-keyboard.h>
31
32 // INTERNAL INCLUDES
33 #include <dali-toolkit/public-api/text/rendering-backend.h>
34 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
35 #include <dali-toolkit/internal/text/rendering/text-backend.h>
36 #include <dali-toolkit/internal/styling/style-manager-impl.h>
37
38 using namespace Dali::Toolkit::Text;
39
40 namespace Dali
41 {
42
43 namespace Toolkit
44 {
45
46 namespace Internal
47 {
48
49 namespace
50 {
51   const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
52 }
53
54 namespace
55 {
56
57 const Scripting::StringEnum< Toolkit::Text::LayoutEngine::HorizontalAlignment > HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
58 {
59   { "BEGIN",  Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_BEGIN  },
60   { "CENTER", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_CENTER },
61   { "END",    Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_END    },
62 };
63 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
64
65 const Scripting::StringEnum< Toolkit::Text::LayoutEngine::VerticalAlignment > VERTICAL_ALIGNMENT_STRING_TABLE[] =
66 {
67   { "TOP",    Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_TOP    },
68   { "CENTER", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_CENTER },
69   { "BOTTOM", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_BOTTOM },
70 };
71 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
72
73 // Type registration
74 BaseHandle Create()
75 {
76   return Toolkit::TextField::New();
77 }
78
79 // Setup properties, signals and actions using the type-registry.
80 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextField, Toolkit::Control, Create );
81
82 DALI_PROPERTY_REGISTRATION( TextField, "rendering-backend",       INTEGER,   RENDERING_BACKEND       )
83 DALI_PROPERTY_REGISTRATION( TextField, "placeholder-text",        STRING,    PLACEHOLDER_TEXT        )
84 DALI_PROPERTY_REGISTRATION( TextField, "text",                    STRING,    TEXT                    )
85 DALI_PROPERTY_REGISTRATION( TextField, "font-family",             STRING,    FONT_FAMILY             )
86 DALI_PROPERTY_REGISTRATION( TextField, "font-style",              STRING,    FONT_STYLE              )
87 DALI_PROPERTY_REGISTRATION( TextField, "point-size",              FLOAT,     POINT_SIZE              )
88 DALI_PROPERTY_REGISTRATION( TextField, "exceed-policy",           INTEGER,   EXCEED_POLICY           )
89 DALI_PROPERTY_REGISTRATION( TextField, "primary-cursor-color",    VECTOR4,   PRIMARY_CURSOR_COLOR    )
90 DALI_PROPERTY_REGISTRATION( TextField, "secondary-cursor-color",  VECTOR4,   SECONDARY_CURSOR_COLOR  )
91 DALI_PROPERTY_REGISTRATION( TextField, "enable-cursor-blink",     BOOLEAN,   ENABLE_CURSOR_BLINK     )
92 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-interval",   FLOAT,     CURSOR_BLINK_INTERVAL   )
93 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-duration",   FLOAT,     CURSOR_BLINK_DURATION   )
94 DALI_PROPERTY_REGISTRATION( TextField, "grab-handle-image",       STRING,    GRAB_HANDLE_IMAGE       )
95 DALI_PROPERTY_REGISTRATION( TextField, "decoration-bounding-box", RECTANGLE, DECORATION_BOUNDING_BOX )
96 DALI_PROPERTY_REGISTRATION( TextField, "horizontal-alignment",    STRING,    HORIZONTAL_ALIGNMENT    )
97 DALI_PROPERTY_REGISTRATION( TextField, "vertical-alignment",      STRING,    VERTICAL_ALIGNMENT      )
98 DALI_TYPE_REGISTRATION_END()
99
100 } // namespace
101
102 Toolkit::TextField TextField::New()
103 {
104   // Create the implementation, temporarily owned by this handle on stack
105   IntrusivePtr< TextField > impl = new TextField();
106
107   // Pass ownership to CustomActor handle
108   Toolkit::TextField handle( *impl );
109
110   // Second-phase init of the implementation
111   // This can only be done after the CustomActor connection has been made...
112   impl->Initialize();
113
114   return handle;
115 }
116
117 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
118 {
119   Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
120
121   if( textField )
122   {
123     TextField& impl( GetImpl( textField ) );
124
125     switch( index )
126     {
127       case Toolkit::TextField::Property::RENDERING_BACKEND:
128       {
129         int backend = value.Get< int >();
130
131         if( impl.mRenderingBackend != backend )
132         {
133           impl.mRenderingBackend = backend;
134           impl.mRenderer.Reset();
135         }
136         break;
137       }
138       case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
139       {
140         if( impl.mController )
141         {
142           //impl.mController->SetPlaceholderText( value.Get< std::string >() ); TODO
143         }
144         break;
145       }
146       case Toolkit::TextField::Property::TEXT:
147       {
148         if( impl.mController )
149         {
150           impl.mController->SetText( value.Get< std::string >() );
151         }
152         break;
153       }
154       case Toolkit::TextField::Property::FONT_FAMILY:
155       {
156         if( impl.mController )
157         {
158           std::string fontFamily = value.Get< std::string >();
159
160           if( impl.mController->GetDefaultFontFamily() != fontFamily )
161           {
162             impl.mController->SetDefaultFontFamily( fontFamily );
163             impl.RequestTextRelayout();
164           }
165         }
166         break;
167       }
168       case Toolkit::TextField::Property::FONT_STYLE:
169       {
170         if( impl.mController )
171         {
172           std::string fontStyle = value.Get< std::string >();
173
174           if( impl.mController->GetDefaultFontStyle() != fontStyle )
175           {
176             impl.mController->SetDefaultFontStyle( fontStyle );
177             impl.RequestTextRelayout();
178           }
179         }
180         break;
181       }
182       case Toolkit::TextField::Property::POINT_SIZE:
183       {
184         if( impl.mController )
185         {
186           float pointSize = value.Get< float >();
187
188           if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
189           {
190             impl.mController->SetDefaultPointSize( pointSize );
191             impl.RequestTextRelayout();
192           }
193         }
194         break;
195       }
196       case Toolkit::TextField::Property::EXCEED_POLICY:
197       {
198         impl.mExceedPolicy = value.Get< int >();
199         break;
200       }
201       case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
202       {
203         if( impl.mDecorator )
204         {
205           impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
206         }
207         break;
208       }
209       case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
210       {
211         if( impl.mDecorator )
212         {
213           impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
214         }
215         break;
216       }
217       case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
218       {
219         if( impl.mController )
220         {
221           impl.mController->SetEnableCursorBlink( value.Get< bool >() );
222         }
223         break;
224       }
225       case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
226       {
227         if( impl.mDecorator )
228         {
229           impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
230         }
231         break;
232       }
233       case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
234       {
235         if( impl.mDecorator )
236         {
237           impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
238         }
239         break;
240       }
241       case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
242       {
243         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
244
245         if( impl.mDecorator )
246         {
247           impl.mDecorator->SetGrabHandleImage( image );
248         }
249         break;
250       }
251       case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
252       {
253         if( impl.mDecorator )
254         {
255           impl.mDecorator->SetBoundingBox( value.Get< Rect<int> >() );
256         }
257         break;
258       }
259       case Toolkit::TextField::Property::HORIZONTAL_ALIGNMENT:
260       {
261         LayoutEngine& engine = impl.mController->GetLayoutEngine();
262         const LayoutEngine::HorizontalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
263                                                                                                                                            HORIZONTAL_ALIGNMENT_STRING_TABLE,
264                                                                                                                                            HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
265
266         if( engine.GetHorizontalAlignment() != alignment )
267         {
268           engine.SetHorizontalAlignment( alignment );
269           impl.RequestTextRelayout();
270         }
271         break;
272       }
273       case Toolkit::TextField::Property::VERTICAL_ALIGNMENT:
274       {
275         LayoutEngine& engine = impl.mController->GetLayoutEngine();
276         const LayoutEngine::VerticalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
277                                                                                                                                        VERTICAL_ALIGNMENT_STRING_TABLE,
278                                                                                                                                        VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
279
280         if( engine.GetVerticalAlignment() != alignment )
281         {
282           engine.SetVerticalAlignment( alignment );
283           impl.RequestTextRelayout();
284         }
285         break;
286       }
287     } // switch
288   } // textfield
289 }
290
291 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
292 {
293   Property::Value value;
294
295   Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
296
297   if( textField )
298   {
299     TextField& impl( GetImpl( textField ) );
300
301     switch( index )
302     {
303       case Toolkit::TextField::Property::RENDERING_BACKEND:
304       {
305         value = impl.mRenderingBackend;
306         break;
307       }
308       case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
309       {
310         if( impl.mController )
311         {
312           std::string text;
313           impl.mController->GetPlaceholderText( text );
314           value = text;
315         }
316         break;
317       }
318       case Toolkit::TextField::Property::TEXT:
319       {
320         if( impl.mController )
321         {
322           std::string text;
323           impl.mController->GetText( text );
324           value = text;
325         }
326         break;
327       }
328       case Toolkit::TextField::Property::EXCEED_POLICY:
329       {
330         value = impl.mExceedPolicy;
331         break;
332       }
333       case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
334       {
335         if( impl.mDecorator )
336         {
337           value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
338         }
339         break;
340       }
341       case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
342       {
343         if( impl.mDecorator )
344         {
345           value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
346         }
347         break;
348       }
349       case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
350       {
351         value = impl.mController->GetEnableCursorBlink();
352         break;
353       }
354       case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
355       {
356         if( impl.mDecorator )
357         {
358           value = impl.mDecorator->GetCursorBlinkInterval();
359         }
360         break;
361       }
362       case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
363       {
364         if( impl.mDecorator )
365         {
366           value = impl.mDecorator->GetCursorBlinkDuration();
367         }
368         break;
369       }
370       case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
371       {
372         if( impl.mDecorator )
373         {
374           value = impl.mDecorator->GetBoundingBox();
375         }
376         break;
377       }
378       case Toolkit::TextField::Property::HORIZONTAL_ALIGNMENT:
379       {
380         if( impl.mController )
381         {
382           value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetLayoutEngine().GetHorizontalAlignment(),
383                                                                                                                   HORIZONTAL_ALIGNMENT_STRING_TABLE,
384                                                                                                                   HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT ) );
385         }
386         break;
387       }
388       case Toolkit::TextField::Property::VERTICAL_ALIGNMENT:
389       {
390         if( impl.mController )
391         {
392           value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetLayoutEngine().GetVerticalAlignment(),
393                                                                                                                   VERTICAL_ALIGNMENT_STRING_TABLE,
394                                                                                                                   VERTICAL_ALIGNMENT_STRING_TABLE_COUNT ) );
395         }
396         break;
397       }
398     } //switch
399   }
400
401   return value;
402 }
403
404 void TextField::OnInitialize()
405 {
406   Actor self = Self();
407
408   mController = Text::Controller::New( *this );
409
410   mDecorator = Text::Decorator::New( *this, *mController );
411
412   mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
413
414   mController->EnableTextInput( mDecorator );
415
416   // Forward input events to controller
417   EnableGestureDetection(Gesture::Tap);
418   GetTapGestureDetector().SetMaximumTapsRequired( 2 );
419   EnableGestureDetection(Gesture::Pan);
420
421   // Set BoundingBox to stage size if not already set.
422   if ( mDecorator->GetBoundingBox().IsEmpty() )
423   {
424     Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
425     mDecorator->SetBoundingBox( Rect<int>( 0.0f, 0.0f, stageSize.width, stageSize.height ) );
426   }
427
428   // Fill-parent area by default
429   self.SetResizePolicy( FILL_TO_PARENT, WIDTH );
430   self.SetResizePolicy( FILL_TO_PARENT, HEIGHT );
431 }
432
433 void TextField::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
434 {
435   GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
436 }
437
438 Vector3 TextField::GetNaturalSize()
439 {
440   return mController->GetNaturalSize();
441 }
442
443 float TextField::GetHeightForWidth( float width )
444 {
445   return mController->GetHeightForWidth( width );
446 }
447
448 void TextField::OnRelayout( const Vector2& size, RelayoutContainer& container )
449 {
450   if( mController->Relayout( size ) ||
451       !mRenderer )
452   {
453     const Vector2& scrollPosition = mController->GetScrollPosition();
454     const Vector2& alignmentOffset = mController->GetAlignmentOffset();
455
456     Vector2 offset = scrollPosition + alignmentOffset;
457
458     if( mDecorator )
459     {
460       mDecorator->Relayout( size, offset );
461     }
462
463     if( !mRenderer )
464     {
465       mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
466     }
467
468     RenderableActor renderableActor;
469     if( mRenderer )
470     {
471       renderableActor = mRenderer->Render( mController->GetView() );
472     }
473
474     EnableClipping( (Dali::Toolkit::TextField::EXCEED_POLICY_CLIP == mExceedPolicy), size );
475
476     if( renderableActor != mRenderableActor )
477     {
478       UnparentAndReset( mRenderableActor );
479       mRenderableActor = renderableActor;
480     }
481
482     if( mRenderableActor )
483     {
484       mRenderableActor.SetPosition( offset.x, offset.y );
485
486       // Make sure the actor is parented correctly with/without clipping
487       if( mClipper )
488       {
489         mClipper->GetRootActor().Add( mRenderableActor );
490       }
491       else
492       {
493         Self().Add( mRenderableActor );
494       }
495     }
496   }
497 }
498
499 void TextField::OnKeyInputFocusGained()
500 {
501   VirtualKeyboard::StatusChangedSignal().Connect( this, &TextField::KeyboardStatusChanged );
502
503   ImfManager imfManager = ImfManager::Get();
504
505   if ( imfManager )
506   {
507     imfManager.EventReceivedSignal().Connect( this, &TextField::OnImfEvent );
508
509     // Notify that the text editing start.
510     imfManager.Activate();
511
512     // When window gain lost focus, the imf manager is deactivated. Thus when window gain focus again, the imf manager must be activated.
513     imfManager.SetRestoreAfterFocusLost( true );
514   }
515
516   mController->KeyboardFocusGainEvent();
517 }
518
519 void TextField::OnKeyInputFocusLost()
520 {
521   VirtualKeyboard::StatusChangedSignal().Disconnect( this, &TextField::KeyboardStatusChanged );
522
523   ImfManager imfManager = ImfManager::Get();
524   if ( imfManager )
525   {
526     // The text editing is finished. Therefore the imf manager don't have restore activation.
527     imfManager.SetRestoreAfterFocusLost( false );
528
529     // Notify that the text editing finish.
530     imfManager.Deactivate();
531
532     imfManager.EventReceivedSignal().Disconnect( this, &TextField::OnImfEvent );
533   }
534
535   mController->KeyboardFocusLostEvent();
536 }
537
538 void TextField::OnTap( const TapGesture& gesture )
539 {
540   // Show the keyboard if it was hidden.
541   if (!VirtualKeyboard::IsVisible())
542   {
543     VirtualKeyboard::Show();
544   }
545
546   SetKeyInputFocus();
547
548   mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
549 }
550
551 void TextField::OnPan( const PanGesture& gesture )
552 {
553   mController->PanEvent( gesture.state, gesture.displacement );
554 }
555
556 bool TextField::OnKeyEvent( const KeyEvent& event )
557 {
558   if( Dali::DALI_KEY_ESCAPE == event.keyCode )
559   {
560     ClearKeyInputFocus();
561   }
562
563   return mController->KeyEvent( event );
564 }
565
566 ImfManager::ImfCallbackData TextField::OnImfEvent( Dali::ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
567 {
568   switch ( imfEvent.eventName )
569   {
570     case ImfManager::COMMIT:
571     {
572       KeyEvent event( "", imfEvent.predictiveString, 0, 0, 0, KeyEvent::Down );
573       mController->KeyEvent( event );
574       break;
575     }
576     case ImfManager::PREEDIT: // fall through
577     case ImfManager::DELETESURROUNDING:
578     case ImfManager::GETSURROUNDING:
579     case ImfManager::VOID:
580     {
581       // do nothing
582     }
583   } // end switch
584
585   return ImfManager::ImfCallbackData();
586 }
587
588 void TextField::RequestTextRelayout()
589 {
590   RelayoutRequest();
591 }
592
593 void TextField::EnableClipping( bool clipping, const Vector2& size )
594 {
595   if( clipping )
596   {
597     // Not worth to created clip actor if width or height is equal to zero.
598     if( size.width > Math::MACHINE_EPSILON_1000 && size.height > Math::MACHINE_EPSILON_1000 )
599     {
600       if( !mClipper )
601       {
602         Actor self = Self();
603
604         mClipper = Clipper::New( size );
605         self.Add( mClipper->GetRootActor() );
606         self.Add( mClipper->GetImageActor() );
607       }
608       else if ( mClipper )
609       {
610         mClipper->Refresh( size );
611       }
612     }
613   }
614   else
615   {
616     // Note - this will automatically remove the root & image actors
617     mClipper.Reset();
618   }
619 }
620
621 void TextField::KeyboardStatusChanged(bool keyboardShown)
622 {
623   // Just hide the grab handle when keyboard is hidden.
624   if (!keyboardShown )
625   {
626     mController->KeyboardFocusLostEvent();
627   }
628 }
629
630 TextField::TextField()
631 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
632   mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
633   mExceedPolicy( Dali::Toolkit::TextField::EXCEED_POLICY_CLIP )
634 {
635 }
636
637 TextField::~TextField()
638 {
639   mClipper.Reset();
640 }
641
642 } // namespace Internal
643
644 } // namespace Toolkit
645
646 } // namespace Dali