Added some keyboard & cursor plumbing
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller.cpp
index 4395e1b..79185ba 100644 (file)
@@ -102,7 +102,9 @@ struct Controller::TextInput
   : mLogicalModel( logicalModel ),
     mVisualModel( visualModel ),
     mDecorator( decorator ),
-    mState( INACTIVE )
+    mState( INACTIVE ),
+    mDecoratorUpdated( false ),
+    mCursorBlinkEnabled( true )
   {
   }
 
@@ -155,6 +157,14 @@ struct Controller::TextInput
 
   void OnKeyboardFocus( bool hasFocus )
   {
+    if( !hasFocus )
+    {
+      ChangeState( INACTIVE );
+    }
+    else
+    {
+      ChangeState( EDITING );
+    }
   }
 
   void OnKeyEvent( const Event& event )
@@ -278,7 +288,10 @@ struct Controller::TextInput
       else if( EDITING == mState )
       {
         mDecorator->SetActiveCursor( ACTIVE_CURSOR_PRIMARY );
-        mDecorator->StartCursorBlink();
+        if( mCursorBlinkEnabled )
+        {
+          mDecorator->StartCursorBlink();
+        }
         mDecorator->SetGrabHandleActive( true );
         mDecorator->SetSelectionActive( false );
         mDecoratorUpdated = true;
@@ -348,7 +361,8 @@ struct Controller::TextInput
 
   State mState;
 
-  bool mDecoratorUpdated;
+  bool mDecoratorUpdated   : 1;
+  bool mCursorBlinkEnabled : 1;
 };
 
 struct Controller::FontDefaults
@@ -553,6 +567,20 @@ float Controller::GetDefaultPointSize() const
   return 0.0f;
 }
 
+void Controller::GetDefaultFonts( Vector<FontRun>& fonts, Length numberOfCharacters )
+{
+  if( mImpl->mFontDefaults )
+  {
+    FontRun fontRun;
+    fontRun.characterRun.characterIndex = 0;
+    fontRun.characterRun.numberOfCharacters = numberOfCharacters;
+    fontRun.fontId = mImpl->mFontDefaults->GetFontId( mImpl->mFontClient );
+    fontRun.isDefault = true;
+
+    fonts.PushBack( fontRun );
+  }
+}
+
 void Controller::EnableTextInput( DecoratorPtr decorator )
 {
   if( !mImpl->mTextInput )
@@ -561,12 +589,45 @@ void Controller::EnableTextInput( DecoratorPtr decorator )
   }
 }
 
+void Controller::SetEnableCursorBlink( bool enable )
+{
+  DALI_ASSERT_DEBUG( NULL != mImpl->mTextInput && "TextInput disabled" );
+
+  if( mImpl->mTextInput )
+  {
+    mImpl->mTextInput->mCursorBlinkEnabled = enable;
+
+    if( !enable &&
+        mImpl->mTextInput->mDecorator )
+    {
+      mImpl->mTextInput->mDecorator->StopCursorBlink();
+    }
+  }
+}
+
+bool Controller::GetEnableCursorBlink() const
+{
+  if( mImpl->mTextInput )
+  {
+    return mImpl->mTextInput->mCursorBlinkEnabled;
+  }
+
+  return false;
+}
+
 bool Controller::Relayout( const Vector2& size )
 {
   if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
   {
+    bool glyphsRemoved( false );
+    if( 0u != mImpl->mVisualModel->GetNumberOfGlyphPositions() )
+    {
+      mImpl->mVisualModel->SetGlyphPositions( NULL, 0u );
+      glyphsRemoved = true;
+    }
+
     // Not worth to relayout if width or height is equal to zero.
-    return false;
+    return glyphsRemoved;
   }
 
   if( size != mImpl->mControlSize )
@@ -632,6 +693,8 @@ bool Controller::DoRelayout( const Vector2& size,
     text.clear();
   }
 
+  const Length numberOfCharacters = mImpl->mLogicalModel->GetNumberOfCharacters();
+
   Vector<LineBreakInfo> lineBreakInfo;
   if( GET_LINE_BREAKS & operations )
   {
@@ -663,12 +726,7 @@ bool Controller::DoRelayout( const Vector2& size,
   const bool validateFonts = VALIDATE_FONTS & operations;
 
   Vector<ScriptRun> scripts;
-  Vector<FontRun> fonts;
-
-  if( mImpl->mFontDefaults )
-  {
-    // TODO - pass into ValidateFonts
-  }
+  Vector<FontRun> validFonts;
 
   if( getScripts || validateFonts )
   {
@@ -689,14 +747,18 @@ bool Controller::DoRelayout( const Vector2& size,
 
     if( validateFonts )
     {
+      // Copy the requested font defaults received via the property system.
+      // These may not be valid i.e. may not contain glyphs for the necessary scripts.
+      GetDefaultFonts( validFonts, numberOfCharacters );
+
       // Validates the fonts. If there is a character with no assigned font it sets a default one.
       // After this call, fonts are validated.
       multilanguageSupport.ValidateFonts( utf32Characters,
                                           scripts,
-                                          fonts );
+                                          validFonts );
 
       // Sets the fonts into the model.
-      mImpl->mLogicalModel->SetFonts( fonts.Begin(), fonts.Count() );
+      mImpl->mLogicalModel->SetFonts( validFonts.Begin(), validFonts.Count() );
     }
   }
 
@@ -707,8 +769,6 @@ bool Controller::DoRelayout( const Vector2& size,
     // after the first time the text has been laid out.
     // Fill the vectors again.
 
-    const Length numberOfCharacters = mImpl->mLogicalModel->GetNumberOfCharacters();
-
     if( 0u == utf32Characters.Count() )
     {
       utf32Characters.Resize( numberOfCharacters );
@@ -767,11 +827,20 @@ bool Controller::DoRelayout( const Vector2& size,
   Vector<Length> charactersPerGlyph;
   if( SHAPE_TEXT & operations )
   {
+    if( 0u == validFonts.Count() )
+    {
+      validFonts.Resize( mImpl->mLogicalModel->GetNumberOfFontRuns( 0u,
+                                                                    numberOfCharacters ) );
+      mImpl->mLogicalModel->GetFontRuns( validFonts.Begin(),
+                                         0u,
+                                         numberOfCharacters );
+    }
+
     // Shapes the text.
     ShapeText( utf32Characters,
                lineBreakInfo,
                scripts,
-               fonts,
+               validFonts,
                glyphs,
                glyphsToCharactersMap,
                charactersPerGlyph );