Added Drop Shadow properties and initial effect with Text Atlas Renderer
[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   mController = Text::Controller::New( *this );
407
408   mDecorator = Text::Decorator::New( *this, *mController );
409
410   mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
411
412   mController->EnableTextInput( mDecorator );
413
414   // Forward input events to controller
415   EnableGestureDetection(Gesture::Tap);
416   GetTapGestureDetector().SetMaximumTapsRequired( 2 );
417   EnableGestureDetection(Gesture::Pan);
418
419   // Set BoundingBox to stage size if not already set.
420   if ( mDecorator->GetBoundingBox().IsEmpty() )
421   {
422     Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
423     mDecorator->SetBoundingBox( Rect<int>( 0.0f, 0.0f, stageSize.width, stageSize.height ) );
424   }
425 }
426
427 void TextField::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
428 {
429   GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
430 }
431
432 Vector3 TextField::GetNaturalSize()
433 {
434   return mController->GetNaturalSize();
435 }
436
437 float TextField::GetHeightForWidth( float width )
438 {
439   return mController->GetHeightForWidth( width );
440 }
441
442 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
443 {
444   if( mController->Relayout( size ) ||
445       !mRenderer )
446   {
447     const Vector2& scrollPosition = mController->GetScrollPosition();
448     const Vector2& alignmentOffset = mController->GetAlignmentOffset();
449
450     Vector2 offset = scrollPosition + alignmentOffset;
451
452     if( mDecorator )
453     {
454       mDecorator->Relayout( size, offset );
455     }
456
457     if( !mRenderer )
458     {
459       mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
460     }
461
462     RenderableActor renderableActor;
463     if( mRenderer )
464     {
465       renderableActor = mRenderer->Render( mController->GetView() );
466     }
467
468     EnableClipping( (Dali::Toolkit::TextField::EXCEED_POLICY_CLIP == mExceedPolicy), size );
469
470     if( renderableActor != mRenderableActor )
471     {
472       UnparentAndReset( mRenderableActor );
473       mRenderableActor = renderableActor;
474     }
475
476     if( mRenderableActor )
477     {
478       mRenderableActor.SetPosition( offset.x, offset.y );
479
480       // Make sure the actor is parented correctly with/without clipping
481       if( mClipper )
482       {
483         mClipper->GetRootActor().Add( mRenderableActor );
484       }
485       else
486       {
487         Self().Add( mRenderableActor );
488       }
489     }
490   }
491 }
492
493 void TextField::OnKeyInputFocusGained()
494 {
495   VirtualKeyboard::StatusChangedSignal().Connect( this, &TextField::KeyboardStatusChanged );
496
497   ImfManager imfManager = ImfManager::Get();
498
499   if ( imfManager )
500   {
501     imfManager.EventReceivedSignal().Connect( this, &TextField::OnImfEvent );
502
503     // Notify that the text editing start.
504     imfManager.Activate();
505
506     // When window gain lost focus, the imf manager is deactivated. Thus when window gain focus again, the imf manager must be activated.
507     imfManager.SetRestoreAfterFocusLost( true );
508   }
509
510   mController->KeyboardFocusGainEvent();
511 }
512
513 void TextField::OnKeyInputFocusLost()
514 {
515   VirtualKeyboard::StatusChangedSignal().Disconnect( this, &TextField::KeyboardStatusChanged );
516
517   ImfManager imfManager = ImfManager::Get();
518   if ( imfManager )
519   {
520     // The text editing is finished. Therefore the imf manager don't have restore activation.
521     imfManager.SetRestoreAfterFocusLost( false );
522
523     // Notify that the text editing finish.
524     imfManager.Deactivate();
525
526     imfManager.EventReceivedSignal().Disconnect( this, &TextField::OnImfEvent );
527   }
528
529   mController->KeyboardFocusLostEvent();
530 }
531
532 void TextField::OnTap( const TapGesture& gesture )
533 {
534   // Show the keyboard if it was hidden.
535   if (!VirtualKeyboard::IsVisible())
536   {
537     VirtualKeyboard::Show();
538   }
539
540   SetKeyInputFocus();
541
542   mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
543 }
544
545 void TextField::OnPan( const PanGesture& gesture )
546 {
547   mController->PanEvent( gesture.state, gesture.displacement );
548 }
549
550 bool TextField::OnKeyEvent( const KeyEvent& event )
551 {
552   if( Dali::DALI_KEY_ESCAPE == event.keyCode )
553   {
554     ClearKeyInputFocus();
555   }
556
557   return mController->KeyEvent( event );
558 }
559
560 ImfManager::ImfCallbackData TextField::OnImfEvent( Dali::ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
561 {
562   switch ( imfEvent.eventName )
563   {
564     case ImfManager::COMMIT:
565     {
566       KeyEvent event( "", imfEvent.predictiveString, 0, 0, 0, KeyEvent::Down );
567       mController->KeyEvent( event );
568       break;
569     }
570     case ImfManager::PREEDIT: // fall through
571     case ImfManager::DELETESURROUNDING:
572     case ImfManager::GETSURROUNDING:
573     case ImfManager::VOID:
574     {
575       // do nothing
576     }
577   } // end switch
578
579   return ImfManager::ImfCallbackData();
580 }
581
582 void TextField::RequestTextRelayout()
583 {
584   RelayoutRequest();
585 }
586
587 void TextField::EnableClipping( bool clipping, const Vector2& size )
588 {
589   if( clipping )
590   {
591     // Not worth to created clip actor if width or height is equal to zero.
592     if( size.width > Math::MACHINE_EPSILON_1000 && size.height > Math::MACHINE_EPSILON_1000 )
593     {
594       if( !mClipper )
595       {
596         Actor self = Self();
597
598         mClipper = Clipper::New( size );
599         self.Add( mClipper->GetRootActor() );
600         self.Add( mClipper->GetImageActor() );
601       }
602       else if ( mClipper )
603       {
604         mClipper->Refresh( size );
605       }
606     }
607   }
608   else
609   {
610     // Note - this will automatically remove the root & image actors
611     mClipper.Reset();
612   }
613 }
614
615 void TextField::KeyboardStatusChanged(bool keyboardShown)
616 {
617   // Just hide the grab handle when keyboard is hidden.
618   if (!keyboardShown )
619   {
620     mController->KeyboardFocusLostEvent();
621   }
622 }
623
624 TextField::TextField()
625 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
626   mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
627   mExceedPolicy( Dali::Toolkit::TextField::EXCEED_POLICY_CLIP )
628 {
629 }
630
631 TextField::~TextField()
632 {
633   mClipper.Reset();
634 }
635
636 } // namespace Internal
637
638 } // namespace Toolkit
639
640 } // namespace Dali