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.
1629 mImpl->NotifyImfMultiLineStatus();
1630 if( mImpl->IsShowingPlaceholderText() )
1632 // Show alternative placeholder-text when editing
1633 ShowPlaceholderText();
1636 mImpl->RequestRelayout();
1640 void Controller::KeyboardFocusLostEvent()
1642 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1644 if( NULL != mImpl->mEventData )
1646 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1648 mImpl->ChangeState( EventData::INACTIVE );
1650 if( !mImpl->IsShowingRealText() )
1652 // Revert to regular placeholder-text when not editing
1653 ShowPlaceholderText();
1657 mImpl->RequestRelayout();
1660 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1662 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1664 bool textChanged( false );
1666 if( ( NULL != mImpl->mEventData ) &&
1667 ( keyEvent.state == KeyEvent::Down ) )
1669 int keyCode = keyEvent.keyCode;
1670 const std::string& keyString = keyEvent.keyPressed;
1672 // Pre-process to separate modifying events from non-modifying input events.
1673 if( Dali::DALI_KEY_ESCAPE == keyCode )
1675 // Escape key is a special case which causes focus loss
1676 KeyboardFocusLostEvent();
1678 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
1679 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1680 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
1681 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
1683 Event event( Event::CURSOR_KEY_EVENT );
1684 event.p1.mInt = keyCode;
1685 mImpl->mEventData->mEventQueue.push_back( event );
1687 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1689 textChanged = BackspaceKeyEvent();
1691 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1693 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1694 // Avoids calling the InsertText() method which can delete selected text
1696 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1697 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1699 mImpl->ChangeState( EventData::INACTIVE );
1700 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1701 // Avoids calling the InsertText() method which can delete selected text
1703 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1705 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1706 // and a character is typed after the type of a upper case latin character.
1712 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1714 // IMF manager is no longer handling key-events
1715 mImpl->ClearPreEditFlag();
1717 InsertText( keyString, COMMIT );
1721 if ( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1722 ( mImpl->mEventData->mState != EventData::INACTIVE ) &&
1723 ( Dali::DALI_KEY_SHIFT_LEFT != keyCode ) )
1725 // Should not change the state if the key is the shift send by the imf manager.
1726 // Otherwise, when the state is SELECTING the text controller can't send the right
1727 // surrounding info to the imf.
1728 mImpl->ChangeState( EventData::EDITING );
1731 mImpl->RequestRelayout();
1736 // Do this last since it provides callbacks into application code
1737 mImpl->mControlInterface.TextChanged();
1743 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1745 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1747 if( NULL != mImpl->mEventData )
1749 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
1750 EventData::State state( mImpl->mEventData->mState );
1751 bool relayoutNeeded( false ); // to avoid unnecessary relayouts when tapping an empty text-field
1753 if( mImpl->IsClipboardVisible() )
1755 if( EventData::INACTIVE == state || EventData::EDITING == state)
1757 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
1759 relayoutNeeded = true;
1761 else if( 1u == tapCount )
1763 if( EventData::EDITING_WITH_POPUP == state || EventData::EDITING_WITH_PASTE_POPUP == state )
1765 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE ); // If Popup shown hide it here so can be shown again if required.
1768 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != state ) )
1770 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
1771 relayoutNeeded = true;
1775 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
1777 // Hide placeholder text
1781 if( EventData::INACTIVE == state )
1783 mImpl->ChangeState( EventData::EDITING );
1785 else if( !mImpl->IsClipboardEmpty() )
1787 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
1789 relayoutNeeded = true;
1792 else if( 2u == tapCount )
1794 if( mImpl->mEventData->mSelectionEnabled &&
1795 mImpl->IsShowingRealText() )
1797 SelectEvent( x, y, false );
1800 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
1801 if( relayoutNeeded )
1803 Event event( Event::TAP_EVENT );
1804 event.p1.mUint = tapCount;
1805 event.p2.mFloat = x;
1806 event.p3.mFloat = y;
1807 mImpl->mEventData->mEventQueue.push_back( event );
1809 mImpl->RequestRelayout();
1813 // Reset keyboard as tap event has occurred.
1814 mImpl->ResetImfManager();
1817 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
1819 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1821 if( NULL != mImpl->mEventData )
1823 Event event( Event::PAN_EVENT );
1824 event.p1.mInt = state;
1825 event.p2.mFloat = displacement.x;
1826 event.p3.mFloat = displacement.y;
1827 mImpl->mEventData->mEventQueue.push_back( event );
1829 mImpl->RequestRelayout();
1833 void Controller::LongPressEvent( Gesture::State state, float x, float y )
1835 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
1837 if( ( state == Gesture::Started ) &&
1838 ( NULL != mImpl->mEventData ) )
1840 if( !mImpl->IsShowingRealText() )
1842 Event event( Event::LONG_PRESS_EVENT );
1843 event.p1.mInt = state;
1844 mImpl->mEventData->mEventQueue.push_back( event );
1845 mImpl->RequestRelayout();
1849 // The 1st long-press on inactive text-field is treated as tap
1850 if( EventData::INACTIVE == mImpl->mEventData->mState )
1852 mImpl->ChangeState( EventData::EDITING );
1854 Event event( Event::TAP_EVENT );
1856 event.p2.mFloat = x;
1857 event.p3.mFloat = y;
1858 mImpl->mEventData->mEventQueue.push_back( event );
1860 mImpl->RequestRelayout();
1862 else if( !mImpl->IsClipboardVisible() )
1864 // Reset the imf manger to commit the pre-edit before selecting the text.
1865 mImpl->ResetImfManager();
1867 SelectEvent( x, y, false );
1873 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
1875 // Whether the text needs to be relaid-out.
1876 bool requestRelayout = false;
1878 // Whether to retrieve the text and cursor position to be sent to the IMF manager.
1879 bool retrieveText = false;
1880 bool retrieveCursor = false;
1882 switch( imfEvent.eventName )
1884 case ImfManager::COMMIT:
1886 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
1887 requestRelayout = true;
1888 retrieveCursor = true;
1891 case ImfManager::PREEDIT:
1893 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
1894 requestRelayout = true;
1895 retrieveCursor = true;
1898 case ImfManager::DELETESURROUNDING:
1900 const bool textDeleted = RemoveText( imfEvent.cursorOffset,
1901 imfEvent.numberOfChars,
1902 DONT_UPDATE_INPUT_STYLE );
1906 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
1907 !mImpl->IsPlaceholderAvailable() )
1909 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1913 ShowPlaceholderText();
1915 mImpl->mEventData->mUpdateCursorPosition = true;
1916 mImpl->mEventData->mScrollAfterDelete = true;
1918 requestRelayout = true;
1922 case ImfManager::GETSURROUNDING:
1924 retrieveText = true;
1925 retrieveCursor = true;
1928 case ImfManager::VOID:
1935 if( requestRelayout )
1937 mImpl->mOperationsPending = ALL_OPERATIONS;
1938 mImpl->RequestRelayout();
1942 CharacterIndex cursorPosition = 0u;
1943 Length numberOfWhiteSpaces = 0u;
1945 if( retrieveCursor )
1947 numberOfWhiteSpaces = mImpl->GetNumberOfWhiteSpaces( 0u );
1949 cursorPosition = mImpl->GetLogicalCursorPosition();
1951 if( cursorPosition < numberOfWhiteSpaces )
1953 cursorPosition = 0u;
1957 cursorPosition -= numberOfWhiteSpaces;
1963 mImpl->GetText( numberOfWhiteSpaces, text );
1966 ImfManager::ImfCallbackData callbackData( ( retrieveText || retrieveCursor ), cursorPosition, text, false );
1968 if( requestRelayout )
1970 // Do this last since it provides callbacks into application code
1971 mImpl->mControlInterface.TextChanged();
1974 return callbackData;
1977 void Controller::PasteClipboardItemEvent()
1979 // Retrieve the clipboard contents first
1980 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
1981 std::string stringToPaste( notifier.GetContent() );
1983 // Commit the current pre-edit text; the contents of the clipboard should be appended
1984 mImpl->ResetImfManager();
1986 // Temporary disable hiding clipboard
1987 mImpl->SetClipboardHideEnable( false );
1990 PasteText( stringToPaste );
1992 mImpl->SetClipboardHideEnable( true );
1995 // protected : Inherit from Text::Decorator::ControllerInterface.
1997 void Controller::GetTargetSize( Vector2& targetSize )
1999 targetSize = mImpl->mVisualModel->mControlSize;
2002 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2004 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2007 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2009 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2011 if( NULL != mImpl->mEventData )
2013 switch( handleType )
2017 Event event( Event::GRAB_HANDLE_EVENT );
2018 event.p1.mUint = state;
2019 event.p2.mFloat = x;
2020 event.p3.mFloat = y;
2022 mImpl->mEventData->mEventQueue.push_back( event );
2025 case LEFT_SELECTION_HANDLE:
2027 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2028 event.p1.mUint = state;
2029 event.p2.mFloat = x;
2030 event.p3.mFloat = y;
2032 mImpl->mEventData->mEventQueue.push_back( event );
2035 case RIGHT_SELECTION_HANDLE:
2037 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2038 event.p1.mUint = state;
2039 event.p2.mFloat = x;
2040 event.p3.mFloat = y;
2042 mImpl->mEventData->mEventQueue.push_back( event );
2045 case LEFT_SELECTION_HANDLE_MARKER:
2046 case RIGHT_SELECTION_HANDLE_MARKER:
2048 // Markers do not move the handles.
2051 case HANDLE_TYPE_COUNT:
2053 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2057 mImpl->RequestRelayout();
2061 // protected : Inherit from TextSelectionPopup::TextPopupButtonCallbackInterface.
2063 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2065 if( NULL == mImpl->mEventData )
2072 case Toolkit::TextSelectionPopup::CUT:
2074 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2075 mImpl->mOperationsPending = ALL_OPERATIONS;
2077 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2078 !mImpl->IsPlaceholderAvailable() )
2080 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2084 ShowPlaceholderText();
2087 mImpl->mEventData->mUpdateCursorPosition = true;
2088 mImpl->mEventData->mScrollAfterDelete = true;
2090 mImpl->RequestRelayout();
2091 mImpl->mControlInterface.TextChanged();
2094 case Toolkit::TextSelectionPopup::COPY:
2096 mImpl->SendSelectionToClipboard( false ); // Text not modified
2098 mImpl->mEventData->mUpdateCursorPosition = true;
2100 mImpl->RequestRelayout(); // Cursor, Handles, Selection Highlight, Popup
2103 case Toolkit::TextSelectionPopup::PASTE:
2105 mImpl->RequestGetTextFromClipboard(); // Request clipboard service to retrieve an item
2108 case Toolkit::TextSelectionPopup::SELECT:
2110 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2112 if( mImpl->mEventData->mSelectionEnabled )
2114 // Creates a SELECT event.
2115 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2119 case Toolkit::TextSelectionPopup::SELECT_ALL:
2121 // Creates a SELECT_ALL event
2122 SelectEvent( 0.f, 0.f, true );
2125 case Toolkit::TextSelectionPopup::CLIPBOARD:
2127 mImpl->ShowClipboard();
2130 case Toolkit::TextSelectionPopup::NONE:
2138 // private : Update.
2140 void Controller::InsertText( const std::string& text, Controller::InsertType type )
2142 bool removedPrevious( false );
2143 bool maxLengthReached( false );
2145 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
2147 if( NULL == mImpl->mEventData )
2152 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
2153 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
2154 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2156 // TODO: At the moment the underline runs are only for pre-edit.
2157 mImpl->mVisualModel->mUnderlineRuns.Clear();
2159 // Keep the current number of characters.
2160 const Length currentNumberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2162 // Remove the previous IMF pre-edit.
2163 if( mImpl->mEventData->mPreEditFlag && ( 0u != mImpl->mEventData->mPreEditLength ) )
2165 removedPrevious = RemoveText( -static_cast<int>( mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition ),
2166 mImpl->mEventData->mPreEditLength,
2167 DONT_UPDATE_INPUT_STYLE );
2169 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
2170 mImpl->mEventData->mPreEditLength = 0u;
2174 // Remove the previous Selection.
2175 removedPrevious = RemoveSelectedText();
2178 Vector<Character> utf32Characters;
2179 Length characterCount = 0u;
2183 // Convert text into UTF-32
2184 utf32Characters.Resize( text.size() );
2186 // This is a bit horrible but std::string returns a (signed) char*
2187 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
2189 // Transform a text array encoded in utf8 into an array encoded in utf32.
2190 // It returns the actual number of characters.
2191 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
2192 utf32Characters.Resize( characterCount );
2194 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
2195 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
2198 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
2200 // The placeholder text is no longer needed
2201 if( mImpl->IsShowingPlaceholderText() )
2206 mImpl->ChangeState( EventData::EDITING );
2208 // Handle the IMF (predicitive text) state changes
2209 if( COMMIT == type )
2211 // IMF manager is no longer handling key-events
2212 mImpl->ClearPreEditFlag();
2216 if( !mImpl->mEventData->mPreEditFlag )
2218 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state\n" );
2220 // Record the start of the pre-edit text
2221 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
2224 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
2225 mImpl->mEventData->mPreEditFlag = true;
2227 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2230 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
2232 // Restrict new text to fit within Maximum characters setting.
2233 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
2234 maxLengthReached = ( characterCount > maxSizeOfNewText );
2236 // The cursor position.
2237 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
2239 // Update the text's style.
2241 // Updates the text style runs by adding characters.
2242 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
2244 // Get the character index from the cursor index.
2245 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
2247 // Retrieve the text's style for the given index.
2249 mImpl->RetrieveDefaultInputStyle( style );
2250 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
2252 // Whether to add a new text color run.
2253 const bool addColorRun = ( style.textColor != mImpl->mEventData->mInputStyle.textColor );
2255 // Whether to add a new font run.
2256 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
2257 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
2258 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
2259 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
2260 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
2265 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
2266 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
2268 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
2269 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
2270 colorRun.characterRun.characterIndex = cursorIndex;
2271 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2274 if( addFontNameRun ||
2280 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
2281 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
2283 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
2285 if( addFontNameRun )
2287 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
2288 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
2289 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
2290 fontDescriptionRun.familyDefined = true;
2292 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
2295 if( addFontWeightRun )
2297 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
2298 fontDescriptionRun.weightDefined = true;
2301 if( addFontWidthRun )
2303 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
2304 fontDescriptionRun.widthDefined = true;
2307 if( addFontSlantRun )
2309 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
2310 fontDescriptionRun.slantDefined = true;
2313 if( addFontSizeRun )
2315 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
2316 fontDescriptionRun.sizeDefined = true;
2319 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
2320 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2323 // Insert at current cursor position.
2324 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
2326 if( cursorIndex < numberOfCharactersInModel )
2328 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2332 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2335 // Mark the first paragraph to be updated.
2336 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
2337 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
2339 // Update the cursor index.
2340 cursorIndex += maxSizeOfNewText;
2342 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
2345 const Length numberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2347 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
2348 mImpl->IsPlaceholderAvailable() )
2350 // Show place-holder if empty after removing the pre-edit text
2351 ShowPlaceholderText();
2352 mImpl->mEventData->mUpdateCursorPosition = true;
2353 mImpl->ClearPreEditFlag();
2355 else if( removedPrevious ||
2356 ( 0 != utf32Characters.Count() ) )
2358 // Queue an inserted event
2359 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
2361 mImpl->mEventData->mUpdateCursorPosition = true;
2362 if( numberOfCharacters < currentNumberOfCharacters )
2364 mImpl->mEventData->mScrollAfterDelete = true;
2368 mImpl->mEventData->mScrollAfterUpdatePosition = true;
2372 if( maxLengthReached )
2374 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
2376 mImpl->ResetImfManager();
2378 // Do this last since it provides callbacks into application code
2379 mImpl->mControlInterface.MaxLengthReached();
2383 void Controller::PasteText( const std::string& stringToPaste )
2385 InsertText( stringToPaste, Text::Controller::COMMIT );
2386 mImpl->ChangeState( EventData::EDITING );
2387 mImpl->RequestRelayout();
2389 // Do this last since it provides callbacks into application code
2390 mImpl->mControlInterface.TextChanged();
2393 bool Controller::RemoveText( int cursorOffset,
2394 int numberOfCharacters,
2395 UpdateInputStyleType type )
2397 bool removed = false;
2399 if( NULL == mImpl->mEventData )
2404 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfCharacters %d\n",
2405 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters );
2407 if( !mImpl->IsShowingPlaceholderText() )
2409 // Delete at current cursor position
2410 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
2411 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
2413 CharacterIndex cursorIndex = oldCursorIndex;
2415 // Validate the cursor position & number of characters
2416 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
2418 cursorIndex = oldCursorIndex + cursorOffset;
2421 if( ( cursorIndex + numberOfCharacters ) > currentText.Count() )
2423 numberOfCharacters = currentText.Count() - cursorIndex;
2426 if( ( cursorIndex + numberOfCharacters ) <= mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters )
2428 // Mark the paragraphs to be updated.
2429 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
2430 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove += numberOfCharacters;
2432 // Update the input style and remove the text's style before removing the text.
2434 if( UPDATE_INPUT_STYLE == type )
2436 // Keep a copy of the current input style.
2437 InputStyle currentInputStyle;
2438 currentInputStyle.Copy( mImpl->mEventData->mInputStyle );
2440 // Set first the default input style.
2441 mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
2443 // Update the input style.
2444 mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
2446 // Compare if the input style has changed.
2447 const bool hasInputStyleChanged = !currentInputStyle.Equal( mImpl->mEventData->mInputStyle );
2449 if( hasInputStyleChanged )
2451 const InputStyle::Mask styleChangedMask = currentInputStyle.GetInputStyleChangeMask( mImpl->mEventData->mInputStyle );
2452 // Queue the input style changed signal.
2453 mImpl->mEventData->mInputStyleChangedQueue.PushBack( styleChangedMask );
2457 // Updates the text style runs by removing characters. Runs with no characters are removed.
2458 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfCharacters );
2460 // Remove the characters.
2461 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
2462 Vector<Character>::Iterator last = first + numberOfCharacters;
2464 currentText.Erase( first, last );
2466 // Cursor position retreat
2467 oldCursorIndex = cursorIndex;
2469 mImpl->mEventData->mScrollAfterDelete = true;
2471 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfCharacters );
2479 bool Controller::RemoveSelectedText()
2481 bool textRemoved( false );
2483 if( EventData::SELECTING == mImpl->mEventData->mState )
2485 std::string removedString;
2486 mImpl->RetrieveSelection( removedString, true );
2488 if( !removedString.empty() )
2491 mImpl->ChangeState( EventData::EDITING );
2498 // private : Relayout.
2500 bool Controller::DoRelayout( const Size& size,
2501 OperationsMask operationsRequired,
2504 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
2505 bool viewUpdated( false );
2507 // Calculate the operations to be done.
2508 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
2510 const CharacterIndex startIndex = mImpl->mTextUpdateInfo.mParagraphCharacterIndex;
2511 const Length requestedNumberOfCharacters = mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters;
2513 // Get the current layout size.
2514 layoutSize = mImpl->mVisualModel->GetLayoutSize();
2516 if( NO_OPERATION != ( LAYOUT & operations ) )
2518 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
2520 // Some vectors with data needed to layout and reorder may be void
2521 // after the first time the text has been laid out.
2522 // Fill the vectors again.
2524 // Calculate the number of glyphs to layout.
2525 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
2526 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
2527 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
2528 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
2530 const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
2531 const GlyphIndex startGlyphIndex = mImpl->mTextUpdateInfo.mStartGlyphIndex;
2532 const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
2533 const Length totalNumberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
2535 if( 0u == totalNumberOfGlyphs )
2537 if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
2539 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
2542 // Nothing else to do if there is no glyphs.
2543 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
2547 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
2548 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
2549 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
2550 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
2551 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
2552 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
2553 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
2555 // Set the layout parameters.
2556 LayoutParameters layoutParameters( size,
2558 lineBreakInfo.Begin(),
2559 wordBreakInfo.Begin(),
2560 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
2562 glyphsToCharactersMap.Begin(),
2563 charactersPerGlyph.Begin(),
2564 charactersToGlyphBuffer,
2565 glyphsPerCharacterBuffer,
2566 totalNumberOfGlyphs );
2568 // Resize the vector of positions to have the same size than the vector of glyphs.
2569 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
2570 glyphPositions.Resize( totalNumberOfGlyphs );
2572 // Whether the last character is a new paragraph character.
2573 mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
2574 layoutParameters.isLastNewParagraph = mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph;
2576 // The initial glyph and the number of glyphs to layout.
2577 layoutParameters.startGlyphIndex = startGlyphIndex;
2578 layoutParameters.numberOfGlyphs = numberOfGlyphs;
2579 layoutParameters.startLineIndex = mImpl->mTextUpdateInfo.mStartLineIndex;
2580 layoutParameters.estimatedNumberOfLines = mImpl->mTextUpdateInfo.mEstimatedNumberOfLines;
2582 // Update the visual model.
2584 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
2586 mImpl->mVisualModel->mLines,
2589 viewUpdated = viewUpdated || ( newLayoutSize != layoutSize );
2593 layoutSize = newLayoutSize;
2595 if ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) )
2597 mImpl->mAutoScrollDirectionRTL = false;
2600 // Reorder the lines
2601 if( NO_OPERATION != ( REORDER & operations ) )
2603 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
2604 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
2606 // Check first if there are paragraphs with bidirectional info.
2607 if( 0u != bidirectionalInfo.Count() )
2610 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
2612 // Reorder the lines.
2613 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
2614 ReorderLines( bidirectionalInfo,
2616 requestedNumberOfCharacters,
2617 mImpl->mVisualModel->mLines,
2618 bidirectionalLineInfo );
2620 // Set the bidirectional info per line into the layout parameters.
2621 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
2622 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
2624 // Re-layout the text. Reorder those lines with right to left characters.
2625 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
2627 requestedNumberOfCharacters,
2630 if ( ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) ) && ( numberOfLines > 0 ) )
2632 const LineRun* const firstline = mImpl->mVisualModel->mLines.Begin();
2635 mImpl->mAutoScrollDirectionRTL = firstline->direction;
2641 // Sets the layout size.
2642 if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
2644 mImpl->mVisualModel->SetLayoutSize( layoutSize );
2648 // Store the size used to layout the text.
2649 mImpl->mVisualModel->mControlSize = size;
2652 if( NO_OPERATION != ( ALIGN & operations ) )
2654 // The laid-out lines.
2655 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
2657 mImpl->mLayoutEngine.Align( size,
2659 requestedNumberOfCharacters,
2664 #if defined(DEBUG_ENABLED)
2665 std::string currentText;
2666 GetText( currentText );
2667 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mAutoScrollDirectionRTL[%s] [%s]\n", this, (mImpl->mAutoScrollDirectionRTL)?"true":"false", currentText.c_str() );
2669 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
2673 void Controller::CalculateVerticalOffset( const Size& controlSize )
2675 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
2677 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
2679 // Get the line height of the default font.
2680 layoutSize.height = mImpl->GetDefaultFontLineHeight();
2683 switch( mImpl->mLayoutEngine.GetVerticalAlignment() )
2685 case LayoutEngine::VERTICAL_ALIGN_TOP:
2687 mImpl->mScrollPosition.y = 0.f;
2690 case LayoutEngine::VERTICAL_ALIGN_CENTER:
2692 mImpl->mScrollPosition.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
2695 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
2697 mImpl->mScrollPosition.y = controlSize.height - layoutSize.height;
2703 // private : Events.
2705 void Controller::ProcessModifyEvents()
2707 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
2709 if( 0u == events.Count() )
2715 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
2716 endIt = events.End();
2720 const ModifyEvent& event = *it;
2722 if( ModifyEvent::TEXT_REPLACED == event.type )
2724 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
2725 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
2727 TextReplacedEvent();
2729 else if( ModifyEvent::TEXT_INSERTED == event.type )
2731 TextInsertedEvent();
2733 else if( ModifyEvent::TEXT_DELETED == event.type )
2735 // Placeholder-text cannot be deleted
2736 if( !mImpl->IsShowingPlaceholderText() )
2743 if( NULL != mImpl->mEventData )
2745 // When the text is being modified, delay cursor blinking
2746 mImpl->mEventData->mDecorator->DelayCursorBlink();
2749 // Discard temporary text
2753 void Controller::TextReplacedEvent()
2755 // The natural size needs to be re-calculated.
2756 mImpl->mRecalculateNaturalSize = true;
2758 // Apply modifications to the model
2759 mImpl->mOperationsPending = ALL_OPERATIONS;
2762 void Controller::TextInsertedEvent()
2764 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
2766 if( NULL == mImpl->mEventData )
2771 // The natural size needs to be re-calculated.
2772 mImpl->mRecalculateNaturalSize = true;
2774 // Apply modifications to the model; TODO - Optimize this
2775 mImpl->mOperationsPending = ALL_OPERATIONS;
2778 void Controller::TextDeletedEvent()
2780 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
2782 if( NULL == mImpl->mEventData )
2787 // The natural size needs to be re-calculated.
2788 mImpl->mRecalculateNaturalSize = true;
2790 // Apply modifications to the model; TODO - Optimize this
2791 mImpl->mOperationsPending = ALL_OPERATIONS;
2794 void Controller::SelectEvent( float x, float y, bool selectAll )
2796 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2798 if( NULL != mImpl->mEventData )
2802 Event event( Event::SELECT_ALL );
2803 mImpl->mEventData->mEventQueue.push_back( event );
2807 Event event( Event::SELECT );
2808 event.p2.mFloat = x;
2809 event.p3.mFloat = y;
2810 mImpl->mEventData->mEventQueue.push_back( event );
2813 mImpl->RequestRelayout();
2817 bool Controller::BackspaceKeyEvent()
2819 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2821 bool removed = false;
2823 if( NULL == mImpl->mEventData )
2828 // IMF manager is no longer handling key-events
2829 mImpl->ClearPreEditFlag();
2831 if( EventData::SELECTING == mImpl->mEventData->mState )
2833 removed = RemoveSelectedText();
2835 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2837 // Remove the character before the current cursor position
2838 removed = RemoveText( -1,
2840 UPDATE_INPUT_STYLE );
2845 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2846 !mImpl->IsPlaceholderAvailable() )
2848 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2852 ShowPlaceholderText();
2854 mImpl->mEventData->mUpdateCursorPosition = true;
2855 mImpl->mEventData->mScrollAfterDelete = true;
2861 // private : Helpers.
2863 void Controller::ResetText()
2866 mImpl->mLogicalModel->mText.Clear();
2868 // We have cleared everything including the placeholder-text
2869 mImpl->PlaceholderCleared();
2871 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2872 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2873 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
2875 // Clear any previous text.
2876 mImpl->mTextUpdateInfo.mClearAll = true;
2878 // The natural size needs to be re-calculated.
2879 mImpl->mRecalculateNaturalSize = true;
2881 // Apply modifications to the model
2882 mImpl->mOperationsPending = ALL_OPERATIONS;
2885 void Controller::ShowPlaceholderText()
2887 if( mImpl->IsPlaceholderAvailable() )
2889 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2891 if( NULL == mImpl->mEventData )
2896 mImpl->mEventData->mIsShowingPlaceholderText = true;
2898 // Disable handles when showing place-holder text
2899 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2900 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2901 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2903 const char* text( NULL );
2906 // TODO - Switch placeholder text styles when changing state
2907 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2908 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2910 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2911 size = mImpl->mEventData->mPlaceholderTextActive.size();
2915 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2916 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2919 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2920 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2922 // Reset model for showing placeholder.
2923 mImpl->mLogicalModel->mText.Clear();
2924 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2926 // Convert text into UTF-32
2927 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2928 utf32Characters.Resize( size );
2930 // This is a bit horrible but std::string returns a (signed) char*
2931 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2933 // Transform a text array encoded in utf8 into an array encoded in utf32.
2934 // It returns the actual number of characters.
2935 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2936 utf32Characters.Resize( characterCount );
2938 // The characters to be added.
2939 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = characterCount;
2941 // Reset the cursor position
2942 mImpl->mEventData->mPrimaryCursorPosition = 0;
2944 // The natural size needs to be re-calculated.
2945 mImpl->mRecalculateNaturalSize = true;
2947 // Apply modifications to the model
2948 mImpl->mOperationsPending = ALL_OPERATIONS;
2950 // Update the rest of the model during size negotiation
2951 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2955 void Controller::ClearFontData()
2957 if( mImpl->mFontDefaults )
2959 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2962 // Set flags to update the model.
2963 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2964 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2965 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
2967 mImpl->mTextUpdateInfo.mClearAll = true;
2968 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
2969 mImpl->mRecalculateNaturalSize = true;
2971 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2976 UPDATE_LAYOUT_SIZE |
2981 void Controller::ClearStyleData()
2983 mImpl->mLogicalModel->mColorRuns.Clear();
2984 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2987 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
2989 // Reset the cursor position
2990 if( NULL != mImpl->mEventData )
2992 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
2994 // Update the cursor if it's in editing mode.
2995 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
2997 mImpl->mEventData->mUpdateCursorPosition = true;
3002 void Controller::ResetScrollPosition()
3004 if( NULL != mImpl->mEventData )
3006 // Reset the scroll position.
3007 mImpl->mScrollPosition = Vector2::ZERO;
3008 mImpl->mEventData->mScrollAfterUpdatePosition = true;
3012 // private : Private contructors & copy operator.
3014 Controller::Controller( ControlInterface& controlInterface )
3017 mImpl = new Controller::Impl( controlInterface );
3020 // The copy constructor and operator are left unimplemented.
3022 // protected : Destructor.
3024 Controller::~Controller()
3031 } // namespace Toolkit