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