Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-field-impl.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/text-controls/text-field-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <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::Alignment > ALIGNMENT_STRING_TABLE[] =
58 {
59   { "BEGIN",  Toolkit::Text::LayoutEngine::ALIGN_BEGIN  },
60   { "CENTER", Toolkit::Text::LayoutEngine::ALIGN_CENTER },
61   { "END",    Toolkit::Text::LayoutEngine::ALIGN_END    },
62 };
63 const unsigned int ALIGNMENT_STRING_TABLE_COUNT = sizeof( ALIGNMENT_STRING_TABLE ) / sizeof( ALIGNMENT_STRING_TABLE[0] );
64
65 // Type registration
66 BaseHandle Create()
67 {
68   return Toolkit::TextField::New();
69 }
70
71 // Setup properties, signals and actions using the type-registry.
72 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextField, Toolkit::Control, Create );
73
74 DALI_PROPERTY_REGISTRATION( TextField, "rendering-backend",       INTEGER,   RENDERING_BACKEND       )
75 DALI_PROPERTY_REGISTRATION( TextField, "placeholder-text",        STRING,    PLACEHOLDER_TEXT        )
76 DALI_PROPERTY_REGISTRATION( TextField, "text",                    STRING,    TEXT                    )
77 DALI_PROPERTY_REGISTRATION( TextField, "font-family",             STRING,    FONT_FAMILY             )
78 DALI_PROPERTY_REGISTRATION( TextField, "font-style",              STRING,    FONT_STYLE              )
79 DALI_PROPERTY_REGISTRATION( TextField, "point-size",              FLOAT,     POINT_SIZE              )
80 DALI_PROPERTY_REGISTRATION( TextField, "exceed-policy",           INTEGER,   EXCEED_POLICY           )
81 DALI_PROPERTY_REGISTRATION( TextField, "primary-cursor-color",    VECTOR4,   PRIMARY_CURSOR_COLOR    )
82 DALI_PROPERTY_REGISTRATION( TextField, "secondary-cursor-color",  VECTOR4,   SECONDARY_CURSOR_COLOR  )
83 DALI_PROPERTY_REGISTRATION( TextField, "enable-cursor-blink",     BOOLEAN,   ENABLE_CURSOR_BLINK     )
84 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-interval",   FLOAT,     CURSOR_BLINK_INTERVAL   )
85 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-duration",   FLOAT,     CURSOR_BLINK_DURATION   )
86 DALI_PROPERTY_REGISTRATION( TextField, "grab-handle-image",       STRING,    GRAB_HANDLE_IMAGE       )
87 DALI_PROPERTY_REGISTRATION( TextField, "decoration bounding-box", RECTANGLE, DECORATION_BOUNDING_BOX )
88 DALI_PROPERTY_REGISTRATION( TextField, "alignment",               STRING,    ALIGNMENT               )
89
90 DALI_TYPE_REGISTRATION_END()
91
92 } // namespace
93
94 Toolkit::TextField TextField::New()
95 {
96   // Create the implementation, temporarily owned by this handle on stack
97   IntrusivePtr< TextField > impl = new TextField();
98
99   // Pass ownership to CustomActor handle
100   Toolkit::TextField handle( *impl );
101
102   // Second-phase init of the implementation
103   // This can only be done after the CustomActor connection has been made...
104   impl->Initialize();
105
106   return handle;
107 }
108
109 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
110 {
111   Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
112
113   if( textField )
114   {
115     TextField& impl( GetImpl( textField ) );
116
117     switch( index )
118     {
119       case Toolkit::TextField::Property::RENDERING_BACKEND:
120       {
121         int backend = value.Get< int >();
122
123         if( impl.mRenderingBackend != backend )
124         {
125           impl.mRenderingBackend = backend;
126           impl.mRenderer.Reset();
127         }
128         break;
129       }
130       case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
131       {
132         if( impl.mController )
133         {
134           //impl.mController->SetPlaceholderText( value.Get< std::string >() ); TODO
135         }
136         break;
137       }
138       case Toolkit::TextField::Property::TEXT:
139       {
140         if( impl.mController )
141         {
142           impl.mController->SetText( value.Get< std::string >() );
143         }
144         break;
145       }
146       case Toolkit::TextField::Property::FONT_FAMILY:
147       {
148         if( impl.mController )
149         {
150           std::string fontFamily = value.Get< std::string >();
151
152           if( impl.mController->GetDefaultFontFamily() != fontFamily )
153           {
154             impl.mController->SetDefaultFontFamily( fontFamily );
155             impl.RequestTextRelayout();
156           }
157         }
158         break;
159       }
160       case Toolkit::TextField::Property::FONT_STYLE:
161       {
162         if( impl.mController )
163         {
164           std::string fontStyle = value.Get< std::string >();
165
166           if( impl.mController->GetDefaultFontStyle() != fontStyle )
167           {
168             impl.mController->SetDefaultFontStyle( fontStyle );
169             impl.RequestTextRelayout();
170           }
171         }
172         break;
173       }
174       case Toolkit::TextField::Property::POINT_SIZE:
175       {
176         if( impl.mController )
177         {
178           float pointSize = value.Get< float >();
179
180           if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
181           {
182             impl.mController->SetDefaultPointSize( pointSize );
183             impl.RequestTextRelayout();
184           }
185         }
186         break;
187       }
188       case Toolkit::TextField::Property::EXCEED_POLICY:
189       {
190         impl.mExceedPolicy = value.Get< int >();
191         break;
192       }
193       case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
194       {
195         if( impl.mDecorator )
196         {
197           impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
198         }
199         break;
200       }
201       case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
202       {
203         if( impl.mDecorator )
204         {
205           impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
206         }
207         break;
208       }
209       case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
210       {
211         if( impl.mController )
212         {
213           impl.mController->SetEnableCursorBlink( value.Get< bool >() );
214         }
215         break;
216       }
217       case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
218       {
219         if( impl.mDecorator )
220         {
221           impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
222         }
223         break;
224       }
225       case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
226       {
227         if( impl.mDecorator )
228         {
229           impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
230         }
231         break;
232       }
233       case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
234       {
235         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
236
237         if( impl.mDecorator )
238         {
239           impl.mDecorator->SetGrabHandleImage( image );
240         }
241         break;
242       }
243       case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
244       {
245         if( impl.mDecorator )
246         {
247           impl.mDecorator->SetBoundingBox( value.Get< Rect<int> >() );
248         }
249         break;
250       }
251       case Toolkit::TextField::Property::ALIGNMENT:
252       {
253         LayoutEngine& engine = impl.mController->GetLayoutEngine();
254         const LayoutEngine::Alignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::Alignment >( value.Get< std::string >().c_str(),
255                                                                                                                        ALIGNMENT_STRING_TABLE,
256                                                                                                                        ALIGNMENT_STRING_TABLE_COUNT );
257
258         if( engine.GetAlignment() != alignment )
259         {
260           engine.SetAlignment( alignment );
261           impl.RequestTextRelayout();
262         }
263         break;
264       }
265     }
266   }
267 }
268
269 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
270 {
271   Property::Value value;
272
273   Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
274
275   if( textField )
276   {
277     TextField& impl( GetImpl( textField ) );
278
279     switch( index )
280     {
281       case Toolkit::TextField::Property::RENDERING_BACKEND:
282       {
283         value = impl.mRenderingBackend;
284         break;
285       }
286       case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
287       {
288         if( impl.mController )
289         {
290           std::string text;
291           impl.mController->GetPlaceholderText( text );
292           value = text;
293         }
294         break;
295       }
296       case Toolkit::TextField::Property::TEXT:
297       {
298         if( impl.mController )
299         {
300           std::string text;
301           impl.mController->GetText( text );
302           value = text;
303         }
304         break;
305       }
306       case Toolkit::TextField::Property::EXCEED_POLICY:
307       {
308         value = impl.mExceedPolicy;
309         break;
310       }
311       case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
312       {
313         if( impl.mDecorator )
314         {
315           value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
316         }
317         break;
318       }
319       case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
320       {
321         if( impl.mDecorator )
322         {
323           value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
324         }
325         break;
326       }
327       case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
328       {
329         value = impl.mController->GetEnableCursorBlink();
330         break;
331       }
332       case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
333       {
334         if( impl.mDecorator )
335         {
336           value = impl.mDecorator->GetCursorBlinkInterval();
337         }
338         break;
339       }
340       case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
341       {
342         if( impl.mDecorator )
343         {
344           value = impl.mDecorator->GetCursorBlinkDuration();
345         }
346         break;
347       }
348       case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
349       {
350         if( impl.mDecorator )
351         {
352           value = impl.mDecorator->GetBoundingBox();
353         }
354         break;
355       }
356       case Toolkit::TextField::Property::ALIGNMENT:
357       {
358         if( impl.mController )
359         {
360           value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::Alignment >( impl.mController->GetLayoutEngine().GetAlignment(),
361                                                                                                         ALIGNMENT_STRING_TABLE,
362                                                                                                         ALIGNMENT_STRING_TABLE_COUNT ) );
363         }
364         break;
365       }
366     }
367   }
368
369   return value;
370 }
371
372 void TextField::OnInitialize()
373 {
374   mController = Text::Controller::New( *this );
375
376   mDecorator = Text::Decorator::New( *this, *mController );
377
378   mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
379
380   mController->EnableTextInput( mDecorator );
381
382   // Forward input events to controller
383   EnableGestureDetection(Gesture::Tap);
384   GetTapGestureDetector().SetMaximumTapsRequired( 2 );
385   EnableGestureDetection(Gesture::Pan);
386
387   // Set BoundingBox to stage size if not already set.
388   if ( mDecorator->GetBoundingBox().IsEmpty() )
389   {
390     Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
391     mDecorator->SetBoundingBox( Rect<int>( 0.0f, 0.0f, stageSize.width, stageSize.height ) );
392   }
393 }
394
395 void TextField::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
396 {
397   GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
398 }
399
400 Vector3 TextField::GetNaturalSize()
401 {
402   return mController->GetNaturalSize();
403 }
404
405 float TextField::GetHeightForWidth( float width )
406 {
407   return mController->GetHeightForWidth( width );
408 }
409
410 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
411 {
412   if( mController->Relayout( size ) ||
413       !mRenderer )
414   {
415     if( mDecorator )
416     {
417       mDecorator->Relayout( size, mController->GetScrollPosition() );
418     }
419
420     if( !mRenderer )
421     {
422       mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
423     }
424
425     RenderableActor renderableActor;
426     if( mRenderer )
427     {
428       renderableActor = mRenderer->Render( mController->GetView() );
429     }
430
431     EnableClipping( (Dali::Toolkit::TextField::EXCEED_POLICY_CLIP == mExceedPolicy), size );
432
433     if( renderableActor != mRenderableActor )
434     {
435       UnparentAndReset( mRenderableActor );
436       mRenderableActor = renderableActor;
437     }
438
439     if( mRenderableActor )
440     {
441       const Vector2& scrollPosition = mController->GetScrollPosition();
442       mRenderableActor.SetPosition( scrollPosition.x, scrollPosition.y );
443
444       // Make sure the actor is parented correctly with/without clipping
445       if( mClipper )
446       {
447         mClipper->GetRootActor().Add( mRenderableActor );
448       }
449       else
450       {
451         Self().Add( mRenderableActor );
452       }
453     }
454   }
455 }
456
457 void TextField::OnKeyInputFocusGained()
458 {
459   VirtualKeyboard::StatusChangedSignal().Connect( this, &TextField::KeyboardStatusChanged );
460
461   ImfManager imfManager = ImfManager::Get();
462
463   if ( imfManager )
464   {
465     imfManager.EventReceivedSignal().Connect( this, &TextField::OnImfEvent );
466
467     // Notify that the text editing start.
468     imfManager.Activate();
469
470     // When window gain lost focus, the imf manager is deactivated. Thus when window gain focus again, the imf manager must be activated.
471     imfManager.SetRestoreAfterFocusLost( true );
472   }
473
474   mController->KeyboardFocusGainEvent();
475 }
476
477 void TextField::OnKeyInputFocusLost()
478 {
479   VirtualKeyboard::StatusChangedSignal().Disconnect( this, &TextField::KeyboardStatusChanged );
480
481   ImfManager imfManager = ImfManager::Get();
482   if ( imfManager )
483   {
484     // The text editing is finished. Therefore the imf manager don't have restore activation.
485     imfManager.SetRestoreAfterFocusLost( false );
486
487     // Notify that the text editing finish.
488     imfManager.Deactivate();
489
490     imfManager.EventReceivedSignal().Disconnect( this, &TextField::OnImfEvent );
491   }
492
493   mController->KeyboardFocusLostEvent();
494 }
495
496 void TextField::OnTap( const TapGesture& gesture )
497 {
498   // Show the keyboard if it was hidden.
499   if (!VirtualKeyboard::IsVisible())
500   {
501     VirtualKeyboard::Show();
502   }
503
504   SetKeyInputFocus();
505
506   mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
507 }
508
509 void TextField::OnPan( const PanGesture& gesture )
510 {
511   mController->PanEvent( gesture.state, gesture.displacement );
512 }
513
514 bool TextField::OnKeyEvent( const KeyEvent& event )
515 {
516   if( Dali::DALI_KEY_ESCAPE == event.keyCode )
517   {
518     ClearKeyInputFocus();
519   }
520
521   return mController->KeyEvent( event );
522 }
523
524 ImfManager::ImfCallbackData TextField::OnImfEvent( Dali::ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
525 {
526   switch ( imfEvent.eventName )
527   {
528     case ImfManager::COMMIT:
529     {
530       KeyEvent event( "", imfEvent.predictiveString, 0, 0, 0, KeyEvent::Down );
531       mController->KeyEvent( event );
532       break;
533     }
534     case ImfManager::PREEDIT: // fall through
535     case ImfManager::DELETESURROUNDING:
536     case ImfManager::GETSURROUNDING:
537     case ImfManager::VOID:
538     {
539       // do nothing
540     }
541   } // end switch
542
543   return ImfManager::ImfCallbackData();
544 }
545
546 void TextField::RequestTextRelayout()
547 {
548   RelayoutRequest();
549 }
550
551 void TextField::EnableClipping( bool clipping, const Vector2& size )
552 {
553   if( clipping )
554   {
555     // Not worth to created clip actor if width or height is equal to zero.
556     if( size.width > Math::MACHINE_EPSILON_1000 && size.height > Math::MACHINE_EPSILON_1000 )
557     {
558       if( !mClipper )
559       {
560         Actor self = Self();
561
562         mClipper = Clipper::New( size );
563         self.Add( mClipper->GetRootActor() );
564         self.Add( mClipper->GetImageActor() );
565       }
566       else if ( mClipper )
567       {
568         mClipper->Refresh( size );
569       }
570     }
571   }
572   else
573   {
574     // Note - this will automatically remove the root & image actors
575     mClipper.Reset();
576   }
577 }
578
579 void TextField::KeyboardStatusChanged(bool keyboardShown)
580 {
581   // Just hide the grab handle when keyboard is hidden.
582   if (!keyboardShown )
583   {
584     mController->KeyboardFocusLostEvent();
585   }
586 }
587
588 TextField::TextField()
589 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
590   mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
591   mExceedPolicy( Dali::Toolkit::TextField::EXCEED_POLICY_CLIP )
592 {
593 }
594
595 TextField::~TextField()
596 {
597   mClipper.Reset();
598 }
599
600 } // namespace Internal
601
602 } // namespace Toolkit
603
604 } // namespace Dali