2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/text/text-controller.h>
24 #include <dali/public-api/adaptor-framework/key.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/devel-api/adaptor-framework/clipboard-event-notifier.h>
29 #include <dali-toolkit/internal/text/bidirectional-support.h>
30 #include <dali-toolkit/internal/text/character-set-conversion.h>
31 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
32 #include <dali-toolkit/internal/text/markup-processor.h>
33 #include <dali-toolkit/internal/text/text-controller-impl.h>
38 #if defined(DEBUG_ENABLED)
39 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
42 const float MAX_FLOAT = std::numeric_limits<float>::max();
44 const std::string EMPTY_STRING("");
46 float ConvertToEven( float value )
48 int intValue(static_cast<int>( value ));
49 return static_cast<float>(intValue % 2 == 0) ? intValue : (intValue + 1);
64 * @brief Adds a new font description run for the selected text.
66 * The new font parameters are added after the call to this method.
68 * @param[in] eventData The event data pointer.
69 * @param[in] logicalModel The logical model where to add the new font description run.
70 * @param[out] startOfSelectedText Index to the first selected character.
71 * @param[out] lengthOfSelectedText Number of selected characters.
73 FontDescriptionRun& UpdateSelectionFontStyleRun( EventData* eventData,
74 LogicalModelPtr logicalModel,
75 CharacterIndex& startOfSelectedText,
76 Length& lengthOfSelectedText )
78 const bool handlesCrossed = eventData->mLeftSelectionPosition > eventData->mRightSelectionPosition;
80 // Get start and end position of selection
81 startOfSelectedText = handlesCrossed ? eventData->mRightSelectionPosition : eventData->mLeftSelectionPosition;
82 lengthOfSelectedText = ( handlesCrossed ? eventData->mLeftSelectionPosition : eventData->mRightSelectionPosition ) - startOfSelectedText;
85 const VectorBase::SizeType numberOfRuns = logicalModel->mFontDescriptionRuns.Count();
86 logicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
88 FontDescriptionRun& fontDescriptionRun = *( logicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
90 fontDescriptionRun.characterRun.characterIndex = startOfSelectedText;
91 fontDescriptionRun.characterRun.numberOfCharacters = lengthOfSelectedText;
93 // Recalculate the selection highlight as the metrics may have changed.
94 eventData->mUpdateLeftSelectionPosition = true;
95 eventData->mUpdateRightSelectionPosition = true;
96 eventData->mUpdateHighlightBox = true;
98 return fontDescriptionRun;
101 // public : Constructor.
103 ControllerPtr Controller::New( ControlInterface& controlInterface )
105 return ControllerPtr( new Controller( controlInterface ) );
108 // public : Configure the text controller.
110 void Controller::EnableTextInput( DecoratorPtr decorator )
112 if( NULL == mImpl->mEventData )
114 mImpl->mEventData = new EventData( decorator );
118 void Controller::SetGlyphType( TextAbstraction::GlyphType glyphType )
120 // Metrics for bitmap & vector based glyphs are different
121 mImpl->mMetrics->SetGlyphType( glyphType );
123 // Clear the font-specific data
126 mImpl->RequestRelayout();
129 void Controller::SetMarkupProcessorEnabled( bool enable )
131 mImpl->mMarkupProcessorEnabled = enable;
134 bool Controller::IsMarkupProcessorEnabled() const
136 return mImpl->mMarkupProcessorEnabled;
139 void Controller::SetAutoScrollEnabled( bool enable )
141 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled[%s] SingleBox[%s]-> [%p]\n", (enable)?"true":"false", ( mImpl->mLayoutEngine.GetLayout() == LayoutEngine::SINGLE_LINE_BOX)?"true":"false", this );
143 if ( mImpl->mLayoutEngine.GetLayout() == LayoutEngine::SINGLE_LINE_BOX )
147 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled for SINGLE_LINE_BOX\n" );
148 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
158 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled Disabling autoscroll\n");
159 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
166 mImpl->mAutoScrollEnabled = enable;
167 mImpl->RequestRelayout();
171 DALI_LOG_WARNING( "Attempted AutoScrolling on a non SINGLE_LINE_BOX, request ignored\n" );
172 mImpl->mAutoScrollEnabled = false;
176 bool Controller::IsAutoScrollEnabled() const
178 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::IsAutoScrollEnabled[%s]\n", (mImpl->mAutoScrollEnabled)?"true":"false" );
180 return mImpl->mAutoScrollEnabled;
183 CharacterDirection Controller::GetAutoScrollDirection() const
185 return mImpl->mAutoScrollDirectionRTL;
188 float Controller::GetAutoScrollLineAlignment() const
192 if( mImpl->mVisualModel &&
193 ( 0u != mImpl->mVisualModel->mLines.Count() ) )
195 offset = ( *mImpl->mVisualModel->mLines.Begin() ).alignmentOffset;
201 void Controller::SetHorizontalScrollEnabled( bool enable )
203 if( ( NULL != mImpl->mEventData ) &&
204 mImpl->mEventData->mDecorator )
206 mImpl->mEventData->mDecorator->SetHorizontalScrollEnabled( enable );
210 bool Controller::IsHorizontalScrollEnabled() const
212 if( ( NULL != mImpl->mEventData ) &&
213 mImpl->mEventData->mDecorator )
215 return mImpl->mEventData->mDecorator->IsHorizontalScrollEnabled();
221 void Controller::SetVerticalScrollEnabled( bool enable )
223 if( ( NULL != mImpl->mEventData ) &&
224 mImpl->mEventData->mDecorator )
226 if( mImpl->mEventData->mDecorator )
228 mImpl->mEventData->mDecorator->SetVerticalScrollEnabled( enable );
233 bool Controller::IsVerticalScrollEnabled() const
235 if( ( NULL != mImpl->mEventData ) &&
236 mImpl->mEventData->mDecorator )
238 return mImpl->mEventData->mDecorator->IsVerticalScrollEnabled();
244 void Controller::SetSmoothHandlePanEnabled( bool enable )
246 if( ( NULL != mImpl->mEventData ) &&
247 mImpl->mEventData->mDecorator )
249 mImpl->mEventData->mDecorator->SetSmoothHandlePanEnabled( enable );
253 bool Controller::IsSmoothHandlePanEnabled() const
255 if( ( NULL != mImpl->mEventData ) &&
256 mImpl->mEventData->mDecorator )
258 return mImpl->mEventData->mDecorator->IsSmoothHandlePanEnabled();
264 void Controller::SetMaximumNumberOfCharacters( Length maxCharacters )
266 mImpl->mMaximumNumberOfCharacters = maxCharacters;
269 int Controller::GetMaximumNumberOfCharacters()
271 return mImpl->mMaximumNumberOfCharacters;
274 void Controller::SetEnableCursorBlink( bool enable )
276 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
278 if( NULL != mImpl->mEventData )
280 mImpl->mEventData->mCursorBlinkEnabled = enable;
283 mImpl->mEventData->mDecorator )
285 mImpl->mEventData->mDecorator->StopCursorBlink();
290 bool Controller::GetEnableCursorBlink() const
292 if( NULL != mImpl->mEventData )
294 return mImpl->mEventData->mCursorBlinkEnabled;
300 void Controller::SetMultiLineEnabled( bool enable )
302 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
304 if( layout != mImpl->mLayoutEngine.GetLayout() )
306 // Set the layout type.
307 mImpl->mLayoutEngine.SetLayout( layout );
309 // Set the flags to redo the layout operations
310 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
315 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
317 mImpl->RequestRelayout();
321 bool Controller::IsMultiLineEnabled() const
323 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
326 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
328 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
330 // Set the alignment.
331 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
333 // Set the flag to redo the alignment operation.
334 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
336 mImpl->RequestRelayout();
340 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
342 return mImpl->mLayoutEngine.GetHorizontalAlignment();
345 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
347 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
349 // Set the alignment.
350 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
352 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
354 mImpl->RequestRelayout();
358 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
360 return mImpl->mLayoutEngine.GetVerticalAlignment();
365 void Controller::SetText( const std::string& text )
367 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
369 // Reset keyboard as text changed
370 mImpl->ResetImfManager();
372 // Remove the previously set text and style.
378 CharacterIndex lastCursorIndex = 0u;
380 if( NULL != mImpl->mEventData )
382 // If popup shown then hide it by switching to Editing state
383 if( ( EventData::SELECTING == mImpl->mEventData->mState ) ||
384 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
385 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
386 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
388 mImpl->ChangeState( EventData::EDITING );
394 mImpl->mVisualModel->SetTextColor( mImpl->mTextColor );
396 MarkupProcessData markupProcessData( mImpl->mLogicalModel->mColorRuns,
397 mImpl->mLogicalModel->mFontDescriptionRuns );
399 Length textSize = 0u;
400 const uint8_t* utf8 = NULL;
401 if( mImpl->mMarkupProcessorEnabled )
403 ProcessMarkupString( text, markupProcessData );
404 textSize = markupProcessData.markupProcessedText.size();
406 // This is a bit horrible but std::string returns a (signed) char*
407 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
411 textSize = text.size();
413 // This is a bit horrible but std::string returns a (signed) char*
414 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
417 // Convert text into UTF-32
418 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
419 utf32Characters.Resize( textSize );
421 // Transform a text array encoded in utf8 into an array encoded in utf32.
422 // It returns the actual number of characters.
423 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
424 utf32Characters.Resize( characterCount );
426 DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
427 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mLogicalModel->mText.Count() );
429 // The characters to be added.
430 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
432 // To reset the cursor position
433 lastCursorIndex = characterCount;
435 // Update the rest of the model during size negotiation
436 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
438 // The natural size needs to be re-calculated.
439 mImpl->mRecalculateNaturalSize = true;
441 // Apply modifications to the model
442 mImpl->mOperationsPending = ALL_OPERATIONS;
446 ShowPlaceholderText();
449 // Resets the cursor position.
450 ResetCursorPosition( lastCursorIndex );
452 // Scrolls the text to make the cursor visible.
453 ResetScrollPosition();
455 mImpl->RequestRelayout();
457 if( NULL != mImpl->mEventData )
459 // Cancel previously queued events
460 mImpl->mEventData->mEventQueue.clear();
463 // Do this last since it provides callbacks into application code
464 mImpl->mControlInterface.TextChanged();
467 void Controller::GetText( std::string& text ) const
469 if( !mImpl->IsShowingPlaceholderText() )
471 // Retrieves the text string.
472 mImpl->GetText( 0u, text );
476 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
480 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
482 if( NULL != mImpl->mEventData )
484 if( PLACEHOLDER_TYPE_INACTIVE == type )
486 mImpl->mEventData->mPlaceholderTextInactive = text;
490 mImpl->mEventData->mPlaceholderTextActive = text;
493 // Update placeholder if there is no text
494 if( mImpl->IsShowingPlaceholderText() ||
495 ( 0u == mImpl->mLogicalModel->mText.Count() ) )
497 ShowPlaceholderText();
502 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
504 if( NULL != mImpl->mEventData )
506 if( PLACEHOLDER_TYPE_INACTIVE == type )
508 text = mImpl->mEventData->mPlaceholderTextInactive;
512 text = mImpl->mEventData->mPlaceholderTextActive;
517 void Controller::UpdateAfterFontChange( const std::string& newDefaultFont )
519 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::UpdateAfterFontChange\n");
521 if( !mImpl->mFontDefaults->familyDefined ) // If user defined font then should not update when system font changes
523 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
524 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
528 mImpl->RequestRelayout();
532 // public : Default style & Input style
534 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
536 if( NULL == mImpl->mFontDefaults )
538 mImpl->mFontDefaults = new FontDefaults();
541 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
542 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
543 mImpl->mFontDefaults->familyDefined = !defaultFontFamily.empty();
545 // Clear the font-specific data
548 mImpl->RequestRelayout();
551 const std::string& Controller::GetDefaultFontFamily() const
553 if( NULL != mImpl->mFontDefaults )
555 return mImpl->mFontDefaults->mFontDescription.family;
561 void Controller::SetDefaultFontWeight( FontWeight weight )
563 if( NULL == mImpl->mFontDefaults )
565 mImpl->mFontDefaults = new FontDefaults();
568 mImpl->mFontDefaults->mFontDescription.weight = weight;
569 mImpl->mFontDefaults->weightDefined = true;
571 // Clear the font-specific data
574 mImpl->RequestRelayout();
577 bool Controller::IsDefaultFontWeightDefined() const
579 return mImpl->mFontDefaults->weightDefined;
582 FontWeight Controller::GetDefaultFontWeight() const
584 if( NULL != mImpl->mFontDefaults )
586 return mImpl->mFontDefaults->mFontDescription.weight;
589 return TextAbstraction::FontWeight::NORMAL;
592 void Controller::SetDefaultFontWidth( FontWidth width )
594 if( NULL == mImpl->mFontDefaults )
596 mImpl->mFontDefaults = new FontDefaults();
599 mImpl->mFontDefaults->mFontDescription.width = width;
600 mImpl->mFontDefaults->widthDefined = true;
602 // Clear the font-specific data
605 mImpl->RequestRelayout();
608 bool Controller::IsDefaultFontWidthDefined() const
610 return mImpl->mFontDefaults->widthDefined;
613 FontWidth Controller::GetDefaultFontWidth() const
615 if( NULL != mImpl->mFontDefaults )
617 return mImpl->mFontDefaults->mFontDescription.width;
620 return TextAbstraction::FontWidth::NORMAL;
623 void Controller::SetDefaultFontSlant( FontSlant slant )
625 if( NULL == mImpl->mFontDefaults )
627 mImpl->mFontDefaults = new FontDefaults();
630 mImpl->mFontDefaults->mFontDescription.slant = slant;
631 mImpl->mFontDefaults->slantDefined = true;
633 // Clear the font-specific data
636 mImpl->RequestRelayout();
639 bool Controller::IsDefaultFontSlantDefined() const
641 return mImpl->mFontDefaults->slantDefined;
644 FontSlant Controller::GetDefaultFontSlant() const
646 if( NULL != mImpl->mFontDefaults )
648 return mImpl->mFontDefaults->mFontDescription.slant;
651 return TextAbstraction::FontSlant::NORMAL;
654 void Controller::SetDefaultPointSize( float pointSize )
656 if( NULL == mImpl->mFontDefaults )
658 mImpl->mFontDefaults = new FontDefaults();
661 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
662 mImpl->mFontDefaults->sizeDefined = true;
664 // Clear the font-specific data
667 mImpl->RequestRelayout();
670 float Controller::GetDefaultPointSize() const
672 if( NULL != mImpl->mFontDefaults )
674 return mImpl->mFontDefaults->mDefaultPointSize;
680 void Controller::SetTextColor( const Vector4& textColor )
682 mImpl->mTextColor = textColor;
684 if( !mImpl->IsShowingPlaceholderText() )
686 mImpl->mVisualModel->SetTextColor( textColor );
688 mImpl->RequestRelayout();
692 const Vector4& Controller::GetTextColor() const
694 return mImpl->mTextColor;
697 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
699 if( NULL != mImpl->mEventData )
701 mImpl->mEventData->mPlaceholderTextColor = textColor;
704 if( mImpl->IsShowingPlaceholderText() )
706 mImpl->mVisualModel->SetTextColor( textColor );
707 mImpl->RequestRelayout();
711 const Vector4& Controller::GetPlaceholderTextColor() const
713 if( NULL != mImpl->mEventData )
715 return mImpl->mEventData->mPlaceholderTextColor;
721 void Controller::SetShadowOffset( const Vector2& shadowOffset )
723 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
725 mImpl->RequestRelayout();
728 const Vector2& Controller::GetShadowOffset() const
730 return mImpl->mVisualModel->GetShadowOffset();
733 void Controller::SetShadowColor( const Vector4& shadowColor )
735 mImpl->mVisualModel->SetShadowColor( shadowColor );
737 mImpl->RequestRelayout();
740 const Vector4& Controller::GetShadowColor() const
742 return mImpl->mVisualModel->GetShadowColor();
745 void Controller::SetDefaultShadowProperties( const std::string& shadowProperties )
747 if( NULL == mImpl->mShadowDefaults )
749 mImpl->mShadowDefaults = new ShadowDefaults();
752 mImpl->mShadowDefaults->properties = shadowProperties;
755 const std::string& Controller::GetDefaultShadowProperties() const
757 if( NULL != mImpl->mShadowDefaults )
759 return mImpl->mShadowDefaults->properties;
765 void Controller::SetUnderlineColor( const Vector4& color )
767 mImpl->mVisualModel->SetUnderlineColor( color );
769 mImpl->RequestRelayout();
772 const Vector4& Controller::GetUnderlineColor() const
774 return mImpl->mVisualModel->GetUnderlineColor();
777 void Controller::SetUnderlineEnabled( bool enabled )
779 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
781 mImpl->RequestRelayout();
784 bool Controller::IsUnderlineEnabled() const
786 return mImpl->mVisualModel->IsUnderlineEnabled();
789 void Controller::SetUnderlineHeight( float height )
791 mImpl->mVisualModel->SetUnderlineHeight( height );
793 mImpl->RequestRelayout();
796 float Controller::GetUnderlineHeight() const
798 return mImpl->mVisualModel->GetUnderlineHeight();
801 void Controller::SetDefaultUnderlineProperties( const std::string& underlineProperties )
803 if( NULL == mImpl->mUnderlineDefaults )
805 mImpl->mUnderlineDefaults = new UnderlineDefaults();
808 mImpl->mUnderlineDefaults->properties = underlineProperties;
811 const std::string& Controller::GetDefaultUnderlineProperties() const
813 if( NULL != mImpl->mUnderlineDefaults )
815 return mImpl->mUnderlineDefaults->properties;
821 void Controller::SetDefaultEmbossProperties( const std::string& embossProperties )
823 if( NULL == mImpl->mEmbossDefaults )
825 mImpl->mEmbossDefaults = new EmbossDefaults();
828 mImpl->mEmbossDefaults->properties = embossProperties;
831 const std::string& Controller::GetDefaultEmbossProperties() const
833 if( NULL != mImpl->mEmbossDefaults )
835 return mImpl->mEmbossDefaults->properties;
841 void Controller::SetDefaultOutlineProperties( const std::string& outlineProperties )
843 if( NULL == mImpl->mOutlineDefaults )
845 mImpl->mOutlineDefaults = new OutlineDefaults();
848 mImpl->mOutlineDefaults->properties = outlineProperties;
851 const std::string& Controller::GetDefaultOutlineProperties() const
853 if( NULL != mImpl->mOutlineDefaults )
855 return mImpl->mOutlineDefaults->properties;
861 void Controller::SetDefaultLineSpacing( float lineSpacing )
863 //TODO finish implementation
864 mImpl->mLayoutEngine.SetDefaultLineSpacing( lineSpacing );
867 float Controller::GetDefaultLineSpacing() const
869 return mImpl->mLayoutEngine.GetDefaultLineSpacing();
872 void Controller::SetInputColor( const Vector4& color )
874 if( NULL != mImpl->mEventData )
876 mImpl->mEventData->mInputStyle.textColor = color;
877 mImpl->mEventData->mInputStyle.isDefaultColor = false;
879 if( EventData::SELECTING == mImpl->mEventData->mState )
881 const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
883 // Get start and end position of selection
884 const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
885 const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
887 // Add the color run.
888 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
889 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
891 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
892 colorRun.color = color;
893 colorRun.characterRun.characterIndex = startOfSelectedText;
894 colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
896 // Request to relayout.
897 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
898 mImpl->RequestRelayout();
900 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
901 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
902 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
907 const Vector4& Controller::GetInputColor() const
909 if( NULL != mImpl->mEventData )
911 return mImpl->mEventData->mInputStyle.textColor;
914 // Return the default text's color if there is no EventData.
915 return mImpl->mTextColor;
919 void Controller::SetInputFontFamily( const std::string& fontFamily )
921 if( NULL != mImpl->mEventData )
923 mImpl->mEventData->mInputStyle.familyName = fontFamily;
924 mImpl->mEventData->mInputStyle.isFamilyDefined = true;
926 if( EventData::SELECTING == mImpl->mEventData->mState )
928 CharacterIndex startOfSelectedText = 0u;
929 Length lengthOfSelectedText = 0u;
930 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
931 mImpl->mLogicalModel,
933 lengthOfSelectedText );
935 fontDescriptionRun.familyLength = fontFamily.size();
936 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
937 memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
938 fontDescriptionRun.familyDefined = true;
940 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
942 // Request to relayout.
943 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
951 mImpl->mRecalculateNaturalSize = true;
952 mImpl->RequestRelayout();
954 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
955 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
956 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
958 // As the font changes, recalculate the handle positions is needed.
959 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
960 mImpl->mEventData->mUpdateRightSelectionPosition = true;
961 mImpl->mEventData->mUpdateHighlightBox = true;
962 mImpl->mEventData->mScrollAfterUpdatePosition = true;
967 const std::string& Controller::GetInputFontFamily() const
969 if( NULL != mImpl->mEventData )
971 return mImpl->mEventData->mInputStyle.familyName;
974 // Return the default font's family if there is no EventData.
975 return GetDefaultFontFamily();
978 void Controller::SetInputFontWeight( FontWeight weight )
980 if( NULL != mImpl->mEventData )
982 mImpl->mEventData->mInputStyle.weight = weight;
983 mImpl->mEventData->mInputStyle.isWeightDefined = true;
985 if( EventData::SELECTING == mImpl->mEventData->mState )
987 CharacterIndex startOfSelectedText = 0u;
988 Length lengthOfSelectedText = 0u;
989 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
990 mImpl->mLogicalModel,
992 lengthOfSelectedText );
994 fontDescriptionRun.weight = weight;
995 fontDescriptionRun.weightDefined = true;
997 // Request to relayout.
998 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1003 UPDATE_LAYOUT_SIZE |
1006 mImpl->mRecalculateNaturalSize = true;
1007 mImpl->RequestRelayout();
1009 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1010 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1011 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1013 // As the font might change, recalculate the handle positions is needed.
1014 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1015 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1016 mImpl->mEventData->mUpdateHighlightBox = true;
1017 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1022 bool Controller::IsInputFontWeightDefined() const
1024 bool defined = false;
1026 if( NULL != mImpl->mEventData )
1028 defined = mImpl->mEventData->mInputStyle.isWeightDefined;
1034 FontWeight Controller::GetInputFontWeight() const
1036 if( NULL != mImpl->mEventData )
1038 return mImpl->mEventData->mInputStyle.weight;
1041 return GetDefaultFontWeight();
1044 void Controller::SetInputFontWidth( FontWidth width )
1046 if( NULL != mImpl->mEventData )
1048 mImpl->mEventData->mInputStyle.width = width;
1049 mImpl->mEventData->mInputStyle.isWidthDefined = true;
1051 if( EventData::SELECTING == mImpl->mEventData->mState )
1053 CharacterIndex startOfSelectedText = 0u;
1054 Length lengthOfSelectedText = 0u;
1055 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1056 mImpl->mLogicalModel,
1057 startOfSelectedText,
1058 lengthOfSelectedText );
1060 fontDescriptionRun.width = width;
1061 fontDescriptionRun.widthDefined = true;
1063 // Request to relayout.
1064 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1069 UPDATE_LAYOUT_SIZE |
1072 mImpl->mRecalculateNaturalSize = true;
1073 mImpl->RequestRelayout();
1075 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1076 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1077 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1079 // As the font might change, recalculate the handle positions is needed.
1080 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1081 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1082 mImpl->mEventData->mUpdateHighlightBox = true;
1083 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1088 bool Controller::IsInputFontWidthDefined() const
1090 bool defined = false;
1092 if( NULL != mImpl->mEventData )
1094 defined = mImpl->mEventData->mInputStyle.isWidthDefined;
1100 FontWidth Controller::GetInputFontWidth() const
1102 if( NULL != mImpl->mEventData )
1104 return mImpl->mEventData->mInputStyle.width;
1107 return GetDefaultFontWidth();
1110 void Controller::SetInputFontSlant( FontSlant slant )
1112 if( NULL != mImpl->mEventData )
1114 mImpl->mEventData->mInputStyle.slant = slant;
1115 mImpl->mEventData->mInputStyle.isSlantDefined = true;
1117 if( EventData::SELECTING == mImpl->mEventData->mState )
1119 CharacterIndex startOfSelectedText = 0u;
1120 Length lengthOfSelectedText = 0u;
1121 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1122 mImpl->mLogicalModel,
1123 startOfSelectedText,
1124 lengthOfSelectedText );
1126 fontDescriptionRun.slant = slant;
1127 fontDescriptionRun.slantDefined = true;
1129 // Request to relayout.
1130 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1135 UPDATE_LAYOUT_SIZE |
1138 mImpl->mRecalculateNaturalSize = true;
1139 mImpl->RequestRelayout();
1141 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1142 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1143 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1145 // As the font might change, recalculate the handle positions is needed.
1146 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1147 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1148 mImpl->mEventData->mUpdateHighlightBox = true;
1149 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1154 bool Controller::IsInputFontSlantDefined() const
1156 bool defined = false;
1158 if( NULL != mImpl->mEventData )
1160 defined = mImpl->mEventData->mInputStyle.isSlantDefined;
1166 FontSlant Controller::GetInputFontSlant() const
1168 if( NULL != mImpl->mEventData )
1170 return mImpl->mEventData->mInputStyle.slant;
1173 return GetDefaultFontSlant();
1176 void Controller::SetInputFontPointSize( float size )
1178 if( NULL != mImpl->mEventData )
1180 mImpl->mEventData->mInputStyle.size = size;
1181 mImpl->mEventData->mInputStyle.isSizeDefined = true;
1183 if( EventData::SELECTING == mImpl->mEventData->mState )
1185 CharacterIndex startOfSelectedText = 0u;
1186 Length lengthOfSelectedText = 0u;
1187 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1188 mImpl->mLogicalModel,
1189 startOfSelectedText,
1190 lengthOfSelectedText );
1192 fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
1193 fontDescriptionRun.sizeDefined = true;
1195 // Request to relayout.
1196 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1201 UPDATE_LAYOUT_SIZE |
1204 mImpl->mRecalculateNaturalSize = true;
1205 mImpl->RequestRelayout();
1207 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1208 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1209 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1211 // As the font might change, recalculate the handle positions is needed.
1212 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1213 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1214 mImpl->mEventData->mUpdateHighlightBox = true;
1215 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1220 float Controller::GetInputFontPointSize() const
1222 if( NULL != mImpl->mEventData )
1224 return mImpl->mEventData->mInputStyle.size;
1227 // Return the default font's point size if there is no EventData.
1228 return GetDefaultPointSize();
1231 void Controller::SetInputLineSpacing( float lineSpacing )
1233 if( NULL != mImpl->mEventData )
1235 mImpl->mEventData->mInputStyle.lineSpacing = lineSpacing;
1236 mImpl->mEventData->mInputStyle.isLineSpacingDefined = true;
1240 float Controller::GetInputLineSpacing() const
1242 if( NULL != mImpl->mEventData )
1244 return mImpl->mEventData->mInputStyle.lineSpacing;
1250 void Controller::SetInputShadowProperties( const std::string& shadowProperties )
1252 if( NULL != mImpl->mEventData )
1254 mImpl->mEventData->mInputStyle.shadowProperties = shadowProperties;
1258 const std::string& Controller::GetInputShadowProperties() const
1260 if( NULL != mImpl->mEventData )
1262 return mImpl->mEventData->mInputStyle.shadowProperties;
1265 return GetDefaultShadowProperties();
1268 void Controller::SetInputUnderlineProperties( const std::string& underlineProperties )
1270 if( NULL != mImpl->mEventData )
1272 mImpl->mEventData->mInputStyle.underlineProperties = underlineProperties;
1276 const std::string& Controller::GetInputUnderlineProperties() const
1278 if( NULL != mImpl->mEventData )
1280 return mImpl->mEventData->mInputStyle.underlineProperties;
1283 return GetDefaultUnderlineProperties();
1286 void Controller::SetInputEmbossProperties( const std::string& embossProperties )
1288 if( NULL != mImpl->mEventData )
1290 mImpl->mEventData->mInputStyle.embossProperties = embossProperties;
1294 const std::string& Controller::GetInputEmbossProperties() const
1296 if( NULL != mImpl->mEventData )
1298 return mImpl->mEventData->mInputStyle.embossProperties;
1301 return GetDefaultEmbossProperties();
1304 void Controller::SetInputOutlineProperties( const std::string& outlineProperties )
1306 if( NULL != mImpl->mEventData )
1308 mImpl->mEventData->mInputStyle.outlineProperties = outlineProperties;
1312 const std::string& Controller::GetInputOutlineProperties() const
1314 if( NULL != mImpl->mEventData )
1316 return mImpl->mEventData->mInputStyle.outlineProperties;
1319 return GetDefaultOutlineProperties();
1322 // public : Queries & retrieves.
1324 LayoutEngine& Controller::GetLayoutEngine()
1326 return mImpl->mLayoutEngine;
1329 View& Controller::GetView()
1331 return mImpl->mView;
1334 const Vector2& Controller::GetScrollPosition() const
1336 return mImpl->mScrollPosition;
1339 Vector3 Controller::GetNaturalSize()
1341 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
1342 Vector3 naturalSize;
1344 // Make sure the model is up-to-date before layouting
1345 ProcessModifyEvents();
1347 if( mImpl->mRecalculateNaturalSize )
1349 // Operations that can be done only once until the text changes.
1350 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1357 GET_GLYPH_METRICS );
1358 // Make sure the model is up-to-date before layouting
1359 mImpl->UpdateModel( onlyOnceOperations );
1361 // Layout the text for the new width.
1362 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1364 // Set the update info to relayout the whole text.
1365 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1366 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1368 // Store the actual control's size to restore later.
1369 const Size actualControlSize = mImpl->mVisualModel->mControlSize;
1371 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
1372 static_cast<OperationsMask>( onlyOnceOperations |
1374 naturalSize.GetVectorXY() );
1376 // Do not do again the only once operations.
1377 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1379 // Do the size related operations again.
1380 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1383 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1385 // Stores the natural size to avoid recalculate it again
1386 // unless the text/style changes.
1387 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
1389 mImpl->mRecalculateNaturalSize = false;
1391 // Clear the update info. This info will be set the next time the text is updated.
1392 mImpl->mTextUpdateInfo.Clear();
1394 // Restore the actual control's size.
1395 mImpl->mVisualModel->mControlSize = actualControlSize;
1397 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1401 naturalSize = mImpl->mVisualModel->GetNaturalSize();
1403 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1406 naturalSize.x = ConvertToEven( naturalSize.x );
1407 naturalSize.y = ConvertToEven( naturalSize.y );
1412 float Controller::GetHeightForWidth( float width )
1414 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1415 // Make sure the model is up-to-date before layouting
1416 ProcessModifyEvents();
1419 if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
1421 // Operations that can be done only once until the text changes.
1422 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1429 GET_GLYPH_METRICS );
1430 // Make sure the model is up-to-date before layouting
1431 mImpl->UpdateModel( onlyOnceOperations );
1434 // Layout the text for the new width.
1435 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1437 // Set the update info to relayout the whole text.
1438 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1439 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1441 // Store the actual control's width.
1442 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
1444 DoRelayout( Size( width, MAX_FLOAT ),
1445 static_cast<OperationsMask>( onlyOnceOperations |
1449 // Do not do again the only once operations.
1450 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1452 // Do the size related operations again.
1453 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1457 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1459 // Clear the update info. This info will be set the next time the text is updated.
1460 mImpl->mTextUpdateInfo.Clear();
1462 // Restore the actual control's width.
1463 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1465 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1469 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1470 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1473 return layoutSize.height;
1476 // public : Relayout.
1478 Controller::UpdateTextType Controller::Relayout( const Size& size )
1480 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", this, size.width, size.height, (mImpl->mAutoScrollEnabled)?"true":"false" );
1482 UpdateTextType updateTextType = NONE_UPDATED;
1484 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1486 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1488 mImpl->mVisualModel->mGlyphPositions.Clear();
1489 updateTextType = MODEL_UPDATED;
1492 // Clear the update info. This info will be set the next time the text is updated.
1493 mImpl->mTextUpdateInfo.Clear();
1495 // Not worth to relayout if width or height is equal to zero.
1496 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1498 return updateTextType;
1501 // Whether a new size has been set.
1502 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1506 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1508 // Layout operations that need to be done if the size changes.
1509 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1512 UPDATE_LAYOUT_SIZE |
1514 // Set the update info to relayout the whole text.
1515 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
1516 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1519 // Whether there are modify events.
1520 if( 0u != mImpl->mModifyEvents.Count() )
1522 // Style operations that need to be done if the text is modified.
1523 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1527 // Make sure the model is up-to-date before layouting.
1528 ProcessModifyEvents();
1529 bool updated = mImpl->UpdateModel( mImpl->mOperationsPending );
1533 updated = DoRelayout( size,
1534 mImpl->mOperationsPending,
1535 layoutSize ) || updated;
1539 updateTextType = MODEL_UPDATED;
1542 // Do not re-do any operation until something changes.
1543 mImpl->mOperationsPending = NO_OPERATION;
1545 // Whether the text control is editable
1546 const bool isEditable = NULL != mImpl->mEventData;
1548 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
1550 if( newSize && isEditable )
1552 offset = mImpl->mScrollPosition;
1555 if( !isEditable || !IsMultiLineEnabled() )
1557 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
1558 CalculateVerticalOffset( size );
1565 // If there is a new size, the scroll position needs to be clamped.
1566 mImpl->ClampHorizontalScroll( layoutSize );
1568 // Update the decorator's positions is needed if there is a new size.
1569 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mScrollPosition - offset );
1572 // Move the cursor, grab handle etc.
1573 if( mImpl->ProcessInputEvents() )
1575 updateTextType = static_cast<UpdateTextType>( updateTextType | DECORATOR_UPDATED );
1579 // Clear the update info. This info will be set the next time the text is updated.
1580 mImpl->mTextUpdateInfo.Clear();
1581 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1583 return updateTextType;
1586 // public : Input style change signals.
1588 bool Controller::IsInputStyleChangedSignalsQueueEmpty()
1590 return ( NULL == mImpl->mEventData ) || ( 0u == mImpl->mEventData->mInputStyleChangedQueue.Count() );
1593 void Controller::ProcessInputStyleChangedSignals()
1595 if( NULL == mImpl->mEventData )
1601 for( Vector<InputStyle::Mask>::ConstIterator it = mImpl->mEventData->mInputStyleChangedQueue.Begin(),
1602 endIt = mImpl->mEventData->mInputStyleChangedQueue.End();
1606 const InputStyle::Mask mask = *it;
1608 // Emit the input style changed signal.
1609 mImpl->mControlInterface.InputStyleChanged( mask );
1612 mImpl->mEventData->mInputStyleChangedQueue.Clear();
1615 // public : Text-input Event Queuing.
1617 void Controller::KeyboardFocusGainEvent()
1619 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1621 if( NULL != mImpl->mEventData )
1623 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1624 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1626 mImpl->ChangeState( EventData::EDITING );
1627 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1628 mImpl->mEventData->mUpdateInputStyle = true;
1630 mImpl->NotifyImfMultiLineStatus();
1631 if( mImpl->IsShowingPlaceholderText() )
1633 // Show alternative placeholder-text when editing
1634 ShowPlaceholderText();
1637 mImpl->RequestRelayout();
1641 void Controller::KeyboardFocusLostEvent()
1643 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1645 if( NULL != mImpl->mEventData )
1647 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1649 mImpl->ChangeState( EventData::INACTIVE );
1651 if( !mImpl->IsShowingRealText() )
1653 // Revert to regular placeholder-text when not editing
1654 ShowPlaceholderText();
1658 mImpl->RequestRelayout();
1661 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1663 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1665 bool textChanged( false );
1667 if( ( NULL != mImpl->mEventData ) &&
1668 ( keyEvent.state == KeyEvent::Down ) )
1670 int keyCode = keyEvent.keyCode;
1671 const std::string& keyString = keyEvent.keyPressed;
1673 // Pre-process to separate modifying events from non-modifying input events.
1674 if( Dali::DALI_KEY_ESCAPE == keyCode )
1676 // Escape key is a special case which causes focus loss
1677 KeyboardFocusLostEvent();
1679 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
1680 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1681 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
1682 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
1684 Event event( Event::CURSOR_KEY_EVENT );
1685 event.p1.mInt = keyCode;
1686 mImpl->mEventData->mEventQueue.push_back( event );
1688 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1690 textChanged = BackspaceKeyEvent();
1692 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1694 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1695 // Avoids calling the InsertText() method which can delete selected text
1697 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1698 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1700 mImpl->ChangeState( EventData::INACTIVE );
1701 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1702 // Avoids calling the InsertText() method which can delete selected text
1704 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1706 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1707 // and a character is typed after the type of a upper case latin character.
1713 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1715 // IMF manager is no longer handling key-events
1716 mImpl->ClearPreEditFlag();
1718 InsertText( keyString, COMMIT );
1722 if ( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1723 ( mImpl->mEventData->mState != EventData::INACTIVE ) &&
1724 ( Dali::DALI_KEY_SHIFT_LEFT != keyCode ) )
1726 // Should not change the state if the key is the shift send by the imf manager.
1727 // Otherwise, when the state is SELECTING the text controller can't send the right
1728 // surrounding info to the imf.
1729 mImpl->ChangeState( EventData::EDITING );
1732 mImpl->RequestRelayout();
1737 // Do this last since it provides callbacks into application code
1738 mImpl->mControlInterface.TextChanged();
1744 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1746 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1748 if( NULL != mImpl->mEventData )
1750 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
1751 EventData::State state( mImpl->mEventData->mState );
1752 bool relayoutNeeded( false ); // to avoid unnecessary relayouts when tapping an empty text-field
1754 if( mImpl->IsClipboardVisible() )
1756 if( EventData::INACTIVE == state || EventData::EDITING == state)
1758 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
1760 relayoutNeeded = true;
1762 else if( 1u == tapCount )
1764 if( EventData::EDITING_WITH_POPUP == state || EventData::EDITING_WITH_PASTE_POPUP == state )
1766 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE ); // If Popup shown hide it here so can be shown again if required.
1769 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != state ) )
1771 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
1772 relayoutNeeded = true;
1776 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
1778 // Hide placeholder text
1782 if( EventData::INACTIVE == state )
1784 mImpl->ChangeState( EventData::EDITING );
1786 else if( !mImpl->IsClipboardEmpty() )
1788 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
1790 relayoutNeeded = true;
1793 else if( 2u == tapCount )
1795 if( mImpl->mEventData->mSelectionEnabled &&
1796 mImpl->IsShowingRealText() )
1798 SelectEvent( x, y, false );
1801 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
1802 if( relayoutNeeded )
1804 Event event( Event::TAP_EVENT );
1805 event.p1.mUint = tapCount;
1806 event.p2.mFloat = x;
1807 event.p3.mFloat = y;
1808 mImpl->mEventData->mEventQueue.push_back( event );
1810 mImpl->RequestRelayout();
1814 // Reset keyboard as tap event has occurred.
1815 mImpl->ResetImfManager();
1818 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
1820 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1822 if( NULL != mImpl->mEventData )
1824 Event event( Event::PAN_EVENT );
1825 event.p1.mInt = state;
1826 event.p2.mFloat = displacement.x;
1827 event.p3.mFloat = displacement.y;
1828 mImpl->mEventData->mEventQueue.push_back( event );
1830 mImpl->RequestRelayout();
1834 void Controller::LongPressEvent( Gesture::State state, float x, float y )
1836 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
1838 if( ( state == Gesture::Started ) &&
1839 ( NULL != mImpl->mEventData ) )
1841 if( !mImpl->IsShowingRealText() )
1843 Event event( Event::LONG_PRESS_EVENT );
1844 event.p1.mInt = state;
1845 mImpl->mEventData->mEventQueue.push_back( event );
1846 mImpl->RequestRelayout();
1850 // The 1st long-press on inactive text-field is treated as tap
1851 if( EventData::INACTIVE == mImpl->mEventData->mState )
1853 mImpl->ChangeState( EventData::EDITING );
1855 Event event( Event::TAP_EVENT );
1857 event.p2.mFloat = x;
1858 event.p3.mFloat = y;
1859 mImpl->mEventData->mEventQueue.push_back( event );
1861 mImpl->RequestRelayout();
1863 else if( !mImpl->IsClipboardVisible() )
1865 // Reset the imf manger to commit the pre-edit before selecting the text.
1866 mImpl->ResetImfManager();
1868 SelectEvent( x, y, false );
1874 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
1876 // Whether the text needs to be relaid-out.
1877 bool requestRelayout = false;
1879 // Whether to retrieve the text and cursor position to be sent to the IMF manager.
1880 bool retrieveText = false;
1881 bool retrieveCursor = false;
1883 switch( imfEvent.eventName )
1885 case ImfManager::COMMIT:
1887 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
1888 requestRelayout = true;
1889 retrieveCursor = true;
1892 case ImfManager::PREEDIT:
1894 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
1895 requestRelayout = true;
1896 retrieveCursor = true;
1899 case ImfManager::DELETESURROUNDING:
1901 const bool textDeleted = RemoveText( imfEvent.cursorOffset,
1902 imfEvent.numberOfChars,
1903 DONT_UPDATE_INPUT_STYLE );
1907 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
1908 !mImpl->IsPlaceholderAvailable() )
1910 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1914 ShowPlaceholderText();
1916 mImpl->mEventData->mUpdateCursorPosition = true;
1917 mImpl->mEventData->mScrollAfterDelete = true;
1919 requestRelayout = true;
1923 case ImfManager::GETSURROUNDING:
1925 retrieveText = true;
1926 retrieveCursor = true;
1929 case ImfManager::VOID:
1936 if( requestRelayout )
1938 mImpl->mOperationsPending = ALL_OPERATIONS;
1939 mImpl->RequestRelayout();
1943 CharacterIndex cursorPosition = 0u;
1944 Length numberOfWhiteSpaces = 0u;
1946 if( retrieveCursor )
1948 numberOfWhiteSpaces = mImpl->GetNumberOfWhiteSpaces( 0u );
1950 cursorPosition = mImpl->GetLogicalCursorPosition();
1952 if( cursorPosition < numberOfWhiteSpaces )
1954 cursorPosition = 0u;
1958 cursorPosition -= numberOfWhiteSpaces;
1964 mImpl->GetText( numberOfWhiteSpaces, text );
1967 ImfManager::ImfCallbackData callbackData( ( retrieveText || retrieveCursor ), cursorPosition, text, false );
1969 if( requestRelayout )
1971 // Do this last since it provides callbacks into application code
1972 mImpl->mControlInterface.TextChanged();
1975 return callbackData;
1978 void Controller::PasteClipboardItemEvent()
1980 // Retrieve the clipboard contents first
1981 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
1982 std::string stringToPaste( notifier.GetContent() );
1984 // Commit the current pre-edit text; the contents of the clipboard should be appended
1985 mImpl->ResetImfManager();
1987 // Temporary disable hiding clipboard
1988 mImpl->SetClipboardHideEnable( false );
1991 PasteText( stringToPaste );
1993 mImpl->SetClipboardHideEnable( true );
1996 // protected : Inherit from Text::Decorator::ControllerInterface.
1998 void Controller::GetTargetSize( Vector2& targetSize )
2000 targetSize = mImpl->mVisualModel->mControlSize;
2003 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2005 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2008 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2010 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2012 if( NULL != mImpl->mEventData )
2014 switch( handleType )
2018 Event event( Event::GRAB_HANDLE_EVENT );
2019 event.p1.mUint = state;
2020 event.p2.mFloat = x;
2021 event.p3.mFloat = y;
2023 mImpl->mEventData->mEventQueue.push_back( event );
2026 case LEFT_SELECTION_HANDLE:
2028 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2029 event.p1.mUint = state;
2030 event.p2.mFloat = x;
2031 event.p3.mFloat = y;
2033 mImpl->mEventData->mEventQueue.push_back( event );
2036 case RIGHT_SELECTION_HANDLE:
2038 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2039 event.p1.mUint = state;
2040 event.p2.mFloat = x;
2041 event.p3.mFloat = y;
2043 mImpl->mEventData->mEventQueue.push_back( event );
2046 case LEFT_SELECTION_HANDLE_MARKER:
2047 case RIGHT_SELECTION_HANDLE_MARKER:
2049 // Markers do not move the handles.
2052 case HANDLE_TYPE_COUNT:
2054 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2058 mImpl->RequestRelayout();
2062 // protected : Inherit from TextSelectionPopup::TextPopupButtonCallbackInterface.
2064 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2066 if( NULL == mImpl->mEventData )
2073 case Toolkit::TextSelectionPopup::CUT:
2075 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2076 mImpl->mOperationsPending = ALL_OPERATIONS;
2078 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2079 !mImpl->IsPlaceholderAvailable() )
2081 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2085 ShowPlaceholderText();
2088 mImpl->mEventData->mUpdateCursorPosition = true;
2089 mImpl->mEventData->mScrollAfterDelete = true;
2091 mImpl->RequestRelayout();
2092 mImpl->mControlInterface.TextChanged();
2095 case Toolkit::TextSelectionPopup::COPY:
2097 mImpl->SendSelectionToClipboard( false ); // Text not modified
2099 mImpl->mEventData->mUpdateCursorPosition = true;
2101 mImpl->RequestRelayout(); // Cursor, Handles, Selection Highlight, Popup
2104 case Toolkit::TextSelectionPopup::PASTE:
2106 mImpl->RequestGetTextFromClipboard(); // Request clipboard service to retrieve an item
2109 case Toolkit::TextSelectionPopup::SELECT:
2111 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2113 if( mImpl->mEventData->mSelectionEnabled )
2115 // Creates a SELECT event.
2116 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2120 case Toolkit::TextSelectionPopup::SELECT_ALL:
2122 // Creates a SELECT_ALL event
2123 SelectEvent( 0.f, 0.f, true );
2126 case Toolkit::TextSelectionPopup::CLIPBOARD:
2128 mImpl->ShowClipboard();
2131 case Toolkit::TextSelectionPopup::NONE:
2139 // private : Update.
2141 void Controller::InsertText( const std::string& text, Controller::InsertType type )
2143 bool removedPrevious( false );
2144 bool maxLengthReached( false );
2146 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
2148 if( NULL == mImpl->mEventData )
2153 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
2154 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
2155 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2157 // TODO: At the moment the underline runs are only for pre-edit.
2158 mImpl->mVisualModel->mUnderlineRuns.Clear();
2160 // Keep the current number of characters.
2161 const Length currentNumberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2163 // Remove the previous IMF pre-edit.
2164 if( mImpl->mEventData->mPreEditFlag && ( 0u != mImpl->mEventData->mPreEditLength ) )
2166 removedPrevious = RemoveText( -static_cast<int>( mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition ),
2167 mImpl->mEventData->mPreEditLength,
2168 DONT_UPDATE_INPUT_STYLE );
2170 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
2171 mImpl->mEventData->mPreEditLength = 0u;
2175 // Remove the previous Selection.
2176 removedPrevious = RemoveSelectedText();
2179 Vector<Character> utf32Characters;
2180 Length characterCount = 0u;
2184 // Convert text into UTF-32
2185 utf32Characters.Resize( text.size() );
2187 // This is a bit horrible but std::string returns a (signed) char*
2188 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
2190 // Transform a text array encoded in utf8 into an array encoded in utf32.
2191 // It returns the actual number of characters.
2192 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
2193 utf32Characters.Resize( characterCount );
2195 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
2196 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
2199 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
2201 // The placeholder text is no longer needed
2202 if( mImpl->IsShowingPlaceholderText() )
2207 mImpl->ChangeState( EventData::EDITING );
2209 // Handle the IMF (predicitive text) state changes
2210 if( COMMIT == type )
2212 // IMF manager is no longer handling key-events
2213 mImpl->ClearPreEditFlag();
2217 if( !mImpl->mEventData->mPreEditFlag )
2219 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state\n" );
2221 // Record the start of the pre-edit text
2222 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
2225 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
2226 mImpl->mEventData->mPreEditFlag = true;
2228 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2231 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
2233 // Restrict new text to fit within Maximum characters setting.
2234 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
2235 maxLengthReached = ( characterCount > maxSizeOfNewText );
2237 // The cursor position.
2238 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
2240 // Update the text's style.
2242 // Updates the text style runs by adding characters.
2243 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
2245 // Get the character index from the cursor index.
2246 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
2248 // Retrieve the text's style for the given index.
2250 mImpl->RetrieveDefaultInputStyle( style );
2251 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
2253 // Whether to add a new text color run.
2254 const bool addColorRun = ( style.textColor != mImpl->mEventData->mInputStyle.textColor );
2256 // Whether to add a new font run.
2257 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
2258 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
2259 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
2260 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
2261 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
2266 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
2267 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
2269 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
2270 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
2271 colorRun.characterRun.characterIndex = cursorIndex;
2272 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2275 if( addFontNameRun ||
2281 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
2282 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
2284 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
2286 if( addFontNameRun )
2288 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
2289 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
2290 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
2291 fontDescriptionRun.familyDefined = true;
2293 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
2296 if( addFontWeightRun )
2298 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
2299 fontDescriptionRun.weightDefined = true;
2302 if( addFontWidthRun )
2304 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
2305 fontDescriptionRun.widthDefined = true;
2308 if( addFontSlantRun )
2310 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
2311 fontDescriptionRun.slantDefined = true;
2314 if( addFontSizeRun )
2316 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
2317 fontDescriptionRun.sizeDefined = true;
2320 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
2321 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2324 // Insert at current cursor position.
2325 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
2327 if( cursorIndex < numberOfCharactersInModel )
2329 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2333 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2336 // Mark the first paragraph to be updated.
2337 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
2338 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
2340 // Update the cursor index.
2341 cursorIndex += maxSizeOfNewText;
2343 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
2346 const Length numberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2348 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
2349 mImpl->IsPlaceholderAvailable() )
2351 // Show place-holder if empty after removing the pre-edit text
2352 ShowPlaceholderText();
2353 mImpl->mEventData->mUpdateCursorPosition = true;
2354 mImpl->ClearPreEditFlag();
2356 else if( removedPrevious ||
2357 ( 0 != utf32Characters.Count() ) )
2359 // Queue an inserted event
2360 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
2362 mImpl->mEventData->mUpdateCursorPosition = true;
2363 if( numberOfCharacters < currentNumberOfCharacters )
2365 mImpl->mEventData->mScrollAfterDelete = true;
2369 mImpl->mEventData->mScrollAfterUpdatePosition = true;
2373 if( maxLengthReached )
2375 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
2377 mImpl->ResetImfManager();
2379 // Do this last since it provides callbacks into application code
2380 mImpl->mControlInterface.MaxLengthReached();
2384 void Controller::PasteText( const std::string& stringToPaste )
2386 InsertText( stringToPaste, Text::Controller::COMMIT );
2387 mImpl->ChangeState( EventData::EDITING );
2388 mImpl->RequestRelayout();
2390 // Do this last since it provides callbacks into application code
2391 mImpl->mControlInterface.TextChanged();
2394 bool Controller::RemoveText( int cursorOffset,
2395 int numberOfCharacters,
2396 UpdateInputStyleType type )
2398 bool removed = false;
2400 if( NULL == mImpl->mEventData )
2405 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfCharacters %d\n",
2406 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters );
2408 if( !mImpl->IsShowingPlaceholderText() )
2410 // Delete at current cursor position
2411 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
2412 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
2414 CharacterIndex cursorIndex = oldCursorIndex;
2416 // Validate the cursor position & number of characters
2417 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
2419 cursorIndex = oldCursorIndex + cursorOffset;
2422 if( ( cursorIndex + numberOfCharacters ) > currentText.Count() )
2424 numberOfCharacters = currentText.Count() - cursorIndex;
2427 if( ( cursorIndex + numberOfCharacters ) <= mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters )
2429 // Mark the paragraphs to be updated.
2430 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
2431 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove += numberOfCharacters;
2433 // Update the input style and remove the text's style before removing the text.
2435 if( UPDATE_INPUT_STYLE == type )
2437 // Keep a copy of the current input style.
2438 InputStyle currentInputStyle;
2439 currentInputStyle.Copy( mImpl->mEventData->mInputStyle );
2441 // Set first the default input style.
2442 mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
2444 // Update the input style.
2445 mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
2447 // Compare if the input style has changed.
2448 const bool hasInputStyleChanged = !currentInputStyle.Equal( mImpl->mEventData->mInputStyle );
2450 if( hasInputStyleChanged )
2452 const InputStyle::Mask styleChangedMask = currentInputStyle.GetInputStyleChangeMask( mImpl->mEventData->mInputStyle );
2453 // Queue the input style changed signal.
2454 mImpl->mEventData->mInputStyleChangedQueue.PushBack( styleChangedMask );
2458 // Updates the text style runs by removing characters. Runs with no characters are removed.
2459 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfCharacters );
2461 // Remove the characters.
2462 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
2463 Vector<Character>::Iterator last = first + numberOfCharacters;
2465 currentText.Erase( first, last );
2467 // Cursor position retreat
2468 oldCursorIndex = cursorIndex;
2470 mImpl->mEventData->mScrollAfterDelete = true;
2472 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfCharacters );
2480 bool Controller::RemoveSelectedText()
2482 bool textRemoved( false );
2484 if( EventData::SELECTING == mImpl->mEventData->mState )
2486 std::string removedString;
2487 mImpl->RetrieveSelection( removedString, true );
2489 if( !removedString.empty() )
2492 mImpl->ChangeState( EventData::EDITING );
2499 // private : Relayout.
2501 bool Controller::DoRelayout( const Size& size,
2502 OperationsMask operationsRequired,
2505 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
2506 bool viewUpdated( false );
2508 // Calculate the operations to be done.
2509 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
2511 const CharacterIndex startIndex = mImpl->mTextUpdateInfo.mParagraphCharacterIndex;
2512 const Length requestedNumberOfCharacters = mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters;
2514 // Get the current layout size.
2515 layoutSize = mImpl->mVisualModel->GetLayoutSize();
2517 if( NO_OPERATION != ( LAYOUT & operations ) )
2519 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
2521 // Some vectors with data needed to layout and reorder may be void
2522 // after the first time the text has been laid out.
2523 // Fill the vectors again.
2525 // Calculate the number of glyphs to layout.
2526 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
2527 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
2528 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
2529 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
2531 const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
2532 const GlyphIndex startGlyphIndex = mImpl->mTextUpdateInfo.mStartGlyphIndex;
2533 const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
2534 const Length totalNumberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
2536 if( 0u == totalNumberOfGlyphs )
2538 if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
2540 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
2543 // Nothing else to do if there is no glyphs.
2544 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
2548 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
2549 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
2550 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
2551 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
2552 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
2553 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
2554 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
2556 // Set the layout parameters.
2557 LayoutParameters layoutParameters( size,
2559 lineBreakInfo.Begin(),
2560 wordBreakInfo.Begin(),
2561 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
2563 glyphsToCharactersMap.Begin(),
2564 charactersPerGlyph.Begin(),
2565 charactersToGlyphBuffer,
2566 glyphsPerCharacterBuffer,
2567 totalNumberOfGlyphs );
2569 // Resize the vector of positions to have the same size than the vector of glyphs.
2570 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
2571 glyphPositions.Resize( totalNumberOfGlyphs );
2573 // Whether the last character is a new paragraph character.
2574 mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
2575 layoutParameters.isLastNewParagraph = mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph;
2577 // The initial glyph and the number of glyphs to layout.
2578 layoutParameters.startGlyphIndex = startGlyphIndex;
2579 layoutParameters.numberOfGlyphs = numberOfGlyphs;
2580 layoutParameters.startLineIndex = mImpl->mTextUpdateInfo.mStartLineIndex;
2581 layoutParameters.estimatedNumberOfLines = mImpl->mTextUpdateInfo.mEstimatedNumberOfLines;
2583 // Update the visual model.
2585 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
2587 mImpl->mVisualModel->mLines,
2590 viewUpdated = viewUpdated || ( newLayoutSize != layoutSize );
2594 layoutSize = newLayoutSize;
2596 if ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) )
2598 mImpl->mAutoScrollDirectionRTL = false;
2601 // Reorder the lines
2602 if( NO_OPERATION != ( REORDER & operations ) )
2604 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
2605 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
2607 // Check first if there are paragraphs with bidirectional info.
2608 if( 0u != bidirectionalInfo.Count() )
2611 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
2613 // Reorder the lines.
2614 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
2615 ReorderLines( bidirectionalInfo,
2617 requestedNumberOfCharacters,
2618 mImpl->mVisualModel->mLines,
2619 bidirectionalLineInfo );
2621 // Set the bidirectional info per line into the layout parameters.
2622 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
2623 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
2625 // Re-layout the text. Reorder those lines with right to left characters.
2626 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
2628 requestedNumberOfCharacters,
2631 if ( ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) ) && ( numberOfLines > 0 ) )
2633 const LineRun* const firstline = mImpl->mVisualModel->mLines.Begin();
2636 mImpl->mAutoScrollDirectionRTL = firstline->direction;
2642 // Sets the layout size.
2643 if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
2645 mImpl->mVisualModel->SetLayoutSize( layoutSize );
2649 // Store the size used to layout the text.
2650 mImpl->mVisualModel->mControlSize = size;
2653 if( NO_OPERATION != ( ALIGN & operations ) )
2655 // The laid-out lines.
2656 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
2658 mImpl->mLayoutEngine.Align( size,
2660 requestedNumberOfCharacters,
2665 #if defined(DEBUG_ENABLED)
2666 std::string currentText;
2667 GetText( currentText );
2668 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mAutoScrollDirectionRTL[%s] [%s]\n", this, (mImpl->mAutoScrollDirectionRTL)?"true":"false", currentText.c_str() );
2670 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
2674 void Controller::CalculateVerticalOffset( const Size& controlSize )
2676 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
2678 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
2680 // Get the line height of the default font.
2681 layoutSize.height = mImpl->GetDefaultFontLineHeight();
2684 switch( mImpl->mLayoutEngine.GetVerticalAlignment() )
2686 case LayoutEngine::VERTICAL_ALIGN_TOP:
2688 mImpl->mScrollPosition.y = 0.f;
2691 case LayoutEngine::VERTICAL_ALIGN_CENTER:
2693 mImpl->mScrollPosition.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
2696 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
2698 mImpl->mScrollPosition.y = controlSize.height - layoutSize.height;
2704 // private : Events.
2706 void Controller::ProcessModifyEvents()
2708 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
2710 if( 0u == events.Count() )
2716 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
2717 endIt = events.End();
2721 const ModifyEvent& event = *it;
2723 if( ModifyEvent::TEXT_REPLACED == event.type )
2725 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
2726 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
2728 TextReplacedEvent();
2730 else if( ModifyEvent::TEXT_INSERTED == event.type )
2732 TextInsertedEvent();
2734 else if( ModifyEvent::TEXT_DELETED == event.type )
2736 // Placeholder-text cannot be deleted
2737 if( !mImpl->IsShowingPlaceholderText() )
2744 if( NULL != mImpl->mEventData )
2746 // When the text is being modified, delay cursor blinking
2747 mImpl->mEventData->mDecorator->DelayCursorBlink();
2750 // Discard temporary text
2754 void Controller::TextReplacedEvent()
2756 // The natural size needs to be re-calculated.
2757 mImpl->mRecalculateNaturalSize = true;
2759 // Apply modifications to the model
2760 mImpl->mOperationsPending = ALL_OPERATIONS;
2763 void Controller::TextInsertedEvent()
2765 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
2767 if( NULL == mImpl->mEventData )
2772 // The natural size needs to be re-calculated.
2773 mImpl->mRecalculateNaturalSize = true;
2775 // Apply modifications to the model; TODO - Optimize this
2776 mImpl->mOperationsPending = ALL_OPERATIONS;
2779 void Controller::TextDeletedEvent()
2781 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
2783 if( NULL == mImpl->mEventData )
2788 // The natural size needs to be re-calculated.
2789 mImpl->mRecalculateNaturalSize = true;
2791 // Apply modifications to the model; TODO - Optimize this
2792 mImpl->mOperationsPending = ALL_OPERATIONS;
2795 void Controller::SelectEvent( float x, float y, bool selectAll )
2797 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2799 if( NULL != mImpl->mEventData )
2803 Event event( Event::SELECT_ALL );
2804 mImpl->mEventData->mEventQueue.push_back( event );
2808 Event event( Event::SELECT );
2809 event.p2.mFloat = x;
2810 event.p3.mFloat = y;
2811 mImpl->mEventData->mEventQueue.push_back( event );
2814 mImpl->RequestRelayout();
2818 bool Controller::BackspaceKeyEvent()
2820 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2822 bool removed = false;
2824 if( NULL == mImpl->mEventData )
2829 // IMF manager is no longer handling key-events
2830 mImpl->ClearPreEditFlag();
2832 if( EventData::SELECTING == mImpl->mEventData->mState )
2834 removed = RemoveSelectedText();
2836 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2838 // Remove the character before the current cursor position
2839 removed = RemoveText( -1,
2841 UPDATE_INPUT_STYLE );
2846 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2847 !mImpl->IsPlaceholderAvailable() )
2849 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2853 ShowPlaceholderText();
2855 mImpl->mEventData->mUpdateCursorPosition = true;
2856 mImpl->mEventData->mScrollAfterDelete = true;
2862 // private : Helpers.
2864 void Controller::ResetText()
2867 mImpl->mLogicalModel->mText.Clear();
2869 // We have cleared everything including the placeholder-text
2870 mImpl->PlaceholderCleared();
2872 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2873 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2874 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
2876 // Clear any previous text.
2877 mImpl->mTextUpdateInfo.mClearAll = true;
2879 // The natural size needs to be re-calculated.
2880 mImpl->mRecalculateNaturalSize = true;
2882 // Apply modifications to the model
2883 mImpl->mOperationsPending = ALL_OPERATIONS;
2886 void Controller::ShowPlaceholderText()
2888 if( mImpl->IsPlaceholderAvailable() )
2890 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2892 if( NULL == mImpl->mEventData )
2897 mImpl->mEventData->mIsShowingPlaceholderText = true;
2899 // Disable handles when showing place-holder text
2900 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2901 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2902 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2904 const char* text( NULL );
2907 // TODO - Switch placeholder text styles when changing state
2908 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2909 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2911 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2912 size = mImpl->mEventData->mPlaceholderTextActive.size();
2916 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2917 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2920 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2921 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2923 // Reset model for showing placeholder.
2924 mImpl->mLogicalModel->mText.Clear();
2925 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2927 // Convert text into UTF-32
2928 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2929 utf32Characters.Resize( size );
2931 // This is a bit horrible but std::string returns a (signed) char*
2932 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2934 // Transform a text array encoded in utf8 into an array encoded in utf32.
2935 // It returns the actual number of characters.
2936 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2937 utf32Characters.Resize( characterCount );
2939 // The characters to be added.
2940 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = characterCount;
2942 // Reset the cursor position
2943 mImpl->mEventData->mPrimaryCursorPosition = 0;
2945 // The natural size needs to be re-calculated.
2946 mImpl->mRecalculateNaturalSize = true;
2948 // Apply modifications to the model
2949 mImpl->mOperationsPending = ALL_OPERATIONS;
2951 // Update the rest of the model during size negotiation
2952 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2956 void Controller::ClearFontData()
2958 if( mImpl->mFontDefaults )
2960 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2963 // Set flags to update the model.
2964 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2965 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2966 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
2968 mImpl->mTextUpdateInfo.mClearAll = true;
2969 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
2970 mImpl->mRecalculateNaturalSize = true;
2972 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2977 UPDATE_LAYOUT_SIZE |
2982 void Controller::ClearStyleData()
2984 mImpl->mLogicalModel->mColorRuns.Clear();
2985 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2988 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
2990 // Reset the cursor position
2991 if( NULL != mImpl->mEventData )
2993 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
2995 // Update the cursor if it's in editing mode.
2996 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
2998 mImpl->mEventData->mUpdateCursorPosition = true;
3003 void Controller::ResetScrollPosition()
3005 if( NULL != mImpl->mEventData )
3007 // Reset the scroll position.
3008 mImpl->mScrollPosition = Vector2::ZERO;
3009 mImpl->mEventData->mScrollAfterUpdatePosition = true;
3013 // private : Private contructors & copy operator.
3015 Controller::Controller( ControlInterface& controlInterface )
3018 mImpl = new Controller::Impl( controlInterface );
3021 // The copy constructor and operator are left unimplemented.
3023 // protected : Destructor.
3025 Controller::~Controller()
3032 } // namespace Toolkit