2 * Copyright (c) 2015 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 ControllerPtr Controller::New( ControlInterface& controlInterface )
103 return ControllerPtr( new Controller( controlInterface ) );
106 void Controller::EnableTextInput( DecoratorPtr decorator )
108 if( NULL == mImpl->mEventData )
110 mImpl->mEventData = new EventData( decorator );
114 void Controller::SetGlyphType( TextAbstraction::GlyphType glyphType )
116 // Metrics for bitmap & vector based glyphs are different
117 mImpl->mMetrics->SetGlyphType( glyphType );
119 // Clear the font-specific data
122 mImpl->RequestRelayout();
125 void Controller::SetMarkupProcessorEnabled( bool enable )
127 mImpl->mMarkupProcessorEnabled = enable;
130 bool Controller::IsMarkupProcessorEnabled() const
132 return mImpl->mMarkupProcessorEnabled;
135 void Controller::SetAutoScrollEnabled( bool enable )
137 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 );
139 if ( mImpl->mLayoutEngine.GetLayout() == LayoutEngine::SINGLE_LINE_BOX )
143 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled for SINGLE_LINE_BOX\n" );
144 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
154 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled Disabling autoscroll\n");
155 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
162 mImpl->mAutoScrollEnabled = enable;
163 mImpl->RequestRelayout();
167 DALI_LOG_WARNING( "Attempted AutoScrolling on a non SINGLE_LINE_BOX, request ignored\n" );
168 mImpl->mAutoScrollEnabled = false;
172 bool Controller::IsAutoScrollEnabled() const
174 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::IsAutoScrollEnabled[%s]\n", (mImpl->mAutoScrollEnabled)?"true":"false" );
176 return mImpl->mAutoScrollEnabled;
179 CharacterDirection Controller::GetAutoScrollDirection() const
181 return mImpl->mAutoScrollDirectionRTL;
184 float Controller::GetAutoScrollLineAlignment() const
188 if( mImpl->mVisualModel &&
189 ( 0u != mImpl->mVisualModel->mLines.Count() ) )
191 offset = ( *mImpl->mVisualModel->mLines.Begin() ).alignmentOffset;
197 void Controller::SetHorizontalScrollEnabled( bool enable )
199 if( ( NULL != mImpl->mEventData ) &&
200 mImpl->mEventData->mDecorator )
202 mImpl->mEventData->mDecorator->SetHorizontalScrollEnabled( enable );
206 bool Controller::IsHorizontalScrollEnabled() const
208 if( ( NULL != mImpl->mEventData ) &&
209 mImpl->mEventData->mDecorator )
211 return mImpl->mEventData->mDecorator->IsHorizontalScrollEnabled();
217 void Controller::SetVerticalScrollEnabled( bool enable )
219 if( ( NULL != mImpl->mEventData ) &&
220 mImpl->mEventData->mDecorator )
222 if( mImpl->mEventData->mDecorator )
224 mImpl->mEventData->mDecorator->SetVerticalScrollEnabled( enable );
229 bool Controller::IsVerticalScrollEnabled() const
231 if( ( NULL != mImpl->mEventData ) &&
232 mImpl->mEventData->mDecorator )
234 return mImpl->mEventData->mDecorator->IsVerticalScrollEnabled();
240 void Controller::SetSmoothHandlePanEnabled( bool enable )
242 if( ( NULL != mImpl->mEventData ) &&
243 mImpl->mEventData->mDecorator )
245 mImpl->mEventData->mDecorator->SetSmoothHandlePanEnabled( enable );
249 bool Controller::IsSmoothHandlePanEnabled() const
251 if( ( NULL != mImpl->mEventData ) &&
252 mImpl->mEventData->mDecorator )
254 return mImpl->mEventData->mDecorator->IsSmoothHandlePanEnabled();
260 void Controller::SetText( const std::string& text )
262 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
264 // Reset keyboard as text changed
265 mImpl->ResetImfManager();
267 // Remove the previously set text and style.
273 CharacterIndex lastCursorIndex = 0u;
275 if( NULL != mImpl->mEventData )
277 // If popup shown then hide it by switching to Editing state
278 if( ( EventData::SELECTING == mImpl->mEventData->mState ) ||
279 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
280 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
281 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
283 mImpl->ChangeState( EventData::EDITING );
289 mImpl->mVisualModel->SetTextColor( mImpl->mTextColor );
291 MarkupProcessData markupProcessData( mImpl->mLogicalModel->mColorRuns,
292 mImpl->mLogicalModel->mFontDescriptionRuns );
294 Length textSize = 0u;
295 const uint8_t* utf8 = NULL;
296 if( mImpl->mMarkupProcessorEnabled )
298 ProcessMarkupString( text, markupProcessData );
299 textSize = markupProcessData.markupProcessedText.size();
301 // This is a bit horrible but std::string returns a (signed) char*
302 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
306 textSize = text.size();
308 // This is a bit horrible but std::string returns a (signed) char*
309 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
312 // Convert text into UTF-32
313 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
314 utf32Characters.Resize( textSize );
316 // Transform a text array encoded in utf8 into an array encoded in utf32.
317 // It returns the actual number of characters.
318 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
319 utf32Characters.Resize( characterCount );
321 DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
322 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mLogicalModel->mText.Count() );
324 // The characters to be added.
325 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
327 // To reset the cursor position
328 lastCursorIndex = characterCount;
330 // Update the rest of the model during size negotiation
331 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
333 // The natural size needs to be re-calculated.
334 mImpl->mRecalculateNaturalSize = true;
336 // Apply modifications to the model
337 mImpl->mOperationsPending = ALL_OPERATIONS;
341 ShowPlaceholderText();
344 // Resets the cursor position.
345 ResetCursorPosition( lastCursorIndex );
347 // Scrolls the text to make the cursor visible.
348 ResetScrollPosition();
350 mImpl->RequestRelayout();
352 if( NULL != mImpl->mEventData )
354 // Cancel previously queued events
355 mImpl->mEventData->mEventQueue.clear();
358 // Do this last since it provides callbacks into application code
359 mImpl->mControlInterface.TextChanged();
362 void Controller::GetText( std::string& text ) const
364 if( !mImpl->IsShowingPlaceholderText() )
366 // Retrieves the text string.
367 mImpl->GetText( 0u, text );
371 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
375 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
377 if( NULL != mImpl->mEventData )
379 if( PLACEHOLDER_TYPE_INACTIVE == type )
381 mImpl->mEventData->mPlaceholderTextInactive = text;
385 mImpl->mEventData->mPlaceholderTextActive = text;
388 // Update placeholder if there is no text
389 if( mImpl->IsShowingPlaceholderText() ||
390 ( 0u == mImpl->mLogicalModel->mText.Count() ) )
392 ShowPlaceholderText();
397 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
399 if( NULL != mImpl->mEventData )
401 if( PLACEHOLDER_TYPE_INACTIVE == type )
403 text = mImpl->mEventData->mPlaceholderTextInactive;
407 text = mImpl->mEventData->mPlaceholderTextActive;
412 void Controller::SetMaximumNumberOfCharacters( Length maxCharacters )
414 mImpl->mMaximumNumberOfCharacters = maxCharacters;
417 int Controller::GetMaximumNumberOfCharacters()
419 return mImpl->mMaximumNumberOfCharacters;
422 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
424 if( NULL == mImpl->mFontDefaults )
426 mImpl->mFontDefaults = new FontDefaults();
429 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
430 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
431 mImpl->mFontDefaults->familyDefined = true;
433 // Clear the font-specific data
436 mImpl->RequestRelayout();
439 const std::string& Controller::GetDefaultFontFamily() const
441 if( NULL != mImpl->mFontDefaults )
443 return mImpl->mFontDefaults->mFontDescription.family;
449 void Controller::SetDefaultFontStyle( const std::string& style )
451 if( NULL == mImpl->mFontDefaults )
453 mImpl->mFontDefaults = new FontDefaults();
456 mImpl->mFontDefaults->mFontStyle = style;
459 const std::string& Controller::GetDefaultFontStyle() const
461 if( NULL != mImpl->mFontDefaults )
463 return mImpl->mFontDefaults->mFontStyle;
469 void Controller::SetDefaultFontWeight( FontWeight weight )
471 if( NULL == mImpl->mFontDefaults )
473 mImpl->mFontDefaults = new FontDefaults();
476 mImpl->mFontDefaults->mFontDescription.weight = weight;
477 mImpl->mFontDefaults->weightDefined = true;
479 // Clear the font-specific data
482 mImpl->RequestRelayout();
485 FontWeight Controller::GetDefaultFontWeight() const
487 if( NULL != mImpl->mFontDefaults )
489 return mImpl->mFontDefaults->mFontDescription.weight;
492 return TextAbstraction::FontWeight::NORMAL;
495 void Controller::SetDefaultFontWidth( FontWidth width )
497 if( NULL == mImpl->mFontDefaults )
499 mImpl->mFontDefaults = new FontDefaults();
502 mImpl->mFontDefaults->mFontDescription.width = width;
503 mImpl->mFontDefaults->widthDefined = true;
505 // Clear the font-specific data
508 mImpl->RequestRelayout();
511 FontWidth Controller::GetDefaultFontWidth() const
513 if( NULL != mImpl->mFontDefaults )
515 return mImpl->mFontDefaults->mFontDescription.width;
518 return TextAbstraction::FontWidth::NORMAL;
521 void Controller::SetDefaultFontSlant( FontSlant slant )
523 if( NULL == mImpl->mFontDefaults )
525 mImpl->mFontDefaults = new FontDefaults();
528 mImpl->mFontDefaults->mFontDescription.slant = slant;
529 mImpl->mFontDefaults->slantDefined = true;
531 // Clear the font-specific data
534 mImpl->RequestRelayout();
537 FontSlant Controller::GetDefaultFontSlant() const
539 if( NULL != mImpl->mFontDefaults )
541 return mImpl->mFontDefaults->mFontDescription.slant;
544 return TextAbstraction::FontSlant::NORMAL;
547 void Controller::SetDefaultPointSize( float pointSize )
549 if( NULL == mImpl->mFontDefaults )
551 mImpl->mFontDefaults = new FontDefaults();
554 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
555 mImpl->mFontDefaults->sizeDefined = true;
557 // Clear the font-specific data
560 mImpl->RequestRelayout();
563 float Controller::GetDefaultPointSize() const
565 if( NULL != mImpl->mFontDefaults )
567 return mImpl->mFontDefaults->mDefaultPointSize;
573 void Controller::UpdateAfterFontChange( const std::string& newDefaultFont )
575 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::UpdateAfterFontChange\n");
577 if( !mImpl->mFontDefaults->familyDefined ) // If user defined font then should not update when system font changes
579 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
580 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
584 mImpl->RequestRelayout();
588 void Controller::SetTextColor( const Vector4& textColor )
590 mImpl->mTextColor = textColor;
592 if( !mImpl->IsShowingPlaceholderText() )
594 mImpl->mVisualModel->SetTextColor( textColor );
596 mImpl->RequestRelayout();
600 const Vector4& Controller::GetTextColor() const
602 return mImpl->mTextColor;
605 bool Controller::RemoveText( int cursorOffset,
606 int numberOfCharacters,
607 UpdateInputStyleType type )
609 bool removed = false;
611 if( NULL == mImpl->mEventData )
616 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfCharacters %d\n",
617 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters );
619 if( !mImpl->IsShowingPlaceholderText() )
621 // Delete at current cursor position
622 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
623 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
625 CharacterIndex cursorIndex = oldCursorIndex;
627 // Validate the cursor position & number of characters
628 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
630 cursorIndex = oldCursorIndex + cursorOffset;
633 if( ( cursorIndex + numberOfCharacters ) > currentText.Count() )
635 numberOfCharacters = currentText.Count() - cursorIndex;
638 if( ( cursorIndex + numberOfCharacters ) <= mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters )
640 // Mark the paragraphs to be updated.
641 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
642 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove += numberOfCharacters;
644 // Update the input style and remove the text's style before removing the text.
646 if( UPDATE_INPUT_STYLE == type )
648 // Set first the default input style.
649 mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
651 // Update the input style.
652 mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
655 // Updates the text style runs by removing characters. Runs with no characters are removed.
656 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfCharacters );
658 // Remove the characters.
659 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
660 Vector<Character>::Iterator last = first + numberOfCharacters;
662 currentText.Erase( first, last );
664 // Cursor position retreat
665 oldCursorIndex = cursorIndex;
667 mImpl->mEventData->mScrollAfterDelete = true;
669 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfCharacters );
677 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
679 if( NULL != mImpl->mEventData )
681 mImpl->mEventData->mPlaceholderTextColor = textColor;
684 if( mImpl->IsShowingPlaceholderText() )
686 mImpl->mVisualModel->SetTextColor( textColor );
687 mImpl->RequestRelayout();
691 const Vector4& Controller::GetPlaceholderTextColor() const
693 if( NULL != mImpl->mEventData )
695 return mImpl->mEventData->mPlaceholderTextColor;
701 void Controller::SetShadowOffset( const Vector2& shadowOffset )
703 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
705 mImpl->RequestRelayout();
708 const Vector2& Controller::GetShadowOffset() const
710 return mImpl->mVisualModel->GetShadowOffset();
713 void Controller::SetShadowColor( const Vector4& shadowColor )
715 mImpl->mVisualModel->SetShadowColor( shadowColor );
717 mImpl->RequestRelayout();
720 const Vector4& Controller::GetShadowColor() const
722 return mImpl->mVisualModel->GetShadowColor();
725 void Controller::SetDefaultShadowProperties( const std::string& shadowProperties )
727 if( NULL == mImpl->mShadowDefaults )
729 mImpl->mShadowDefaults = new ShadowDefaults();
732 mImpl->mShadowDefaults->properties = shadowProperties;
735 const std::string& Controller::GetDefaultShadowProperties() const
737 if( NULL != mImpl->mShadowDefaults )
739 return mImpl->mShadowDefaults->properties;
745 void Controller::SetUnderlineColor( const Vector4& color )
747 mImpl->mVisualModel->SetUnderlineColor( color );
749 mImpl->RequestRelayout();
752 const Vector4& Controller::GetUnderlineColor() const
754 return mImpl->mVisualModel->GetUnderlineColor();
757 void Controller::SetUnderlineEnabled( bool enabled )
759 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
761 mImpl->RequestRelayout();
764 bool Controller::IsUnderlineEnabled() const
766 return mImpl->mVisualModel->IsUnderlineEnabled();
769 void Controller::SetUnderlineHeight( float height )
771 mImpl->mVisualModel->SetUnderlineHeight( height );
773 mImpl->RequestRelayout();
776 float Controller::GetUnderlineHeight() const
778 return mImpl->mVisualModel->GetUnderlineHeight();
781 void Controller::SetDefaultUnderlineProperties( const std::string& underlineProperties )
783 if( NULL == mImpl->mUnderlineDefaults )
785 mImpl->mUnderlineDefaults = new UnderlineDefaults();
788 mImpl->mUnderlineDefaults->properties = underlineProperties;
791 const std::string& Controller::GetDefaultUnderlineProperties() const
793 if( NULL != mImpl->mUnderlineDefaults )
795 return mImpl->mUnderlineDefaults->properties;
801 void Controller::SetDefaultEmbossProperties( const std::string& embossProperties )
803 if( NULL == mImpl->mEmbossDefaults )
805 mImpl->mEmbossDefaults = new EmbossDefaults();
808 mImpl->mEmbossDefaults->properties = embossProperties;
811 const std::string& Controller::GetDefaultEmbossProperties() const
813 if( NULL != mImpl->mEmbossDefaults )
815 return mImpl->mEmbossDefaults->properties;
821 void Controller::SetDefaultOutlineProperties( const std::string& outlineProperties )
823 if( NULL == mImpl->mOutlineDefaults )
825 mImpl->mOutlineDefaults = new OutlineDefaults();
828 mImpl->mOutlineDefaults->properties = outlineProperties;
831 const std::string& Controller::GetDefaultOutlineProperties() const
833 if( NULL != mImpl->mOutlineDefaults )
835 return mImpl->mOutlineDefaults->properties;
841 void Controller::SetDefaultLineSpacing( float lineSpacing )
843 //TODO finish implementation
844 mImpl->mLayoutEngine.SetDefaultLineSpacing( lineSpacing );
847 float Controller::GetDefaultLineSpacing() const
849 return mImpl->mLayoutEngine.GetDefaultLineSpacing();
852 void Controller::SetInputColor( const Vector4& color )
854 if( NULL != mImpl->mEventData )
856 mImpl->mEventData->mInputStyle.textColor = color;
857 mImpl->mEventData->mInputStyle.isDefaultColor = false;
859 if( EventData::SELECTING == mImpl->mEventData->mState )
861 const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
863 // Get start and end position of selection
864 const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
865 const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
867 // Add the color run.
868 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
869 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
871 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
872 colorRun.color = color;
873 colorRun.characterRun.characterIndex = startOfSelectedText;
874 colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
876 // Request to relayout.
877 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
878 mImpl->RequestRelayout();
880 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
881 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
882 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
887 const Vector4& Controller::GetInputColor() const
889 if( NULL != mImpl->mEventData )
891 return mImpl->mEventData->mInputStyle.textColor;
894 // Return the default text's color if there is no EventData.
895 return mImpl->mTextColor;
899 void Controller::SetInputFontFamily( const std::string& fontFamily )
901 if( NULL != mImpl->mEventData )
903 mImpl->mEventData->mInputStyle.familyName = fontFamily;
904 mImpl->mEventData->mInputStyle.familyDefined = true;
906 if( EventData::SELECTING == mImpl->mEventData->mState )
908 CharacterIndex startOfSelectedText = 0u;
909 Length lengthOfSelectedText = 0u;
910 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
911 mImpl->mLogicalModel,
913 lengthOfSelectedText );
915 fontDescriptionRun.familyLength = fontFamily.size();
916 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
917 memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
918 fontDescriptionRun.familyDefined = true;
920 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
922 // Request to relayout.
923 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
931 mImpl->mRecalculateNaturalSize = true;
932 mImpl->RequestRelayout();
934 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
935 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
936 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
938 // As the font changes, recalculate the handle positions is needed.
939 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
940 mImpl->mEventData->mUpdateRightSelectionPosition = true;
941 mImpl->mEventData->mUpdateHighlightBox = true;
942 mImpl->mEventData->mScrollAfterUpdatePosition = true;
947 const std::string& Controller::GetInputFontFamily() const
949 if( NULL != mImpl->mEventData )
951 return mImpl->mEventData->mInputStyle.familyName;
954 // Return the default font's family if there is no EventData.
955 return GetDefaultFontFamily();
958 void Controller::SetInputFontStyle( const std::string& fontStyle )
960 if( NULL != mImpl->mEventData )
962 mImpl->mEventData->mInputStyle.fontStyle = fontStyle;
966 const std::string& Controller::GetInputFontStyle() const
968 if( NULL != mImpl->mEventData )
970 return mImpl->mEventData->mInputStyle.fontStyle;
973 // Return the default font's style if there is no EventData.
974 return GetDefaultFontStyle();
977 void Controller::SetInputFontWeight( FontWeight weight )
979 if( NULL != mImpl->mEventData )
981 mImpl->mEventData->mInputStyle.weight = weight;
982 mImpl->mEventData->mInputStyle.weightDefined = true;
984 if( EventData::SELECTING == mImpl->mEventData->mState )
986 CharacterIndex startOfSelectedText = 0u;
987 Length lengthOfSelectedText = 0u;
988 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
989 mImpl->mLogicalModel,
991 lengthOfSelectedText );
993 fontDescriptionRun.weight = weight;
994 fontDescriptionRun.weightDefined = true;
996 // Request to relayout.
997 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1002 UPDATE_LAYOUT_SIZE |
1005 mImpl->mRecalculateNaturalSize = true;
1006 mImpl->RequestRelayout();
1008 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1009 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1010 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1012 // As the font might change, recalculate the handle positions is needed.
1013 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1014 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1015 mImpl->mEventData->mUpdateHighlightBox = true;
1016 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1021 FontWeight Controller::GetInputFontWeight() const
1023 if( NULL != mImpl->mEventData )
1025 return mImpl->mEventData->mInputStyle.weight;
1028 return GetDefaultFontWeight();
1031 void Controller::SetInputFontWidth( FontWidth width )
1033 if( NULL != mImpl->mEventData )
1035 mImpl->mEventData->mInputStyle.width = width;
1036 mImpl->mEventData->mInputStyle.widthDefined = true;
1038 if( EventData::SELECTING == mImpl->mEventData->mState )
1040 CharacterIndex startOfSelectedText = 0u;
1041 Length lengthOfSelectedText = 0u;
1042 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1043 mImpl->mLogicalModel,
1044 startOfSelectedText,
1045 lengthOfSelectedText );
1047 fontDescriptionRun.width = width;
1048 fontDescriptionRun.widthDefined = true;
1050 // Request to relayout.
1051 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1056 UPDATE_LAYOUT_SIZE |
1059 mImpl->mRecalculateNaturalSize = true;
1060 mImpl->RequestRelayout();
1062 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1063 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1064 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1066 // As the font might change, recalculate the handle positions is needed.
1067 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1068 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1069 mImpl->mEventData->mUpdateHighlightBox = true;
1070 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1075 FontWidth Controller::GetInputFontWidth() const
1077 if( NULL != mImpl->mEventData )
1079 return mImpl->mEventData->mInputStyle.width;
1082 return GetDefaultFontWidth();
1085 void Controller::SetInputFontSlant( FontSlant slant )
1087 if( NULL != mImpl->mEventData )
1089 mImpl->mEventData->mInputStyle.slant = slant;
1090 mImpl->mEventData->mInputStyle.slantDefined = true;
1092 if( EventData::SELECTING == mImpl->mEventData->mState )
1094 CharacterIndex startOfSelectedText = 0u;
1095 Length lengthOfSelectedText = 0u;
1096 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1097 mImpl->mLogicalModel,
1098 startOfSelectedText,
1099 lengthOfSelectedText );
1101 fontDescriptionRun.slant = slant;
1102 fontDescriptionRun.slantDefined = true;
1104 // Request to relayout.
1105 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1110 UPDATE_LAYOUT_SIZE |
1113 mImpl->mRecalculateNaturalSize = true;
1114 mImpl->RequestRelayout();
1116 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1117 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1118 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1120 // As the font might change, recalculate the handle positions is needed.
1121 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1122 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1123 mImpl->mEventData->mUpdateHighlightBox = true;
1124 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1129 FontSlant Controller::GetInputFontSlant() const
1131 if( NULL != mImpl->mEventData )
1133 return mImpl->mEventData->mInputStyle.slant;
1136 return GetDefaultFontSlant();
1139 void Controller::SetInputFontPointSize( float size )
1141 if( NULL != mImpl->mEventData )
1143 mImpl->mEventData->mInputStyle.size = size;
1145 if( EventData::SELECTING == mImpl->mEventData->mState )
1147 CharacterIndex startOfSelectedText = 0u;
1148 Length lengthOfSelectedText = 0u;
1149 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1150 mImpl->mLogicalModel,
1151 startOfSelectedText,
1152 lengthOfSelectedText );
1154 fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
1155 fontDescriptionRun.sizeDefined = true;
1157 // Request to relayout.
1158 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1163 UPDATE_LAYOUT_SIZE |
1166 mImpl->mRecalculateNaturalSize = true;
1167 mImpl->RequestRelayout();
1169 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1170 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1171 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1173 // As the font might change, recalculate the handle positions is needed.
1174 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1175 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1176 mImpl->mEventData->mUpdateHighlightBox = true;
1177 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1182 float Controller::GetInputFontPointSize() const
1184 if( NULL != mImpl->mEventData )
1186 return mImpl->mEventData->mInputStyle.size;
1189 // Return the default font's point size if there is no EventData.
1190 return GetDefaultPointSize();
1193 void Controller::SetInputLineSpacing( float lineSpacing )
1195 if( NULL != mImpl->mEventData )
1197 mImpl->mEventData->mInputStyle.lineSpacing = lineSpacing;
1201 float Controller::GetInputLineSpacing() const
1203 if( NULL != mImpl->mEventData )
1205 return mImpl->mEventData->mInputStyle.lineSpacing;
1211 void Controller::SetInputShadowProperties( const std::string& shadowProperties )
1213 if( NULL != mImpl->mEventData )
1215 mImpl->mEventData->mInputStyle.shadowProperties = shadowProperties;
1219 const std::string& Controller::GetInputShadowProperties() const
1221 if( NULL != mImpl->mEventData )
1223 return mImpl->mEventData->mInputStyle.shadowProperties;
1226 return GetDefaultShadowProperties();
1229 void Controller::SetInputUnderlineProperties( const std::string& underlineProperties )
1231 if( NULL != mImpl->mEventData )
1233 mImpl->mEventData->mInputStyle.underlineProperties = underlineProperties;
1237 const std::string& Controller::GetInputUnderlineProperties() const
1239 if( NULL != mImpl->mEventData )
1241 return mImpl->mEventData->mInputStyle.underlineProperties;
1244 return GetDefaultUnderlineProperties();
1247 void Controller::SetInputEmbossProperties( const std::string& embossProperties )
1249 if( NULL != mImpl->mEventData )
1251 mImpl->mEventData->mInputStyle.embossProperties = embossProperties;
1255 const std::string& Controller::GetInputEmbossProperties() const
1257 if( NULL != mImpl->mEventData )
1259 return mImpl->mEventData->mInputStyle.embossProperties;
1262 return GetDefaultEmbossProperties();
1265 void Controller::SetInputOutlineProperties( const std::string& outlineProperties )
1267 if( NULL != mImpl->mEventData )
1269 mImpl->mEventData->mInputStyle.outlineProperties = outlineProperties;
1273 const std::string& Controller::GetInputOutlineProperties() const
1275 if( NULL != mImpl->mEventData )
1277 return mImpl->mEventData->mInputStyle.outlineProperties;
1280 return GetDefaultOutlineProperties();
1283 void Controller::SetEnableCursorBlink( bool enable )
1285 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
1287 if( NULL != mImpl->mEventData )
1289 mImpl->mEventData->mCursorBlinkEnabled = enable;
1292 mImpl->mEventData->mDecorator )
1294 mImpl->mEventData->mDecorator->StopCursorBlink();
1299 bool Controller::GetEnableCursorBlink() const
1301 if( NULL != mImpl->mEventData )
1303 return mImpl->mEventData->mCursorBlinkEnabled;
1309 const Vector2& Controller::GetScrollPosition() const
1311 return mImpl->mScrollPosition;
1314 Vector3 Controller::GetNaturalSize()
1316 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
1317 Vector3 naturalSize;
1319 // Make sure the model is up-to-date before layouting
1320 ProcessModifyEvents();
1322 if( mImpl->mRecalculateNaturalSize )
1324 // Operations that can be done only once until the text changes.
1325 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1332 GET_GLYPH_METRICS );
1333 // Make sure the model is up-to-date before layouting
1334 mImpl->UpdateModel( onlyOnceOperations );
1336 // Layout the text for the new width.
1337 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1339 // Set the update info to relayout the whole text.
1340 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1341 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1343 // Store the actual control's size to restore later.
1344 const Size actualControlSize = mImpl->mVisualModel->mControlSize;
1346 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
1347 static_cast<OperationsMask>( onlyOnceOperations |
1349 naturalSize.GetVectorXY() );
1351 // Do not do again the only once operations.
1352 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1354 // Do the size related operations again.
1355 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1358 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1360 // Stores the natural size to avoid recalculate it again
1361 // unless the text/style changes.
1362 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
1364 mImpl->mRecalculateNaturalSize = false;
1366 // Clear the update info. This info will be set the next time the text is updated.
1367 mImpl->mTextUpdateInfo.Clear();
1369 // Restore the actual control's size.
1370 mImpl->mVisualModel->mControlSize = actualControlSize;
1372 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1376 naturalSize = mImpl->mVisualModel->GetNaturalSize();
1378 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1381 naturalSize.x = ConvertToEven( naturalSize.x );
1382 naturalSize.y = ConvertToEven( naturalSize.y );
1387 float Controller::GetHeightForWidth( float width )
1389 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1390 // Make sure the model is up-to-date before layouting
1391 ProcessModifyEvents();
1394 if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
1396 // Operations that can be done only once until the text changes.
1397 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1404 GET_GLYPH_METRICS );
1405 // Make sure the model is up-to-date before layouting
1406 mImpl->UpdateModel( onlyOnceOperations );
1409 // Layout the text for the new width.
1410 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1412 // Set the update info to relayout the whole text.
1413 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1414 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1416 // Store the actual control's width.
1417 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
1419 DoRelayout( Size( width, MAX_FLOAT ),
1420 static_cast<OperationsMask>( onlyOnceOperations |
1424 // Do not do again the only once operations.
1425 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1427 // Do the size related operations again.
1428 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1432 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1434 // Clear the update info. This info will be set the next time the text is updated.
1435 mImpl->mTextUpdateInfo.Clear();
1437 // Restore the actual control's width.
1438 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1440 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1444 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1445 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1448 return layoutSize.height;
1451 Controller::UpdateTextType Controller::Relayout( const Size& size )
1453 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", this, size.width, size.height, (mImpl->mAutoScrollEnabled)?"true":"false" );
1455 UpdateTextType updateTextType = NONE_UPDATED;
1457 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1459 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1461 mImpl->mVisualModel->mGlyphPositions.Clear();
1462 updateTextType = MODEL_UPDATED;
1465 // Clear the update info. This info will be set the next time the text is updated.
1466 mImpl->mTextUpdateInfo.Clear();
1468 // Not worth to relayout if width or height is equal to zero.
1469 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1471 return updateTextType;
1474 // Whether a new size has been set.
1475 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1479 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1481 // Layout operations that need to be done if the size changes.
1482 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1485 UPDATE_LAYOUT_SIZE |
1487 // Set the update info to relayout the whole text.
1488 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
1489 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1492 // Whether there are modify events.
1493 if( 0u != mImpl->mModifyEvents.Count() )
1495 // Style operations that need to be done if the text is modified.
1496 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1500 // Make sure the model is up-to-date before layouting.
1501 ProcessModifyEvents();
1502 bool updated = mImpl->UpdateModel( mImpl->mOperationsPending );
1506 updated = DoRelayout( size,
1507 mImpl->mOperationsPending,
1508 layoutSize ) || updated;
1512 updateTextType = MODEL_UPDATED;
1515 // Do not re-do any operation until something changes.
1516 mImpl->mOperationsPending = NO_OPERATION;
1518 // Whether the text control is editable
1519 const bool isEditable = NULL != mImpl->mEventData;
1521 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
1523 if( newSize && isEditable )
1525 offset = mImpl->mScrollPosition;
1528 if( !isEditable || !IsMultiLineEnabled() )
1530 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
1531 CalculateVerticalOffset( size );
1538 // If there is a new size, the scroll position needs to be clamped.
1539 mImpl->ClampHorizontalScroll( layoutSize );
1541 // Update the decorator's positions is needed if there is a new size.
1542 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mScrollPosition - offset );
1545 // Move the cursor, grab handle etc.
1546 if( mImpl->ProcessInputEvents() )
1548 updateTextType = static_cast<UpdateTextType>( updateTextType | DECORATOR_UPDATED );
1552 // Clear the update info. This info will be set the next time the text is updated.
1553 mImpl->mTextUpdateInfo.Clear();
1554 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1556 return updateTextType;
1559 void Controller::ProcessModifyEvents()
1561 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
1563 if( 0u == events.Count() )
1569 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
1570 endIt = events.End();
1574 const ModifyEvent& event = *it;
1576 if( ModifyEvent::TEXT_REPLACED == event.type )
1578 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
1579 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
1581 TextReplacedEvent();
1583 else if( ModifyEvent::TEXT_INSERTED == event.type )
1585 TextInsertedEvent();
1587 else if( ModifyEvent::TEXT_DELETED == event.type )
1589 // Placeholder-text cannot be deleted
1590 if( !mImpl->IsShowingPlaceholderText() )
1597 if( NULL != mImpl->mEventData )
1599 // When the text is being modified, delay cursor blinking
1600 mImpl->mEventData->mDecorator->DelayCursorBlink();
1603 // Discard temporary text
1607 void Controller::ResetText()
1610 mImpl->mLogicalModel->mText.Clear();
1612 // We have cleared everything including the placeholder-text
1613 mImpl->PlaceholderCleared();
1615 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1616 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1617 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
1619 // Clear any previous text.
1620 mImpl->mTextUpdateInfo.mClearAll = true;
1622 // The natural size needs to be re-calculated.
1623 mImpl->mRecalculateNaturalSize = true;
1625 // Apply modifications to the model
1626 mImpl->mOperationsPending = ALL_OPERATIONS;
1629 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
1631 // Reset the cursor position
1632 if( NULL != mImpl->mEventData )
1634 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
1636 // Update the cursor if it's in editing mode.
1637 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1639 mImpl->mEventData->mUpdateCursorPosition = true;
1644 void Controller::ResetScrollPosition()
1646 if( NULL != mImpl->mEventData )
1648 // Reset the scroll position.
1649 mImpl->mScrollPosition = Vector2::ZERO;
1650 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1654 void Controller::TextReplacedEvent()
1656 // The natural size needs to be re-calculated.
1657 mImpl->mRecalculateNaturalSize = true;
1659 // Apply modifications to the model
1660 mImpl->mOperationsPending = ALL_OPERATIONS;
1663 void Controller::TextInsertedEvent()
1665 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
1667 if( NULL == mImpl->mEventData )
1672 // The natural size needs to be re-calculated.
1673 mImpl->mRecalculateNaturalSize = true;
1675 // Apply modifications to the model; TODO - Optimize this
1676 mImpl->mOperationsPending = ALL_OPERATIONS;
1679 void Controller::TextDeletedEvent()
1681 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
1683 if( NULL == mImpl->mEventData )
1688 // The natural size needs to be re-calculated.
1689 mImpl->mRecalculateNaturalSize = true;
1691 // Apply modifications to the model; TODO - Optimize this
1692 mImpl->mOperationsPending = ALL_OPERATIONS;
1695 bool Controller::DoRelayout( const Size& size,
1696 OperationsMask operationsRequired,
1699 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
1700 bool viewUpdated( false );
1702 // Calculate the operations to be done.
1703 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
1705 const CharacterIndex startIndex = mImpl->mTextUpdateInfo.mParagraphCharacterIndex;
1706 const Length requestedNumberOfCharacters = mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters;
1708 // Get the current layout size.
1709 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1711 if( NO_OPERATION != ( LAYOUT & operations ) )
1713 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
1715 // Some vectors with data needed to layout and reorder may be void
1716 // after the first time the text has been laid out.
1717 // Fill the vectors again.
1719 // Calculate the number of glyphs to layout.
1720 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1721 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1722 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
1723 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
1725 const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
1726 const GlyphIndex startGlyphIndex = mImpl->mTextUpdateInfo.mStartGlyphIndex;
1727 const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
1728 const Length totalNumberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
1730 if( 0u == totalNumberOfGlyphs )
1732 if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
1734 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
1737 // Nothing else to do if there is no glyphs.
1738 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1742 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1743 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1744 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1745 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1746 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1747 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1748 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1750 // Set the layout parameters.
1751 LayoutParameters layoutParameters( size,
1753 lineBreakInfo.Begin(),
1754 wordBreakInfo.Begin(),
1755 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1757 glyphsToCharactersMap.Begin(),
1758 charactersPerGlyph.Begin(),
1759 charactersToGlyphBuffer,
1760 glyphsPerCharacterBuffer,
1761 totalNumberOfGlyphs );
1763 // Resize the vector of positions to have the same size than the vector of glyphs.
1764 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1765 glyphPositions.Resize( totalNumberOfGlyphs );
1767 // Whether the last character is a new paragraph character.
1768 mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1769 layoutParameters.isLastNewParagraph = mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph;
1771 // The initial glyph and the number of glyphs to layout.
1772 layoutParameters.startGlyphIndex = startGlyphIndex;
1773 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1774 layoutParameters.startLineIndex = mImpl->mTextUpdateInfo.mStartLineIndex;
1775 layoutParameters.estimatedNumberOfLines = mImpl->mTextUpdateInfo.mEstimatedNumberOfLines;
1777 // Update the visual model.
1779 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1781 mImpl->mVisualModel->mLines,
1784 viewUpdated = viewUpdated || ( newLayoutSize != layoutSize );
1788 layoutSize = newLayoutSize;
1790 if ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) )
1792 mImpl->mAutoScrollDirectionRTL = false;
1795 // Reorder the lines
1796 if( NO_OPERATION != ( REORDER & operations ) )
1798 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1799 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1801 // Check first if there are paragraphs with bidirectional info.
1802 if( 0u != bidirectionalInfo.Count() )
1805 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1807 // Reorder the lines.
1808 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1809 ReorderLines( bidirectionalInfo,
1811 requestedNumberOfCharacters,
1812 mImpl->mVisualModel->mLines,
1813 bidirectionalLineInfo );
1815 // Set the bidirectional info per line into the layout parameters.
1816 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1817 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1819 // Re-layout the text. Reorder those lines with right to left characters.
1820 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1822 requestedNumberOfCharacters,
1825 if ( ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) ) && ( numberOfLines > 0 ) )
1827 const LineRun* const firstline = mImpl->mVisualModel->mLines.Begin();
1830 mImpl->mAutoScrollDirectionRTL = firstline->direction;
1836 // Sets the layout size.
1837 if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
1839 mImpl->mVisualModel->SetLayoutSize( layoutSize );
1843 // Store the size used to layout the text.
1844 mImpl->mVisualModel->mControlSize = size;
1847 if( NO_OPERATION != ( ALIGN & operations ) )
1849 // The laid-out lines.
1850 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1852 mImpl->mLayoutEngine.Align( size,
1854 requestedNumberOfCharacters,
1859 #if defined(DEBUG_ENABLED)
1860 std::string currentText;
1861 GetText( currentText );
1862 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mAutoScrollDirectionRTL[%s] [%s]\n", this, (mImpl->mAutoScrollDirectionRTL)?"true":"false", currentText.c_str() );
1864 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1868 void Controller::SetMultiLineEnabled( bool enable )
1870 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1872 if( layout != mImpl->mLayoutEngine.GetLayout() )
1874 // Set the layout type.
1875 mImpl->mLayoutEngine.SetLayout( layout );
1877 // Set the flags to redo the layout operations
1878 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1879 UPDATE_LAYOUT_SIZE |
1883 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1885 mImpl->RequestRelayout();
1889 bool Controller::IsMultiLineEnabled() const
1891 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1894 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1896 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1898 // Set the alignment.
1899 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1901 // Set the flag to redo the alignment operation.
1902 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1904 mImpl->RequestRelayout();
1908 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1910 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1913 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1915 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1917 // Set the alignment.
1918 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1920 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1922 mImpl->RequestRelayout();
1926 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1928 return mImpl->mLayoutEngine.GetVerticalAlignment();
1931 void Controller::CalculateVerticalOffset( const Size& controlSize )
1933 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1935 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1937 // Get the line height of the default font.
1938 layoutSize.height = mImpl->GetDefaultFontLineHeight();
1941 switch( mImpl->mLayoutEngine.GetVerticalAlignment() )
1943 case LayoutEngine::VERTICAL_ALIGN_TOP:
1945 mImpl->mScrollPosition.y = 0.f;
1948 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1950 mImpl->mScrollPosition.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
1953 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1955 mImpl->mScrollPosition.y = controlSize.height - layoutSize.height;
1961 LayoutEngine& Controller::GetLayoutEngine()
1963 return mImpl->mLayoutEngine;
1966 View& Controller::GetView()
1968 return mImpl->mView;
1971 void Controller::KeyboardFocusGainEvent()
1973 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1975 if( NULL != mImpl->mEventData )
1977 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1978 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1980 mImpl->ChangeState( EventData::EDITING );
1981 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1984 if( mImpl->IsShowingPlaceholderText() )
1986 // Show alternative placeholder-text when editing
1987 ShowPlaceholderText();
1990 mImpl->RequestRelayout();
1994 void Controller::KeyboardFocusLostEvent()
1996 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1998 if( NULL != mImpl->mEventData )
2000 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
2002 mImpl->ChangeState( EventData::INACTIVE );
2004 if( !mImpl->IsShowingRealText() )
2006 // Revert to regular placeholder-text when not editing
2007 ShowPlaceholderText();
2011 mImpl->RequestRelayout();
2014 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
2016 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
2018 bool textChanged( false );
2020 if( ( NULL != mImpl->mEventData ) &&
2021 ( keyEvent.state == KeyEvent::Down ) )
2023 int keyCode = keyEvent.keyCode;
2024 const std::string& keyString = keyEvent.keyPressed;
2026 // Pre-process to separate modifying events from non-modifying input events.
2027 if( Dali::DALI_KEY_ESCAPE == keyCode )
2029 // Escape key is a special case which causes focus loss
2030 KeyboardFocusLostEvent();
2032 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
2033 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
2034 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
2035 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
2037 Event event( Event::CURSOR_KEY_EVENT );
2038 event.p1.mInt = keyCode;
2039 mImpl->mEventData->mEventQueue.push_back( event );
2041 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
2043 textChanged = BackspaceKeyEvent();
2045 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
2047 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
2048 // Avoids calling the InsertText() method which can delete selected text
2050 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
2051 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
2053 mImpl->ChangeState( EventData::INACTIVE );
2054 // Menu/Home key behaviour does not allow edit mode to resume like Power key
2055 // Avoids calling the InsertText() method which can delete selected text
2057 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
2059 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
2060 // and a character is typed after the type of a upper case latin character.
2066 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
2068 // IMF manager is no longer handling key-events
2069 mImpl->ClearPreEditFlag();
2071 InsertText( keyString, COMMIT );
2075 if ( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
2076 ( mImpl->mEventData->mState != EventData::INACTIVE ) &&
2077 ( Dali::DALI_KEY_SHIFT_LEFT != keyCode ) )
2079 // Should not change the state if the key is the shift send by the imf manager.
2080 // Otherwise, when the state is SELECTING the text controller can't send the right
2081 // surrounding info to the imf.
2082 mImpl->ChangeState( EventData::EDITING );
2085 mImpl->RequestRelayout();
2090 // Do this last since it provides callbacks into application code
2091 mImpl->mControlInterface.TextChanged();
2097 void Controller::InsertText( const std::string& text, Controller::InsertType type )
2099 bool removedPrevious( false );
2100 bool maxLengthReached( false );
2102 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
2104 if( NULL == mImpl->mEventData )
2109 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
2110 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
2111 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2113 // TODO: At the moment the underline runs are only for pre-edit.
2114 mImpl->mVisualModel->mUnderlineRuns.Clear();
2116 // Keep the current number of characters.
2117 const Length currentNumberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2119 // Remove the previous IMF pre-edit.
2120 if( mImpl->mEventData->mPreEditFlag && ( 0u != mImpl->mEventData->mPreEditLength ) )
2122 removedPrevious = RemoveText( -static_cast<int>( mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition ),
2123 mImpl->mEventData->mPreEditLength,
2124 DONT_UPDATE_INPUT_STYLE );
2126 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
2127 mImpl->mEventData->mPreEditLength = 0u;
2131 // Remove the previous Selection.
2132 removedPrevious = RemoveSelectedText();
2135 Vector<Character> utf32Characters;
2136 Length characterCount = 0u;
2140 // Convert text into UTF-32
2141 utf32Characters.Resize( text.size() );
2143 // This is a bit horrible but std::string returns a (signed) char*
2144 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
2146 // Transform a text array encoded in utf8 into an array encoded in utf32.
2147 // It returns the actual number of characters.
2148 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
2149 utf32Characters.Resize( characterCount );
2151 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
2152 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
2155 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
2157 // The placeholder text is no longer needed
2158 if( mImpl->IsShowingPlaceholderText() )
2163 mImpl->ChangeState( EventData::EDITING );
2165 // Handle the IMF (predicitive text) state changes
2166 if( COMMIT == type )
2168 // IMF manager is no longer handling key-events
2169 mImpl->ClearPreEditFlag();
2173 if( !mImpl->mEventData->mPreEditFlag )
2175 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state\n" );
2177 // Record the start of the pre-edit text
2178 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
2181 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
2182 mImpl->mEventData->mPreEditFlag = true;
2184 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2187 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
2189 // Restrict new text to fit within Maximum characters setting.
2190 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
2191 maxLengthReached = ( characterCount > maxSizeOfNewText );
2193 // The cursor position.
2194 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
2196 // Update the text's style.
2198 // Updates the text style runs by adding characters.
2199 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
2201 // Get the character index from the cursor index.
2202 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
2204 // Retrieve the text's style for the given index.
2206 mImpl->RetrieveDefaultInputStyle( style );
2207 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
2209 // Whether to add a new text color run.
2210 const bool addColorRun = ( style.textColor != mImpl->mEventData->mInputStyle.textColor );
2212 // Whether to add a new font run.
2213 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
2214 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
2215 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
2216 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
2217 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
2222 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
2223 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
2225 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
2226 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
2227 colorRun.characterRun.characterIndex = cursorIndex;
2228 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2231 if( addFontNameRun ||
2237 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
2238 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
2240 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
2242 if( addFontNameRun )
2244 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
2245 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
2246 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
2247 fontDescriptionRun.familyDefined = true;
2249 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
2252 if( addFontWeightRun )
2254 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
2255 fontDescriptionRun.weightDefined = true;
2258 if( addFontWidthRun )
2260 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
2261 fontDescriptionRun.widthDefined = true;
2264 if( addFontSlantRun )
2266 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
2267 fontDescriptionRun.slantDefined = true;
2270 if( addFontSizeRun )
2272 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
2273 fontDescriptionRun.sizeDefined = true;
2276 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
2277 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2280 // Insert at current cursor position.
2281 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
2283 if( cursorIndex < numberOfCharactersInModel )
2285 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2289 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2292 // Mark the first paragraph to be updated.
2293 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
2294 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
2296 // Update the cursor index.
2297 cursorIndex += maxSizeOfNewText;
2299 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
2302 const Length numberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2304 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
2305 mImpl->IsPlaceholderAvailable() )
2307 // Show place-holder if empty after removing the pre-edit text
2308 ShowPlaceholderText();
2309 mImpl->mEventData->mUpdateCursorPosition = true;
2310 mImpl->ClearPreEditFlag();
2312 else if( removedPrevious ||
2313 ( 0 != utf32Characters.Count() ) )
2315 // Queue an inserted event
2316 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
2318 mImpl->mEventData->mUpdateCursorPosition = true;
2319 if( numberOfCharacters < currentNumberOfCharacters )
2321 mImpl->mEventData->mScrollAfterDelete = true;
2325 mImpl->mEventData->mScrollAfterUpdatePosition = true;
2329 if( maxLengthReached )
2331 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
2333 mImpl->ResetImfManager();
2335 // Do this last since it provides callbacks into application code
2336 mImpl->mControlInterface.MaxLengthReached();
2340 bool Controller::RemoveSelectedText()
2342 bool textRemoved( false );
2344 if( EventData::SELECTING == mImpl->mEventData->mState )
2346 std::string removedString;
2347 mImpl->RetrieveSelection( removedString, true );
2349 if( !removedString.empty() )
2352 mImpl->ChangeState( EventData::EDITING );
2359 void Controller::TapEvent( unsigned int tapCount, float x, float y )
2361 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
2363 if( NULL != mImpl->mEventData )
2365 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
2367 if( 1u == tapCount )
2369 // This is to avoid unnecessary relayouts when tapping an empty text-field
2370 bool relayoutNeeded( false );
2372 if( ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
2373 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
2375 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE ); // If Popup shown hide it here so can be shown again if required.
2378 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
2380 // Already in an active state so show a popup
2381 if( !mImpl->IsClipboardEmpty() )
2383 // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
2384 mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
2388 // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2389 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2391 relayoutNeeded = true;
2395 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2397 // Hide placeholder text
2401 if( EventData::INACTIVE == mImpl->mEventData->mState )
2403 mImpl->ChangeState( EventData::EDITING );
2405 else if( !mImpl->IsClipboardEmpty() )
2407 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2409 relayoutNeeded = true;
2412 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2413 if( relayoutNeeded )
2415 Event event( Event::TAP_EVENT );
2416 event.p1.mUint = tapCount;
2417 event.p2.mFloat = x;
2418 event.p3.mFloat = y;
2419 mImpl->mEventData->mEventQueue.push_back( event );
2421 mImpl->RequestRelayout();
2424 else if( 2u == tapCount )
2426 if( mImpl->mEventData->mSelectionEnabled &&
2427 mImpl->IsShowingRealText() )
2429 SelectEvent( x, y, false );
2434 // Reset keyboard as tap event has occurred.
2435 mImpl->ResetImfManager();
2438 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2440 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2442 if( NULL != mImpl->mEventData )
2444 Event event( Event::PAN_EVENT );
2445 event.p1.mInt = state;
2446 event.p2.mFloat = displacement.x;
2447 event.p3.mFloat = displacement.y;
2448 mImpl->mEventData->mEventQueue.push_back( event );
2450 mImpl->RequestRelayout();
2454 void Controller::LongPressEvent( Gesture::State state, float x, float y )
2456 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2458 if( ( state == Gesture::Started ) &&
2459 ( NULL != mImpl->mEventData ) )
2461 if( !mImpl->IsShowingRealText() )
2463 Event event( Event::LONG_PRESS_EVENT );
2464 event.p1.mInt = state;
2465 mImpl->mEventData->mEventQueue.push_back( event );
2466 mImpl->RequestRelayout();
2470 // The 1st long-press on inactive text-field is treated as tap
2471 if( EventData::INACTIVE == mImpl->mEventData->mState )
2473 mImpl->ChangeState( EventData::EDITING );
2475 Event event( Event::TAP_EVENT );
2477 event.p2.mFloat = x;
2478 event.p3.mFloat = y;
2479 mImpl->mEventData->mEventQueue.push_back( event );
2481 mImpl->RequestRelayout();
2485 // Reset the imf manger to commit the pre-edit before selecting the text.
2486 mImpl->ResetImfManager();
2488 SelectEvent( x, y, false );
2494 void Controller::SelectEvent( float x, float y, bool selectAll )
2496 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2498 if( NULL != mImpl->mEventData )
2502 Event event( Event::SELECT_ALL );
2503 mImpl->mEventData->mEventQueue.push_back( event );
2507 Event event( Event::SELECT );
2508 event.p2.mFloat = x;
2509 event.p3.mFloat = y;
2510 mImpl->mEventData->mEventQueue.push_back( event );
2513 mImpl->RequestRelayout();
2517 void Controller::GetTargetSize( Vector2& targetSize )
2519 targetSize = mImpl->mVisualModel->mControlSize;
2522 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2524 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2527 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2529 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2531 if( NULL != mImpl->mEventData )
2533 switch( handleType )
2537 Event event( Event::GRAB_HANDLE_EVENT );
2538 event.p1.mUint = state;
2539 event.p2.mFloat = x;
2540 event.p3.mFloat = y;
2542 mImpl->mEventData->mEventQueue.push_back( event );
2545 case LEFT_SELECTION_HANDLE:
2547 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2548 event.p1.mUint = state;
2549 event.p2.mFloat = x;
2550 event.p3.mFloat = y;
2552 mImpl->mEventData->mEventQueue.push_back( event );
2555 case RIGHT_SELECTION_HANDLE:
2557 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2558 event.p1.mUint = state;
2559 event.p2.mFloat = x;
2560 event.p3.mFloat = y;
2562 mImpl->mEventData->mEventQueue.push_back( event );
2565 case LEFT_SELECTION_HANDLE_MARKER:
2566 case RIGHT_SELECTION_HANDLE_MARKER:
2568 // Markers do not move the handles.
2571 case HANDLE_TYPE_COUNT:
2573 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2577 mImpl->RequestRelayout();
2581 void Controller::PasteText( const std::string& stringToPaste )
2583 InsertText( stringToPaste, Text::Controller::COMMIT );
2584 mImpl->ChangeState( EventData::EDITING );
2585 mImpl->RequestRelayout();
2587 // Do this last since it provides callbacks into application code
2588 mImpl->mControlInterface.TextChanged();
2591 void Controller::PasteClipboardItemEvent()
2593 // Retrieve the clipboard contents first
2594 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2595 std::string stringToPaste( notifier.GetContent() );
2597 // Commit the current pre-edit text; the contents of the clipboard should be appended
2598 mImpl->ResetImfManager();
2600 // Temporary disable hiding clipboard
2601 mImpl->SetClipboardHideEnable( false );
2604 PasteText( stringToPaste );
2606 mImpl->SetClipboardHideEnable( true );
2609 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2611 if( NULL == mImpl->mEventData )
2618 case Toolkit::TextSelectionPopup::CUT:
2620 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2621 mImpl->mOperationsPending = ALL_OPERATIONS;
2623 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2624 !mImpl->IsPlaceholderAvailable() )
2626 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2630 ShowPlaceholderText();
2633 mImpl->mEventData->mUpdateCursorPosition = true;
2634 mImpl->mEventData->mScrollAfterDelete = true;
2636 mImpl->RequestRelayout();
2637 mImpl->mControlInterface.TextChanged();
2640 case Toolkit::TextSelectionPopup::COPY:
2642 mImpl->SendSelectionToClipboard( false ); // Text not modified
2644 mImpl->mEventData->mUpdateCursorPosition = true;
2646 mImpl->RequestRelayout(); // Cursor, Handles, Selection Highlight, Popup
2649 case Toolkit::TextSelectionPopup::PASTE:
2651 std::string stringToPaste("");
2652 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2653 PasteText( stringToPaste );
2656 case Toolkit::TextSelectionPopup::SELECT:
2658 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2660 if( mImpl->mEventData->mSelectionEnabled )
2662 // Creates a SELECT event.
2663 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2667 case Toolkit::TextSelectionPopup::SELECT_ALL:
2669 // Creates a SELECT_ALL event
2670 SelectEvent( 0.f, 0.f, true );
2673 case Toolkit::TextSelectionPopup::CLIPBOARD:
2675 mImpl->ShowClipboard();
2678 case Toolkit::TextSelectionPopup::NONE:
2686 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2688 // Whether the text needs to be relaid-out.
2689 bool requestRelayout = false;
2691 // Whether to retrieve the text and cursor position to be sent to the IMF manager.
2692 bool retrieveText = false;
2693 bool retrieveCursor = false;
2695 switch( imfEvent.eventName )
2697 case ImfManager::COMMIT:
2699 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2700 requestRelayout = true;
2701 retrieveCursor = true;
2704 case ImfManager::PREEDIT:
2706 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2707 requestRelayout = true;
2708 retrieveCursor = true;
2711 case ImfManager::DELETESURROUNDING:
2713 const bool textDeleted = RemoveText( imfEvent.cursorOffset,
2714 imfEvent.numberOfChars,
2715 DONT_UPDATE_INPUT_STYLE );
2719 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2720 !mImpl->IsPlaceholderAvailable() )
2722 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2726 ShowPlaceholderText();
2728 mImpl->mEventData->mUpdateCursorPosition = true;
2729 mImpl->mEventData->mScrollAfterDelete = true;
2731 requestRelayout = true;
2735 case ImfManager::GETSURROUNDING:
2737 retrieveText = true;
2738 retrieveCursor = true;
2741 case ImfManager::VOID:
2748 if( requestRelayout )
2750 mImpl->mOperationsPending = ALL_OPERATIONS;
2751 mImpl->RequestRelayout();
2753 // Do this last since it provides callbacks into application code
2754 mImpl->mControlInterface.TextChanged();
2758 CharacterIndex cursorPosition = 0u;
2759 Length numberOfWhiteSpaces = 0u;
2761 if( retrieveCursor )
2763 numberOfWhiteSpaces = mImpl->GetNumberOfWhiteSpaces( 0u );
2765 cursorPosition = mImpl->GetLogicalCursorPosition();
2767 if( cursorPosition < numberOfWhiteSpaces )
2769 cursorPosition = 0u;
2773 cursorPosition -= numberOfWhiteSpaces;
2779 mImpl->GetText( numberOfWhiteSpaces, text );
2782 ImfManager::ImfCallbackData callbackData( ( retrieveText || retrieveCursor ), cursorPosition, text, false );
2784 return callbackData;
2787 Controller::~Controller()
2792 bool Controller::BackspaceKeyEvent()
2794 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2796 bool removed = false;
2798 if( NULL == mImpl->mEventData )
2803 // IMF manager is no longer handling key-events
2804 mImpl->ClearPreEditFlag();
2806 if( EventData::SELECTING == mImpl->mEventData->mState )
2808 removed = RemoveSelectedText();
2810 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2812 // Remove the character before the current cursor position
2813 removed = RemoveText( -1,
2815 UPDATE_INPUT_STYLE );
2820 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2821 !mImpl->IsPlaceholderAvailable() )
2823 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2827 ShowPlaceholderText();
2829 mImpl->mEventData->mUpdateCursorPosition = true;
2830 mImpl->mEventData->mScrollAfterDelete = true;
2836 void Controller::ShowPlaceholderText()
2838 if( mImpl->IsPlaceholderAvailable() )
2840 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2842 if( NULL == mImpl->mEventData )
2847 mImpl->mEventData->mIsShowingPlaceholderText = true;
2849 // Disable handles when showing place-holder text
2850 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2851 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2852 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2854 const char* text( NULL );
2857 // TODO - Switch placeholder text styles when changing state
2858 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2859 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2861 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2862 size = mImpl->mEventData->mPlaceholderTextActive.size();
2866 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2867 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2870 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2871 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2873 // Reset model for showing placeholder.
2874 mImpl->mLogicalModel->mText.Clear();
2875 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2877 // Convert text into UTF-32
2878 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2879 utf32Characters.Resize( size );
2881 // This is a bit horrible but std::string returns a (signed) char*
2882 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2884 // Transform a text array encoded in utf8 into an array encoded in utf32.
2885 // It returns the actual number of characters.
2886 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2887 utf32Characters.Resize( characterCount );
2889 // The characters to be added.
2890 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = characterCount;
2892 // Reset the cursor position
2893 mImpl->mEventData->mPrimaryCursorPosition = 0;
2895 // The natural size needs to be re-calculated.
2896 mImpl->mRecalculateNaturalSize = true;
2898 // Apply modifications to the model
2899 mImpl->mOperationsPending = ALL_OPERATIONS;
2901 // Update the rest of the model during size negotiation
2902 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2906 void Controller::ClearFontData()
2908 if( mImpl->mFontDefaults )
2910 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2913 // Set flags to update the model.
2914 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2915 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2916 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
2918 mImpl->mTextUpdateInfo.mClearAll = true;
2919 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
2920 mImpl->mRecalculateNaturalSize = true;
2922 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2927 UPDATE_LAYOUT_SIZE |
2932 void Controller::ClearStyleData()
2934 mImpl->mLogicalModel->mColorRuns.Clear();
2935 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2938 Controller::Controller( ControlInterface& controlInterface )
2941 mImpl = new Controller::Impl( controlInterface );
2946 } // namespace Toolkit