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