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;
97 return fontDescriptionRun;
100 ControllerPtr Controller::New( ControlInterface& controlInterface )
102 return ControllerPtr( new Controller( controlInterface ) );
105 void Controller::EnableTextInput( DecoratorPtr decorator )
107 if( NULL == mImpl->mEventData )
109 mImpl->mEventData = new EventData( decorator );
113 void Controller::SetGlyphType( TextAbstraction::GlyphType glyphType )
115 // Metrics for bitmap & vector based glyphs are different
116 mImpl->mMetrics->SetGlyphType( glyphType );
118 // Clear the font-specific data
121 mImpl->RequestRelayout();
124 void Controller::SetMarkupProcessorEnabled( bool enable )
126 mImpl->mMarkupProcessorEnabled = enable;
129 bool Controller::IsMarkupProcessorEnabled() const
131 return mImpl->mMarkupProcessorEnabled;
134 void Controller::SetAutoScrollEnabled( bool enable )
136 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 );
138 if ( mImpl->mLayoutEngine.GetLayout() == LayoutEngine::SINGLE_LINE_BOX )
142 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled for SINGLE_LINE_BOX\n" );
143 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
153 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled Disabling autoscroll\n");
154 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
161 mImpl->mAutoScrollEnabled = enable;
162 mImpl->RequestRelayout();
166 DALI_LOG_WARNING( "Attempted AutoScrolling on a non SINGLE_LINE_BOX, request ignored" );
167 mImpl->mAutoScrollEnabled = false;
171 bool Controller::IsAutoScrollEnabled() const
173 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::IsAutoScrollEnabled[%s]\n", (mImpl->mAutoScrollEnabled)?"true":"false" );
175 return mImpl->mAutoScrollEnabled;
178 CharacterDirection Controller::GetAutoScrollDirection() const
180 return mImpl->mAutoScrollDirectionRTL;
183 float Controller::GetAutoScrollLineAlignment() const
187 if( mImpl->mVisualModel &&
188 ( 0u != mImpl->mVisualModel->mLines.Count() ) )
190 offset = ( *mImpl->mVisualModel->mLines.Begin() ).alignmentOffset;
196 void Controller::SetText( const std::string& text )
198 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
200 // Reset keyboard as text changed
201 mImpl->ResetImfManager();
203 // Remove the previously set text and style.
209 CharacterIndex lastCursorIndex = 0u;
211 if( NULL != mImpl->mEventData )
213 // If popup shown then hide it by switching to Editing state
214 if( ( EventData::SELECTING == mImpl->mEventData->mState ) ||
215 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
216 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
217 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
219 mImpl->ChangeState( EventData::EDITING );
225 mImpl->mVisualModel->SetTextColor( mImpl->mTextColor );
227 MarkupProcessData markupProcessData( mImpl->mLogicalModel->mColorRuns,
228 mImpl->mLogicalModel->mFontDescriptionRuns );
230 Length textSize = 0u;
231 const uint8_t* utf8 = NULL;
232 if( mImpl->mMarkupProcessorEnabled )
234 ProcessMarkupString( text, markupProcessData );
235 textSize = markupProcessData.markupProcessedText.size();
237 // This is a bit horrible but std::string returns a (signed) char*
238 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
242 textSize = text.size();
244 // This is a bit horrible but std::string returns a (signed) char*
245 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
248 // Convert text into UTF-32
249 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
250 utf32Characters.Resize( textSize );
252 // Transform a text array encoded in utf8 into an array encoded in utf32.
253 // It returns the actual number of characters.
254 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
255 utf32Characters.Resize( characterCount );
257 DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
258 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mLogicalModel->mText.Count() );
260 // The characters to be added.
261 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
263 // To reset the cursor position
264 lastCursorIndex = characterCount;
266 // Update the rest of the model during size negotiation
267 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
269 // The natural size needs to be re-calculated.
270 mImpl->mRecalculateNaturalSize = true;
272 // Apply modifications to the model
273 mImpl->mOperationsPending = ALL_OPERATIONS;
277 ShowPlaceholderText();
280 // Resets the cursor position.
281 ResetCursorPosition( lastCursorIndex );
283 // Scrolls the text to make the cursor visible.
284 ResetScrollPosition();
286 mImpl->RequestRelayout();
288 if( NULL != mImpl->mEventData )
290 // Cancel previously queued events
291 mImpl->mEventData->mEventQueue.clear();
294 // Do this last since it provides callbacks into application code
295 mImpl->mControlInterface.TextChanged();
298 void Controller::GetText( std::string& text ) const
300 if( !mImpl->IsShowingPlaceholderText() )
302 // Retrieves the text string.
303 mImpl->GetText( 0u, text );
307 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
311 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
313 if( NULL != mImpl->mEventData )
315 if( PLACEHOLDER_TYPE_INACTIVE == type )
317 mImpl->mEventData->mPlaceholderTextInactive = text;
321 mImpl->mEventData->mPlaceholderTextActive = text;
324 // Update placeholder if there is no text
325 if( mImpl->IsShowingPlaceholderText() ||
326 ( 0u == mImpl->mLogicalModel->mText.Count() ) )
328 ShowPlaceholderText();
333 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
335 if( NULL != mImpl->mEventData )
337 if( PLACEHOLDER_TYPE_INACTIVE == type )
339 text = mImpl->mEventData->mPlaceholderTextInactive;
343 text = mImpl->mEventData->mPlaceholderTextActive;
348 void Controller::SetMaximumNumberOfCharacters( Length maxCharacters )
350 mImpl->mMaximumNumberOfCharacters = maxCharacters;
353 int Controller::GetMaximumNumberOfCharacters()
355 return mImpl->mMaximumNumberOfCharacters;
358 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
360 if( NULL == mImpl->mFontDefaults )
362 mImpl->mFontDefaults = new FontDefaults();
365 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
366 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
367 mImpl->mFontDefaults->familyDefined = true;
369 // Clear the font-specific data
372 mImpl->RequestRelayout();
375 const std::string& Controller::GetDefaultFontFamily() const
377 if( NULL != mImpl->mFontDefaults )
379 return mImpl->mFontDefaults->mFontDescription.family;
385 void Controller::SetDefaultFontStyle( const std::string& style )
387 if( NULL == mImpl->mFontDefaults )
389 mImpl->mFontDefaults = new FontDefaults();
392 mImpl->mFontDefaults->mFontStyle = style;
395 const std::string& Controller::GetDefaultFontStyle() const
397 if( NULL != mImpl->mFontDefaults )
399 return mImpl->mFontDefaults->mFontStyle;
405 void Controller::SetDefaultFontWeight( FontWeight weight )
407 if( NULL == mImpl->mFontDefaults )
409 mImpl->mFontDefaults = new FontDefaults();
412 mImpl->mFontDefaults->mFontDescription.weight = weight;
413 mImpl->mFontDefaults->weightDefined = true;
415 // Clear the font-specific data
418 mImpl->RequestRelayout();
421 FontWeight Controller::GetDefaultFontWeight() const
423 if( NULL != mImpl->mFontDefaults )
425 return mImpl->mFontDefaults->mFontDescription.weight;
428 return TextAbstraction::FontWeight::NORMAL;
431 void Controller::SetDefaultFontWidth( FontWidth width )
433 if( NULL == mImpl->mFontDefaults )
435 mImpl->mFontDefaults = new FontDefaults();
438 mImpl->mFontDefaults->mFontDescription.width = width;
439 mImpl->mFontDefaults->widthDefined = true;
441 // Clear the font-specific data
444 mImpl->RequestRelayout();
447 FontWidth Controller::GetDefaultFontWidth() const
449 if( NULL != mImpl->mFontDefaults )
451 return mImpl->mFontDefaults->mFontDescription.width;
454 return TextAbstraction::FontWidth::NORMAL;
457 void Controller::SetDefaultFontSlant( FontSlant slant )
459 if( NULL == mImpl->mFontDefaults )
461 mImpl->mFontDefaults = new FontDefaults();
464 mImpl->mFontDefaults->mFontDescription.slant = slant;
465 mImpl->mFontDefaults->slantDefined = true;
467 // Clear the font-specific data
470 mImpl->RequestRelayout();
473 FontSlant Controller::GetDefaultFontSlant() const
475 if( NULL != mImpl->mFontDefaults )
477 return mImpl->mFontDefaults->mFontDescription.slant;
480 return TextAbstraction::FontSlant::NORMAL;
483 void Controller::SetDefaultPointSize( float pointSize )
485 if( NULL == mImpl->mFontDefaults )
487 mImpl->mFontDefaults = new FontDefaults();
490 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
491 mImpl->mFontDefaults->sizeDefined = true;
493 // Clear the font-specific data
496 mImpl->RequestRelayout();
499 float Controller::GetDefaultPointSize() const
501 if( NULL != mImpl->mFontDefaults )
503 return mImpl->mFontDefaults->mDefaultPointSize;
509 void Controller::UpdateAfterFontChange( const std::string& newDefaultFont )
511 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::UpdateAfterFontChange");
513 if( !mImpl->mFontDefaults->familyDefined ) // If user defined font then should not update when system font changes
515 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
516 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
520 mImpl->RequestRelayout();
524 void Controller::SetTextColor( const Vector4& textColor )
526 mImpl->mTextColor = textColor;
528 if( !mImpl->IsShowingPlaceholderText() )
530 mImpl->mVisualModel->SetTextColor( textColor );
532 mImpl->RequestRelayout();
536 const Vector4& Controller::GetTextColor() const
538 return mImpl->mTextColor;
541 bool Controller::RemoveText( int cursorOffset,
542 int numberOfCharacters,
543 UpdateInputStyleType type )
545 bool removed = false;
547 if( NULL == mImpl->mEventData )
552 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfCharacters %d\n",
553 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters );
555 if( !mImpl->IsShowingPlaceholderText() )
557 // Delete at current cursor position
558 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
559 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
561 CharacterIndex cursorIndex = oldCursorIndex;
563 // Validate the cursor position & number of characters
564 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
566 cursorIndex = oldCursorIndex + cursorOffset;
569 if( ( cursorIndex + numberOfCharacters ) > currentText.Count() )
571 numberOfCharacters = currentText.Count() - cursorIndex;
574 if( ( cursorIndex + numberOfCharacters ) <= mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters )
576 // Mark the paragraphs to be updated.
577 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
578 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove += numberOfCharacters;
580 // Update the input style and remove the text's style before removing the text.
582 if( UPDATE_INPUT_STYLE == type )
584 // Set first the default input style.
585 mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
587 // Update the input style.
588 mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
591 // Updates the text style runs by removing characters. Runs with no characters are removed.
592 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfCharacters );
594 // Remove the characters.
595 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
596 Vector<Character>::Iterator last = first + numberOfCharacters;
598 currentText.Erase( first, last );
600 // Cursor position retreat
601 oldCursorIndex = cursorIndex;
603 mImpl->mEventData->mScrollAfterDelete = true;
605 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfCharacters );
613 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
615 if( NULL != mImpl->mEventData )
617 mImpl->mEventData->mPlaceholderTextColor = textColor;
620 if( mImpl->IsShowingPlaceholderText() )
622 mImpl->mVisualModel->SetTextColor( textColor );
623 mImpl->RequestRelayout();
627 const Vector4& Controller::GetPlaceholderTextColor() const
629 if( NULL != mImpl->mEventData )
631 return mImpl->mEventData->mPlaceholderTextColor;
637 void Controller::SetShadowOffset( const Vector2& shadowOffset )
639 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
641 mImpl->RequestRelayout();
644 const Vector2& Controller::GetShadowOffset() const
646 return mImpl->mVisualModel->GetShadowOffset();
649 void Controller::SetShadowColor( const Vector4& shadowColor )
651 mImpl->mVisualModel->SetShadowColor( shadowColor );
653 mImpl->RequestRelayout();
656 const Vector4& Controller::GetShadowColor() const
658 return mImpl->mVisualModel->GetShadowColor();
661 void Controller::SetDefaultShadowProperties( const std::string& shadowProperties )
663 if( NULL == mImpl->mShadowDefaults )
665 mImpl->mShadowDefaults = new ShadowDefaults();
668 mImpl->mShadowDefaults->properties = shadowProperties;
671 const std::string& Controller::GetDefaultShadowProperties() const
673 if( NULL != mImpl->mShadowDefaults )
675 return mImpl->mShadowDefaults->properties;
681 void Controller::SetUnderlineColor( const Vector4& color )
683 mImpl->mVisualModel->SetUnderlineColor( color );
685 mImpl->RequestRelayout();
688 const Vector4& Controller::GetUnderlineColor() const
690 return mImpl->mVisualModel->GetUnderlineColor();
693 void Controller::SetUnderlineEnabled( bool enabled )
695 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
697 mImpl->RequestRelayout();
700 bool Controller::IsUnderlineEnabled() const
702 return mImpl->mVisualModel->IsUnderlineEnabled();
705 void Controller::SetUnderlineHeight( float height )
707 mImpl->mVisualModel->SetUnderlineHeight( height );
709 mImpl->RequestRelayout();
712 float Controller::GetUnderlineHeight() const
714 return mImpl->mVisualModel->GetUnderlineHeight();
717 void Controller::SetDefaultUnderlineProperties( const std::string& underlineProperties )
719 if( NULL == mImpl->mUnderlineDefaults )
721 mImpl->mUnderlineDefaults = new UnderlineDefaults();
724 mImpl->mUnderlineDefaults->properties = underlineProperties;
727 const std::string& Controller::GetDefaultUnderlineProperties() const
729 if( NULL != mImpl->mUnderlineDefaults )
731 return mImpl->mUnderlineDefaults->properties;
737 void Controller::SetDefaultEmbossProperties( const std::string& embossProperties )
739 if( NULL == mImpl->mEmbossDefaults )
741 mImpl->mEmbossDefaults = new EmbossDefaults();
744 mImpl->mEmbossDefaults->properties = embossProperties;
747 const std::string& Controller::GetDefaultEmbossProperties() const
749 if( NULL != mImpl->mEmbossDefaults )
751 return mImpl->mEmbossDefaults->properties;
757 void Controller::SetDefaultOutlineProperties( const std::string& outlineProperties )
759 if( NULL == mImpl->mOutlineDefaults )
761 mImpl->mOutlineDefaults = new OutlineDefaults();
764 mImpl->mOutlineDefaults->properties = outlineProperties;
767 const std::string& Controller::GetDefaultOutlineProperties() const
769 if( NULL != mImpl->mOutlineDefaults )
771 return mImpl->mOutlineDefaults->properties;
777 void Controller::SetDefaultLineSpacing( float lineSpacing )
779 //TODO finish implementation
780 mImpl->mLayoutEngine.SetDefaultLineSpacing( lineSpacing );
783 float Controller::GetDefaultLineSpacing() const
785 return mImpl->mLayoutEngine.GetDefaultLineSpacing();
788 void Controller::SetInputColor( const Vector4& color )
790 if( NULL != mImpl->mEventData )
792 mImpl->mEventData->mInputStyle.textColor = color;
793 mImpl->mEventData->mInputStyle.isDefaultColor = false;
795 if( EventData::SELECTING == mImpl->mEventData->mState )
797 const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
799 // Get start and end position of selection
800 const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
801 const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
803 // Add the color run.
804 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
805 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
807 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
808 colorRun.color = color;
809 colorRun.characterRun.characterIndex = startOfSelectedText;
810 colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
812 // Request to relayout.
813 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
814 mImpl->RequestRelayout();
816 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
817 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
818 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
823 const Vector4& Controller::GetInputColor() const
825 if( NULL != mImpl->mEventData )
827 return mImpl->mEventData->mInputStyle.textColor;
830 // Return the default text's color if there is no EventData.
831 return mImpl->mTextColor;
835 void Controller::SetInputFontFamily( const std::string& fontFamily )
837 if( NULL != mImpl->mEventData )
839 mImpl->mEventData->mInputStyle.familyName = fontFamily;
840 mImpl->mEventData->mInputStyle.familyDefined = true;
842 if( EventData::SELECTING == mImpl->mEventData->mState )
844 CharacterIndex startOfSelectedText = 0u;
845 Length lengthOfSelectedText = 0u;
846 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
847 mImpl->mLogicalModel,
849 lengthOfSelectedText );
851 fontDescriptionRun.familyLength = fontFamily.size();
852 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
853 memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
854 fontDescriptionRun.familyDefined = true;
856 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
858 // Request to relayout.
859 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
867 mImpl->mRecalculateNaturalSize = true;
868 mImpl->RequestRelayout();
870 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
871 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
872 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
874 // As the font changes, recalculate the handle positions is needed.
875 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
876 mImpl->mEventData->mUpdateRightSelectionPosition = true;
877 mImpl->mEventData->mScrollAfterUpdatePosition = true;
882 const std::string& Controller::GetInputFontFamily() const
884 if( NULL != mImpl->mEventData )
886 return mImpl->mEventData->mInputStyle.familyName;
889 // Return the default font's family if there is no EventData.
890 return GetDefaultFontFamily();
893 void Controller::SetInputFontStyle( const std::string& fontStyle )
895 if( NULL != mImpl->mEventData )
897 mImpl->mEventData->mInputStyle.fontStyle = fontStyle;
901 const std::string& Controller::GetInputFontStyle() const
903 if( NULL != mImpl->mEventData )
905 return mImpl->mEventData->mInputStyle.fontStyle;
908 // Return the default font's style if there is no EventData.
909 return GetDefaultFontStyle();
912 void Controller::SetInputFontWeight( FontWeight weight )
914 if( NULL != mImpl->mEventData )
916 mImpl->mEventData->mInputStyle.weight = weight;
917 mImpl->mEventData->mInputStyle.weightDefined = true;
919 if( EventData::SELECTING == mImpl->mEventData->mState )
921 CharacterIndex startOfSelectedText = 0u;
922 Length lengthOfSelectedText = 0u;
923 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
924 mImpl->mLogicalModel,
926 lengthOfSelectedText );
928 fontDescriptionRun.weight = weight;
929 fontDescriptionRun.weightDefined = true;
931 // Request to relayout.
932 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
940 mImpl->mRecalculateNaturalSize = true;
941 mImpl->RequestRelayout();
943 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
944 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
945 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
947 // As the font might change, recalculate the handle positions is needed.
948 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
949 mImpl->mEventData->mUpdateRightSelectionPosition = true;
950 mImpl->mEventData->mScrollAfterUpdatePosition = true;
955 FontWeight Controller::GetInputFontWeight() const
957 if( NULL != mImpl->mEventData )
959 return mImpl->mEventData->mInputStyle.weight;
962 return GetDefaultFontWeight();
965 void Controller::SetInputFontWidth( FontWidth width )
967 if( NULL != mImpl->mEventData )
969 mImpl->mEventData->mInputStyle.width = width;
970 mImpl->mEventData->mInputStyle.widthDefined = true;
972 if( EventData::SELECTING == mImpl->mEventData->mState )
974 CharacterIndex startOfSelectedText = 0u;
975 Length lengthOfSelectedText = 0u;
976 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
977 mImpl->mLogicalModel,
979 lengthOfSelectedText );
981 fontDescriptionRun.width = width;
982 fontDescriptionRun.widthDefined = true;
984 // Request to relayout.
985 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
993 mImpl->mRecalculateNaturalSize = true;
994 mImpl->RequestRelayout();
996 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
997 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
998 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1000 // As the font might change, recalculate the handle positions is needed.
1001 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1002 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1003 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1008 FontWidth Controller::GetInputFontWidth() const
1010 if( NULL != mImpl->mEventData )
1012 return mImpl->mEventData->mInputStyle.width;
1015 return GetDefaultFontWidth();
1018 void Controller::SetInputFontSlant( FontSlant slant )
1020 if( NULL != mImpl->mEventData )
1022 mImpl->mEventData->mInputStyle.slant = slant;
1023 mImpl->mEventData->mInputStyle.slantDefined = true;
1025 if( EventData::SELECTING == mImpl->mEventData->mState )
1027 CharacterIndex startOfSelectedText = 0u;
1028 Length lengthOfSelectedText = 0u;
1029 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1030 mImpl->mLogicalModel,
1031 startOfSelectedText,
1032 lengthOfSelectedText );
1034 fontDescriptionRun.slant = slant;
1035 fontDescriptionRun.slantDefined = true;
1037 // Request to relayout.
1038 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1043 UPDATE_ACTUAL_SIZE |
1046 mImpl->mRecalculateNaturalSize = true;
1047 mImpl->RequestRelayout();
1049 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1050 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1051 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1053 // As the font might change, recalculate the handle positions is needed.
1054 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1055 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1056 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1061 FontSlant Controller::GetInputFontSlant() const
1063 if( NULL != mImpl->mEventData )
1065 return mImpl->mEventData->mInputStyle.slant;
1068 return GetDefaultFontSlant();
1071 void Controller::SetInputFontPointSize( float size )
1073 if( NULL != mImpl->mEventData )
1075 mImpl->mEventData->mInputStyle.size = size;
1077 if( EventData::SELECTING == mImpl->mEventData->mState )
1079 CharacterIndex startOfSelectedText = 0u;
1080 Length lengthOfSelectedText = 0u;
1081 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1082 mImpl->mLogicalModel,
1083 startOfSelectedText,
1084 lengthOfSelectedText );
1086 fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
1087 fontDescriptionRun.sizeDefined = true;
1089 // Request to relayout.
1090 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1095 UPDATE_ACTUAL_SIZE |
1098 mImpl->mRecalculateNaturalSize = true;
1099 mImpl->RequestRelayout();
1101 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1102 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1103 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1105 // As the font might change, recalculate the handle positions is needed.
1106 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1107 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1108 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1113 float Controller::GetInputFontPointSize() const
1115 if( NULL != mImpl->mEventData )
1117 return mImpl->mEventData->mInputStyle.size;
1120 // Return the default font's point size if there is no EventData.
1121 return GetDefaultPointSize();
1124 void Controller::SetInputLineSpacing( float lineSpacing )
1126 if( NULL != mImpl->mEventData )
1128 mImpl->mEventData->mInputStyle.lineSpacing = lineSpacing;
1132 float Controller::GetInputLineSpacing() const
1134 if( NULL != mImpl->mEventData )
1136 return mImpl->mEventData->mInputStyle.lineSpacing;
1142 void Controller::SetInputShadowProperties( const std::string& shadowProperties )
1144 if( NULL != mImpl->mEventData )
1146 mImpl->mEventData->mInputStyle.shadowProperties = shadowProperties;
1150 const std::string& Controller::GetInputShadowProperties() const
1152 if( NULL != mImpl->mEventData )
1154 return mImpl->mEventData->mInputStyle.shadowProperties;
1157 return GetDefaultShadowProperties();
1160 void Controller::SetInputUnderlineProperties( const std::string& underlineProperties )
1162 if( NULL != mImpl->mEventData )
1164 mImpl->mEventData->mInputStyle.underlineProperties = underlineProperties;
1168 const std::string& Controller::GetInputUnderlineProperties() const
1170 if( NULL != mImpl->mEventData )
1172 return mImpl->mEventData->mInputStyle.underlineProperties;
1175 return GetDefaultUnderlineProperties();
1178 void Controller::SetInputEmbossProperties( const std::string& embossProperties )
1180 if( NULL != mImpl->mEventData )
1182 mImpl->mEventData->mInputStyle.embossProperties = embossProperties;
1186 const std::string& Controller::GetInputEmbossProperties() const
1188 if( NULL != mImpl->mEventData )
1190 return mImpl->mEventData->mInputStyle.embossProperties;
1193 return GetDefaultEmbossProperties();
1196 void Controller::SetInputOutlineProperties( const std::string& outlineProperties )
1198 if( NULL != mImpl->mEventData )
1200 mImpl->mEventData->mInputStyle.outlineProperties = outlineProperties;
1204 const std::string& Controller::GetInputOutlineProperties() const
1206 if( NULL != mImpl->mEventData )
1208 return mImpl->mEventData->mInputStyle.outlineProperties;
1211 return GetDefaultOutlineProperties();
1214 void Controller::SetEnableCursorBlink( bool enable )
1216 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
1218 if( NULL != mImpl->mEventData )
1220 mImpl->mEventData->mCursorBlinkEnabled = enable;
1223 mImpl->mEventData->mDecorator )
1225 mImpl->mEventData->mDecorator->StopCursorBlink();
1230 bool Controller::GetEnableCursorBlink() const
1232 if( NULL != mImpl->mEventData )
1234 return mImpl->mEventData->mCursorBlinkEnabled;
1240 const Vector2& Controller::GetScrollPosition() const
1242 return mImpl->mScrollPosition;
1245 Vector3 Controller::GetNaturalSize()
1247 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
1248 Vector3 naturalSize;
1250 // Make sure the model is up-to-date before layouting
1251 ProcessModifyEvents();
1253 if( mImpl->mRecalculateNaturalSize )
1255 // Operations that can be done only once until the text changes.
1256 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1263 GET_GLYPH_METRICS );
1264 // Make sure the model is up-to-date before layouting
1265 mImpl->UpdateModel( onlyOnceOperations );
1267 // Layout the text for the new width.
1268 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1270 // Set the update info to relayout the whole text.
1271 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1272 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1274 // Store the actual control's size to restore later.
1275 const Size actualControlSize = mImpl->mVisualModel->mControlSize;
1277 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
1278 static_cast<OperationsMask>( onlyOnceOperations |
1280 naturalSize.GetVectorXY() );
1282 // Do not do again the only once operations.
1283 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1285 // Do the size related operations again.
1286 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1289 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1291 // Stores the natural size to avoid recalculate it again
1292 // unless the text/style changes.
1293 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
1295 mImpl->mRecalculateNaturalSize = false;
1297 // Clear the update info. This info will be set the next time the text is updated.
1298 mImpl->mTextUpdateInfo.Clear();
1300 // Restore the actual control's size.
1301 mImpl->mVisualModel->mControlSize = actualControlSize;
1303 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1307 naturalSize = mImpl->mVisualModel->GetNaturalSize();
1309 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1312 naturalSize.x = ConvertToEven( naturalSize.x );
1313 naturalSize.y = ConvertToEven( naturalSize.y );
1318 float Controller::GetHeightForWidth( float width )
1320 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1321 // Make sure the model is up-to-date before layouting
1322 ProcessModifyEvents();
1325 if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
1327 // Operations that can be done only once until the text changes.
1328 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1335 GET_GLYPH_METRICS );
1336 // Make sure the model is up-to-date before layouting
1337 mImpl->UpdateModel( onlyOnceOperations );
1340 // Layout the text for the new width.
1341 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1343 // Set the update info to relayout the whole text.
1344 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1345 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1347 // Store the actual control's width.
1348 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
1350 DoRelayout( Size( width, MAX_FLOAT ),
1351 static_cast<OperationsMask>( onlyOnceOperations |
1355 // Do not do again the only once operations.
1356 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1358 // Do the size related operations again.
1359 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1363 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1365 // Clear the update info. This info will be set the next time the text is updated.
1366 mImpl->mTextUpdateInfo.Clear();
1368 // Restore the actual control's width.
1369 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1371 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1375 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1376 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1379 return layoutSize.height;
1382 bool Controller::Relayout( const Size& size )
1384 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", this, size.width, size.height, (mImpl->mAutoScrollEnabled)?"true":"false" );
1386 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1388 bool glyphsRemoved( false );
1389 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1391 mImpl->mVisualModel->mGlyphPositions.Clear();
1392 glyphsRemoved = true;
1395 // Clear the update info. This info will be set the next time the text is updated.
1396 mImpl->mTextUpdateInfo.Clear();
1398 // Not worth to relayout if width or height is equal to zero.
1399 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1401 return glyphsRemoved;
1404 // Whether a new size has been set.
1405 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1409 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1411 // Layout operations that need to be done if the size changes.
1412 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1415 UPDATE_ACTUAL_SIZE |
1417 // Set the update info to relayout the whole text.
1418 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
1419 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1422 // Whether there are modify events.
1423 if( 0u != mImpl->mModifyEvents.Count() )
1425 // Style operations that need to be done if the text is modified.
1426 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1430 // Make sure the model is up-to-date before layouting.
1431 ProcessModifyEvents();
1432 bool updated = mImpl->UpdateModel( mImpl->mOperationsPending );
1436 updated = DoRelayout( size,
1437 mImpl->mOperationsPending,
1438 layoutSize ) || updated;
1440 // Do not re-do any operation until something changes.
1441 mImpl->mOperationsPending = NO_OPERATION;
1443 // Whether the text control is editable
1444 const bool isEditable = NULL != mImpl->mEventData;
1446 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
1448 if( newSize && isEditable )
1450 offset = mImpl->mScrollPosition;
1453 if( !isEditable || !IsMultiLineEnabled() )
1455 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
1456 CalculateVerticalOffset( size );
1463 // If there is a new size, the scroll position needs to be clamped.
1464 mImpl->ClampHorizontalScroll( layoutSize );
1466 // Update the decorator's positions is needed if there is a new size.
1467 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mScrollPosition - offset );
1470 // Move the cursor, grab handle etc.
1471 updated = mImpl->ProcessInputEvents() || updated;
1474 // Clear the update info. This info will be set the next time the text is updated.
1475 mImpl->mTextUpdateInfo.Clear();
1476 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1481 void Controller::ProcessModifyEvents()
1483 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
1485 if( 0u == events.Count() )
1491 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
1492 endIt = events.End();
1496 const ModifyEvent& event = *it;
1498 if( ModifyEvent::TEXT_REPLACED == event.type )
1500 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
1501 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
1503 TextReplacedEvent();
1505 else if( ModifyEvent::TEXT_INSERTED == event.type )
1507 TextInsertedEvent();
1509 else if( ModifyEvent::TEXT_DELETED == event.type )
1511 // Placeholder-text cannot be deleted
1512 if( !mImpl->IsShowingPlaceholderText() )
1519 if( NULL != mImpl->mEventData )
1521 // When the text is being modified, delay cursor blinking
1522 mImpl->mEventData->mDecorator->DelayCursorBlink();
1525 // Discard temporary text
1529 void Controller::ResetText()
1532 mImpl->mLogicalModel->mText.Clear();
1534 // We have cleared everything including the placeholder-text
1535 mImpl->PlaceholderCleared();
1537 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1538 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1539 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
1541 // Clear any previous text.
1542 mImpl->mTextUpdateInfo.mClearAll = true;
1544 // The natural size needs to be re-calculated.
1545 mImpl->mRecalculateNaturalSize = true;
1547 // Apply modifications to the model
1548 mImpl->mOperationsPending = ALL_OPERATIONS;
1551 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
1553 // Reset the cursor position
1554 if( NULL != mImpl->mEventData )
1556 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
1558 // Update the cursor if it's in editing mode.
1559 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1561 mImpl->mEventData->mUpdateCursorPosition = true;
1566 void Controller::ResetScrollPosition()
1568 if( NULL != mImpl->mEventData )
1570 // Reset the scroll position.
1571 mImpl->mScrollPosition = Vector2::ZERO;
1572 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1576 void Controller::TextReplacedEvent()
1578 // The natural size needs to be re-calculated.
1579 mImpl->mRecalculateNaturalSize = true;
1581 // Apply modifications to the model
1582 mImpl->mOperationsPending = ALL_OPERATIONS;
1585 void Controller::TextInsertedEvent()
1587 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
1589 if( NULL == mImpl->mEventData )
1594 // The natural size needs to be re-calculated.
1595 mImpl->mRecalculateNaturalSize = true;
1597 // Apply modifications to the model; TODO - Optimize this
1598 mImpl->mOperationsPending = ALL_OPERATIONS;
1601 void Controller::TextDeletedEvent()
1603 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
1605 if( NULL == mImpl->mEventData )
1610 // The natural size needs to be re-calculated.
1611 mImpl->mRecalculateNaturalSize = true;
1613 // Apply modifications to the model; TODO - Optimize this
1614 mImpl->mOperationsPending = ALL_OPERATIONS;
1617 bool Controller::DoRelayout( const Size& size,
1618 OperationsMask operationsRequired,
1621 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
1622 bool viewUpdated( false );
1624 // Calculate the operations to be done.
1625 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
1627 const CharacterIndex startIndex = mImpl->mTextUpdateInfo.mParagraphCharacterIndex;
1628 const Length requestedNumberOfCharacters = mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters;
1630 if( NO_OPERATION != ( LAYOUT & operations ) )
1632 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
1634 // Some vectors with data needed to layout and reorder may be void
1635 // after the first time the text has been laid out.
1636 // Fill the vectors again.
1638 // Calculate the number of glyphs to layout.
1639 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1640 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1641 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
1642 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
1644 const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
1645 const GlyphIndex startGlyphIndex = mImpl->mTextUpdateInfo.mStartGlyphIndex;
1646 const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
1647 const Length totalNumberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
1649 if( 0u == totalNumberOfGlyphs )
1651 if( NO_OPERATION != ( UPDATE_ACTUAL_SIZE & operations ) )
1653 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
1656 // Nothing else to do if there is no glyphs.
1657 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1661 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1662 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1663 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1664 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1665 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1666 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1667 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1669 // Set the layout parameters.
1670 LayoutParameters layoutParameters( size,
1672 lineBreakInfo.Begin(),
1673 wordBreakInfo.Begin(),
1674 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1676 glyphsToCharactersMap.Begin(),
1677 charactersPerGlyph.Begin(),
1678 charactersToGlyphBuffer,
1679 glyphsPerCharacterBuffer,
1680 totalNumberOfGlyphs );
1682 // Resize the vector of positions to have the same size than the vector of glyphs.
1683 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1684 glyphPositions.Resize( totalNumberOfGlyphs );
1686 // Whether the last character is a new paragraph character.
1687 mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1688 layoutParameters.isLastNewParagraph = mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph;
1690 // The initial glyph and the number of glyphs to layout.
1691 layoutParameters.startGlyphIndex = startGlyphIndex;
1692 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1693 layoutParameters.startLineIndex = mImpl->mTextUpdateInfo.mStartLineIndex;
1694 layoutParameters.estimatedNumberOfLines = mImpl->mTextUpdateInfo.mEstimatedNumberOfLines;
1696 // Update the visual model.
1697 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1699 mImpl->mVisualModel->mLines,
1705 if ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) )
1707 mImpl->mAutoScrollDirectionRTL = false;
1710 // Reorder the lines
1711 if( NO_OPERATION != ( REORDER & operations ) )
1713 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1714 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1716 // Check first if there are paragraphs with bidirectional info.
1717 if( 0u != bidirectionalInfo.Count() )
1720 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1722 // Reorder the lines.
1723 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1724 ReorderLines( bidirectionalInfo,
1726 requestedNumberOfCharacters,
1727 mImpl->mVisualModel->mLines,
1728 bidirectionalLineInfo );
1730 // Set the bidirectional info per line into the layout parameters.
1731 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1732 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1734 // Re-layout the text. Reorder those lines with right to left characters.
1735 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1737 requestedNumberOfCharacters,
1740 if ( ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) ) && ( numberOfLines > 0 ) )
1742 const LineRun* const firstline = mImpl->mVisualModel->mLines.Begin();
1745 mImpl->mAutoScrollDirectionRTL = firstline->direction;
1751 // Sets the actual size.
1752 if( NO_OPERATION != ( UPDATE_ACTUAL_SIZE & operations ) )
1754 mImpl->mVisualModel->SetLayoutSize( layoutSize );
1758 // Store the size used to layout the text.
1759 mImpl->mVisualModel->mControlSize = size;
1763 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1766 if( NO_OPERATION != ( ALIGN & operations ) )
1768 // The laid-out lines.
1769 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1771 mImpl->mLayoutEngine.Align( size,
1773 requestedNumberOfCharacters,
1778 #if defined(DEBUG_ENABLED)
1779 std::string currentText;
1780 GetText( currentText );
1781 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mAutoScrollDirectionRTL[%s] [%s]\n", this, (mImpl->mAutoScrollDirectionRTL)?"true":"false", currentText.c_str() );
1783 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1787 void Controller::SetMultiLineEnabled( bool enable )
1789 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1791 if( layout != mImpl->mLayoutEngine.GetLayout() )
1793 // Set the layout type.
1794 mImpl->mLayoutEngine.SetLayout( layout );
1796 // Set the flags to redo the layout operations
1797 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1798 UPDATE_ACTUAL_SIZE |
1802 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1804 mImpl->RequestRelayout();
1808 bool Controller::IsMultiLineEnabled() const
1810 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1813 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1815 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1817 // Set the alignment.
1818 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1820 // Set the flag to redo the alignment operation.
1821 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1823 mImpl->RequestRelayout();
1827 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1829 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1832 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1834 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1836 // Set the alignment.
1837 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1839 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1841 mImpl->RequestRelayout();
1845 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1847 return mImpl->mLayoutEngine.GetVerticalAlignment();
1850 void Controller::CalculateVerticalOffset( const Size& controlSize )
1852 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1854 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1856 // Get the line height of the default font.
1857 layoutSize.height = mImpl->GetDefaultFontLineHeight();
1860 switch( mImpl->mLayoutEngine.GetVerticalAlignment() )
1862 case LayoutEngine::VERTICAL_ALIGN_TOP:
1864 mImpl->mScrollPosition.y = 0.f;
1867 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1869 mImpl->mScrollPosition.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
1872 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1874 mImpl->mScrollPosition.y = controlSize.height - layoutSize.height;
1880 LayoutEngine& Controller::GetLayoutEngine()
1882 return mImpl->mLayoutEngine;
1885 View& Controller::GetView()
1887 return mImpl->mView;
1890 void Controller::KeyboardFocusGainEvent()
1892 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1894 if( NULL != mImpl->mEventData )
1896 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1897 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1899 mImpl->ChangeState( EventData::EDITING );
1900 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1903 if( mImpl->IsShowingPlaceholderText() )
1905 // Show alternative placeholder-text when editing
1906 ShowPlaceholderText();
1909 mImpl->RequestRelayout();
1913 void Controller::KeyboardFocusLostEvent()
1915 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1917 if( NULL != mImpl->mEventData )
1919 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1921 mImpl->ChangeState( EventData::INACTIVE );
1923 if( !mImpl->IsShowingRealText() )
1925 // Revert to regular placeholder-text when not editing
1926 ShowPlaceholderText();
1930 mImpl->RequestRelayout();
1933 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1935 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1937 bool textChanged( false );
1939 if( ( NULL != mImpl->mEventData ) &&
1940 ( keyEvent.state == KeyEvent::Down ) )
1942 int keyCode = keyEvent.keyCode;
1943 const std::string& keyString = keyEvent.keyPressed;
1945 // Pre-process to separate modifying events from non-modifying input events.
1946 if( Dali::DALI_KEY_ESCAPE == keyCode )
1948 // Escape key is a special case which causes focus loss
1949 KeyboardFocusLostEvent();
1951 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
1952 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1953 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
1954 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
1956 Event event( Event::CURSOR_KEY_EVENT );
1957 event.p1.mInt = keyCode;
1958 mImpl->mEventData->mEventQueue.push_back( event );
1960 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1962 textChanged = BackspaceKeyEvent();
1964 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1966 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1967 // Avoids calling the InsertText() method which can delete selected text
1969 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1970 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1972 mImpl->ChangeState( EventData::INACTIVE );
1973 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1974 // Avoids calling the InsertText() method which can delete selected text
1976 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1978 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1979 // and a character is typed after the type of a upper case latin character.
1985 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1987 // IMF manager is no longer handling key-events
1988 mImpl->ClearPreEditFlag();
1990 InsertText( keyString, COMMIT );
1994 if ( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1995 ( mImpl->mEventData->mState != EventData::INACTIVE ) &&
1996 ( Dali::DALI_KEY_SHIFT_LEFT != keyCode ) )
1998 // Should not change the state if the key is the shift send by the imf manager.
1999 // Otherwise, when the state is SELECTING the text controller can't send the right
2000 // surrounding info to the imf.
2001 mImpl->ChangeState( EventData::EDITING );
2004 mImpl->RequestRelayout();
2009 // Do this last since it provides callbacks into application code
2010 mImpl->mControlInterface.TextChanged();
2016 void Controller::InsertText( const std::string& text, Controller::InsertType type )
2018 bool removedPrevious( false );
2019 bool maxLengthReached( false );
2021 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
2023 if( NULL == mImpl->mEventData )
2028 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
2029 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
2030 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2032 // TODO: At the moment the underline runs are only for pre-edit.
2033 mImpl->mVisualModel->mUnderlineRuns.Clear();
2035 // Keep the current number of characters.
2036 const Length currentNumberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2038 // Remove the previous IMF pre-edit.
2039 if( mImpl->mEventData->mPreEditFlag && ( 0u != mImpl->mEventData->mPreEditLength ) )
2041 removedPrevious = RemoveText( -static_cast<int>( mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition ),
2042 mImpl->mEventData->mPreEditLength,
2043 DONT_UPDATE_INPUT_STYLE );
2045 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
2046 mImpl->mEventData->mPreEditLength = 0u;
2050 // Remove the previous Selection.
2051 removedPrevious = RemoveSelectedText();
2054 Vector<Character> utf32Characters;
2055 Length characterCount = 0u;
2059 // Convert text into UTF-32
2060 utf32Characters.Resize( text.size() );
2062 // This is a bit horrible but std::string returns a (signed) char*
2063 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
2065 // Transform a text array encoded in utf8 into an array encoded in utf32.
2066 // It returns the actual number of characters.
2067 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
2068 utf32Characters.Resize( characterCount );
2070 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
2071 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
2074 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
2076 // The placeholder text is no longer needed
2077 if( mImpl->IsShowingPlaceholderText() )
2082 mImpl->ChangeState( EventData::EDITING );
2084 // Handle the IMF (predicitive text) state changes
2085 if( COMMIT == type )
2087 // IMF manager is no longer handling key-events
2088 mImpl->ClearPreEditFlag();
2092 if( !mImpl->mEventData->mPreEditFlag )
2094 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
2096 // Record the start of the pre-edit text
2097 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
2100 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
2101 mImpl->mEventData->mPreEditFlag = true;
2103 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2106 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
2108 // Restrict new text to fit within Maximum characters setting.
2109 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
2110 maxLengthReached = ( characterCount > maxSizeOfNewText );
2112 // The cursor position.
2113 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
2115 // Update the text's style.
2117 // Updates the text style runs by adding characters.
2118 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
2120 // Get the character index from the cursor index.
2121 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
2123 // Retrieve the text's style for the given index.
2125 mImpl->RetrieveDefaultInputStyle( style );
2126 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
2128 // Whether to add a new text color run.
2129 const bool addColorRun = ( style.textColor != mImpl->mEventData->mInputStyle.textColor );
2131 // Whether to add a new font run.
2132 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
2133 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
2134 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
2135 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
2136 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
2141 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
2142 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
2144 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
2145 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
2146 colorRun.characterRun.characterIndex = cursorIndex;
2147 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2150 if( addFontNameRun ||
2156 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
2157 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
2159 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
2161 if( addFontNameRun )
2163 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
2164 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
2165 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
2166 fontDescriptionRun.familyDefined = true;
2168 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
2171 if( addFontWeightRun )
2173 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
2174 fontDescriptionRun.weightDefined = true;
2177 if( addFontWidthRun )
2179 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
2180 fontDescriptionRun.widthDefined = true;
2183 if( addFontSlantRun )
2185 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
2186 fontDescriptionRun.slantDefined = true;
2189 if( addFontSizeRun )
2191 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
2192 fontDescriptionRun.sizeDefined = true;
2195 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
2196 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2199 // Insert at current cursor position.
2200 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
2202 if( cursorIndex < numberOfCharactersInModel )
2204 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2208 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2211 // Mark the first paragraph to be updated.
2212 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
2213 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
2215 // Update the cursor index.
2216 cursorIndex += maxSizeOfNewText;
2218 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
2221 const Length numberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2223 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
2224 mImpl->IsPlaceholderAvailable() )
2226 // Show place-holder if empty after removing the pre-edit text
2227 ShowPlaceholderText();
2228 mImpl->mEventData->mUpdateCursorPosition = true;
2229 mImpl->ClearPreEditFlag();
2231 else if( removedPrevious ||
2232 ( 0 != utf32Characters.Count() ) )
2234 // Queue an inserted event
2235 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
2237 mImpl->mEventData->mUpdateCursorPosition = true;
2238 if( numberOfCharacters < currentNumberOfCharacters )
2240 mImpl->mEventData->mScrollAfterDelete = true;
2244 mImpl->mEventData->mScrollAfterUpdatePosition = true;
2248 if( maxLengthReached )
2250 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
2252 mImpl->ResetImfManager();
2254 // Do this last since it provides callbacks into application code
2255 mImpl->mControlInterface.MaxLengthReached();
2259 bool Controller::RemoveSelectedText()
2261 bool textRemoved( false );
2263 if( EventData::SELECTING == mImpl->mEventData->mState )
2265 std::string removedString;
2266 mImpl->RetrieveSelection( removedString, true );
2268 if( !removedString.empty() )
2271 mImpl->ChangeState( EventData::EDITING );
2278 void Controller::TapEvent( unsigned int tapCount, float x, float y )
2280 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
2282 if( NULL != mImpl->mEventData )
2284 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
2286 if( 1u == tapCount )
2288 // This is to avoid unnecessary relayouts when tapping an empty text-field
2289 bool relayoutNeeded( false );
2291 if( ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
2292 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
2294 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE ); // If Popup shown hide it here so can be shown again if required.
2297 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
2299 // Already in an active state so show a popup
2300 if( !mImpl->IsClipboardEmpty() )
2302 // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
2303 mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
2307 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2309 relayoutNeeded = true;
2313 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2315 // Hide placeholder text
2319 if( EventData::INACTIVE == mImpl->mEventData->mState )
2321 mImpl->ChangeState( EventData::EDITING );
2323 else if( !mImpl->IsClipboardEmpty() )
2325 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2327 relayoutNeeded = true;
2330 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2331 if( relayoutNeeded )
2333 Event event( Event::TAP_EVENT );
2334 event.p1.mUint = tapCount;
2335 event.p2.mFloat = x;
2336 event.p3.mFloat = y;
2337 mImpl->mEventData->mEventQueue.push_back( event );
2339 mImpl->RequestRelayout();
2342 else if( 2u == tapCount )
2344 if( mImpl->mEventData->mSelectionEnabled &&
2345 mImpl->IsShowingRealText() )
2347 SelectEvent( x, y, false );
2352 // Reset keyboard as tap event has occurred.
2353 mImpl->ResetImfManager();
2356 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2357 // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2359 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2361 if( NULL != mImpl->mEventData )
2363 Event event( Event::PAN_EVENT );
2364 event.p1.mInt = state;
2365 event.p2.mFloat = displacement.x;
2366 event.p3.mFloat = displacement.y;
2367 mImpl->mEventData->mEventQueue.push_back( event );
2369 mImpl->RequestRelayout();
2373 void Controller::LongPressEvent( Gesture::State state, float x, float y )
2375 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2377 if( ( state == Gesture::Started ) &&
2378 ( NULL != mImpl->mEventData ) )
2380 if( !mImpl->IsShowingRealText() )
2382 Event event( Event::LONG_PRESS_EVENT );
2383 event.p1.mInt = state;
2384 mImpl->mEventData->mEventQueue.push_back( event );
2385 mImpl->RequestRelayout();
2389 // The 1st long-press on inactive text-field is treated as tap
2390 if( EventData::INACTIVE == mImpl->mEventData->mState )
2392 mImpl->ChangeState( EventData::EDITING );
2394 Event event( Event::TAP_EVENT );
2396 event.p2.mFloat = x;
2397 event.p3.mFloat = y;
2398 mImpl->mEventData->mEventQueue.push_back( event );
2400 mImpl->RequestRelayout();
2404 // Reset the imf manger to commit the pre-edit before selecting the text.
2405 mImpl->ResetImfManager();
2407 SelectEvent( x, y, false );
2413 void Controller::SelectEvent( float x, float y, bool selectAll )
2415 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2417 if( NULL != mImpl->mEventData )
2419 mImpl->ChangeState( EventData::SELECTING );
2423 Event event( Event::SELECT_ALL );
2424 mImpl->mEventData->mEventQueue.push_back( event );
2428 Event event( Event::SELECT );
2429 event.p2.mFloat = x;
2430 event.p3.mFloat = y;
2431 mImpl->mEventData->mEventQueue.push_back( event );
2434 mImpl->RequestRelayout();
2438 void Controller::GetTargetSize( Vector2& targetSize )
2440 targetSize = mImpl->mVisualModel->mControlSize;
2443 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2445 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2448 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2450 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2452 if( NULL != mImpl->mEventData )
2454 switch( handleType )
2458 Event event( Event::GRAB_HANDLE_EVENT );
2459 event.p1.mUint = state;
2460 event.p2.mFloat = x;
2461 event.p3.mFloat = y;
2463 mImpl->mEventData->mEventQueue.push_back( event );
2466 case LEFT_SELECTION_HANDLE:
2468 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2469 event.p1.mUint = state;
2470 event.p2.mFloat = x;
2471 event.p3.mFloat = y;
2473 mImpl->mEventData->mEventQueue.push_back( event );
2476 case RIGHT_SELECTION_HANDLE:
2478 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2479 event.p1.mUint = state;
2480 event.p2.mFloat = x;
2481 event.p3.mFloat = y;
2483 mImpl->mEventData->mEventQueue.push_back( event );
2486 case LEFT_SELECTION_HANDLE_MARKER:
2487 case RIGHT_SELECTION_HANDLE_MARKER:
2489 // Markers do not move the handles.
2492 case HANDLE_TYPE_COUNT:
2494 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2498 mImpl->RequestRelayout();
2502 void Controller::PasteText( const std::string& stringToPaste )
2504 InsertText( stringToPaste, Text::Controller::COMMIT );
2505 mImpl->ChangeState( EventData::EDITING );
2506 mImpl->RequestRelayout();
2508 // Do this last since it provides callbacks into application code
2509 mImpl->mControlInterface.TextChanged();
2512 void Controller::PasteClipboardItemEvent()
2514 // Retrieve the clipboard contents first
2515 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2516 std::string stringToPaste( notifier.GetContent() );
2518 // Commit the current pre-edit text; the contents of the clipboard should be appended
2519 mImpl->ResetImfManager();
2521 // Temporary disable hiding clipboard
2522 mImpl->SetClipboardHideEnable( false );
2525 PasteText( stringToPaste );
2527 mImpl->SetClipboardHideEnable( true );
2530 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2532 if( NULL == mImpl->mEventData )
2539 case Toolkit::TextSelectionPopup::CUT:
2541 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2542 mImpl->mOperationsPending = ALL_OPERATIONS;
2544 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2545 !mImpl->IsPlaceholderAvailable() )
2547 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2551 ShowPlaceholderText();
2554 mImpl->mEventData->mUpdateCursorPosition = true;
2555 mImpl->mEventData->mScrollAfterDelete = true;
2557 mImpl->RequestRelayout();
2558 mImpl->mControlInterface.TextChanged();
2561 case Toolkit::TextSelectionPopup::COPY:
2563 mImpl->SendSelectionToClipboard( false ); // Text not modified
2564 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
2567 case Toolkit::TextSelectionPopup::PASTE:
2569 std::string stringToPaste("");
2570 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2571 PasteText( stringToPaste );
2574 case Toolkit::TextSelectionPopup::SELECT:
2576 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2578 if( mImpl->mEventData->mSelectionEnabled )
2580 // Creates a SELECT event.
2581 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2585 case Toolkit::TextSelectionPopup::SELECT_ALL:
2587 // Creates a SELECT_ALL event
2588 SelectEvent( 0.f, 0.f, true );
2591 case Toolkit::TextSelectionPopup::CLIPBOARD:
2593 mImpl->ShowClipboard();
2596 case Toolkit::TextSelectionPopup::NONE:
2604 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2606 // Whether the text needs to be relaid-out.
2607 bool requestRelayout = false;
2609 // Whether to retrieve the text and cursor position to be sent to the IMF manager.
2610 bool retrieveText = false;
2611 bool retrieveCursor = false;
2613 switch( imfEvent.eventName )
2615 case ImfManager::COMMIT:
2617 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2618 requestRelayout = true;
2619 retrieveCursor = true;
2622 case ImfManager::PREEDIT:
2624 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2625 requestRelayout = true;
2626 retrieveCursor = true;
2629 case ImfManager::DELETESURROUNDING:
2631 const bool textDeleted = RemoveText( imfEvent.cursorOffset,
2632 imfEvent.numberOfChars,
2633 DONT_UPDATE_INPUT_STYLE );
2637 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2638 !mImpl->IsPlaceholderAvailable() )
2640 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2644 ShowPlaceholderText();
2646 mImpl->mEventData->mUpdateCursorPosition = true;
2647 mImpl->mEventData->mScrollAfterDelete = true;
2649 requestRelayout = true;
2650 retrieveCursor = true;
2654 case ImfManager::GETSURROUNDING:
2656 retrieveText = true;
2657 retrieveCursor = true;
2660 case ImfManager::VOID:
2667 if( requestRelayout )
2669 mImpl->mOperationsPending = ALL_OPERATIONS;
2670 mImpl->RequestRelayout();
2672 // Do this last since it provides callbacks into application code
2673 mImpl->mControlInterface.TextChanged();
2677 CharacterIndex cursorPosition = 0u;
2678 Length numberOfWhiteSpaces = 0u;
2680 if( retrieveCursor )
2682 numberOfWhiteSpaces = mImpl->GetNumberOfWhiteSpaces( 0u );
2684 cursorPosition = mImpl->GetLogicalCursorPosition();
2686 if( cursorPosition < numberOfWhiteSpaces )
2688 cursorPosition = 0u;
2692 cursorPosition -= numberOfWhiteSpaces;
2698 mImpl->GetText( numberOfWhiteSpaces, text );
2701 ImfManager::ImfCallbackData callbackData( ( retrieveText || retrieveCursor ), cursorPosition, text, false );
2703 return callbackData;
2706 Controller::~Controller()
2711 bool Controller::BackspaceKeyEvent()
2713 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2715 bool removed = false;
2717 if( NULL == mImpl->mEventData )
2722 // IMF manager is no longer handling key-events
2723 mImpl->ClearPreEditFlag();
2725 if( EventData::SELECTING == mImpl->mEventData->mState )
2727 removed = RemoveSelectedText();
2729 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2731 // Remove the character before the current cursor position
2732 removed = RemoveText( -1,
2734 UPDATE_INPUT_STYLE );
2739 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2740 !mImpl->IsPlaceholderAvailable() )
2742 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2746 ShowPlaceholderText();
2748 mImpl->mEventData->mUpdateCursorPosition = true;
2749 mImpl->mEventData->mScrollAfterDelete = true;
2755 void Controller::ShowPlaceholderText()
2757 if( mImpl->IsPlaceholderAvailable() )
2759 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2761 if( NULL == mImpl->mEventData )
2766 mImpl->mEventData->mIsShowingPlaceholderText = true;
2768 // Disable handles when showing place-holder text
2769 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2770 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2771 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2773 const char* text( NULL );
2776 // TODO - Switch placeholder text styles when changing state
2777 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2778 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2780 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2781 size = mImpl->mEventData->mPlaceholderTextActive.size();
2785 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2786 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2789 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2790 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2792 // Reset model for showing placeholder.
2793 mImpl->mLogicalModel->mText.Clear();
2794 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2796 // Convert text into UTF-32
2797 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2798 utf32Characters.Resize( size );
2800 // This is a bit horrible but std::string returns a (signed) char*
2801 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2803 // Transform a text array encoded in utf8 into an array encoded in utf32.
2804 // It returns the actual number of characters.
2805 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2806 utf32Characters.Resize( characterCount );
2808 // The characters to be added.
2809 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = characterCount;
2811 // Reset the cursor position
2812 mImpl->mEventData->mPrimaryCursorPosition = 0;
2814 // The natural size needs to be re-calculated.
2815 mImpl->mRecalculateNaturalSize = true;
2817 // Apply modifications to the model
2818 mImpl->mOperationsPending = ALL_OPERATIONS;
2820 // Update the rest of the model during size negotiation
2821 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2825 void Controller::ClearFontData()
2827 if( mImpl->mFontDefaults )
2829 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2832 // Set flags to update the model.
2833 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2834 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2835 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
2837 mImpl->mTextUpdateInfo.mClearAll = true;
2838 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
2839 mImpl->mRecalculateNaturalSize = true;
2841 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2846 UPDATE_ACTUAL_SIZE |
2851 void Controller::ClearStyleData()
2853 mImpl->mLogicalModel->mColorRuns.Clear();
2854 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2857 Controller::Controller( ControlInterface& controlInterface )
2860 mImpl = new Controller::Impl( controlInterface );
2865 } // namespace Toolkit