Merge "Text Decorator - Set positions in Control coords." into 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     if( mDecorator )
455     {
456       mDecorator->Relayout( size );
457     }
458
459     if( !mRenderer )
460     {
461       mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
462     }
463
464     RenderableActor renderableActor;
465     if( mRenderer )
466     {
467       renderableActor = mRenderer->Render( mController->GetView() );
468     }
469
470     EnableClipping( (Dali::Toolkit::TextField::EXCEED_POLICY_CLIP == mExceedPolicy), size );
471
472     if( renderableActor != mRenderableActor )
473     {
474       UnparentAndReset( mRenderableActor );
475       mRenderableActor = renderableActor;
476     }
477
478     if( mRenderableActor )
479     {
480       const Vector2 offset = mController->GetScrollPosition() + mController->GetAlignmentOffset();
481
482       mRenderableActor.SetPosition( offset.x, offset.y );
483
484       // Make sure the actor is parented correctly with/without clipping
485       if( mClipper )
486       {
487         mClipper->GetRootActor().Add( mRenderableActor );
488       }
489       else
490       {
491         Self().Add( mRenderableActor );
492       }
493     }
494   }
495 }
496
497 void TextField::OnKeyInputFocusGained()
498 {
499   VirtualKeyboard::StatusChangedSignal().Connect( this, &TextField::KeyboardStatusChanged );
500
501   ImfManager imfManager = ImfManager::Get();
502
503   if ( imfManager )
504   {
505     imfManager.EventReceivedSignal().Connect( this, &TextField::OnImfEvent );
506
507     // Notify that the text editing start.
508     imfManager.Activate();
509
510     // When window gain lost focus, the imf manager is deactivated. Thus when window gain focus again, the imf manager must be activated.
511     imfManager.SetRestoreAfterFocusLost( true );
512   }
513
514   mController->KeyboardFocusGainEvent();
515
516   EmitKeyInputFocusSignal( true ); // Calls back into the Control hence done last.
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   EmitKeyInputFocusSignal( false ); // Calls back into the Control hence done last.
538 }
539
540 void TextField::OnTap( const TapGesture& gesture )
541 {
542   // Show the keyboard if it was hidden.
543   if (!VirtualKeyboard::IsVisible())
544   {
545     VirtualKeyboard::Show();
546   }
547
548   SetKeyInputFocus();
549
550   mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
551 }
552
553 void TextField::OnPan( const PanGesture& gesture )
554 {
555   mController->PanEvent( gesture.state, gesture.displacement );
556 }
557
558 bool TextField::OnKeyEvent( const KeyEvent& event )
559 {
560   if( Dali::DALI_KEY_ESCAPE == event.keyCode )
561   {
562     ClearKeyInputFocus();
563   }
564
565   return mController->KeyEvent( event );
566 }
567
568 ImfManager::ImfCallbackData TextField::OnImfEvent( Dali::ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
569 {
570   switch ( imfEvent.eventName )
571   {
572     case ImfManager::COMMIT:
573     {
574       KeyEvent event( "", imfEvent.predictiveString, 0, 0, 0, KeyEvent::Down );
575       mController->KeyEvent( event );
576       break;
577     }
578     case ImfManager::PREEDIT: // fall through
579     case ImfManager::DELETESURROUNDING:
580     case ImfManager::GETSURROUNDING:
581     case ImfManager::VOID:
582     {
583       // do nothing
584     }
585   } // end switch
586
587   return ImfManager::ImfCallbackData();
588 }
589
590 void TextField::RequestTextRelayout()
591 {
592   RelayoutRequest();
593 }
594
595 void TextField::EnableClipping( bool clipping, const Vector2& size )
596 {
597   if( clipping )
598   {
599     // Not worth to created clip actor if width or height is equal to zero.
600     if( size.width > Math::MACHINE_EPSILON_1000 && size.height > Math::MACHINE_EPSILON_1000 )
601     {
602       if( !mClipper )
603       {
604         Actor self = Self();
605
606         mClipper = Clipper::New( size );
607         self.Add( mClipper->GetRootActor() );
608         self.Add( mClipper->GetImageActor() );
609       }
610       else if ( mClipper )
611       {
612         mClipper->Refresh( size );
613       }
614     }
615   }
616   else
617   {
618     // Note - this will automatically remove the root & image actors
619     mClipper.Reset();
620   }
621 }
622
623 void TextField::KeyboardStatusChanged(bool keyboardShown)
624 {
625   // Just hide the grab handle when keyboard is hidden.
626   if (!keyboardShown )
627   {
628     mController->KeyboardFocusLostEvent();
629   }
630 }
631
632 TextField::TextField()
633 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
634   mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
635   mExceedPolicy( Dali::Toolkit::TextField::EXCEED_POLICY_CLIP )
636 {
637 }
638
639 TextField::~TextField()
640 {
641   mClipper.Reset();
642 }
643
644 } // namespace Internal
645
646 } // namespace Toolkit
647
648 } // namespace Dali