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 Controller::UpdateTextType 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 UpdateTextType updateTextType = NONE_UPDATED;
1388 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1390 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1392 mImpl->mVisualModel->mGlyphPositions.Clear();
1393 updateTextType = MODEL_UPDATED;
1396 // Clear the update info. This info will be set the next time the text is updated.
1397 mImpl->mTextUpdateInfo.Clear();
1399 // Not worth to relayout if width or height is equal to zero.
1400 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1402 return updateTextType;
1405 // Whether a new size has been set.
1406 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1410 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1412 // Layout operations that need to be done if the size changes.
1413 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1416 UPDATE_ACTUAL_SIZE |
1418 // Set the update info to relayout the whole text.
1419 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
1420 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1423 // Whether there are modify events.
1424 if( 0u != mImpl->mModifyEvents.Count() )
1426 // Style operations that need to be done if the text is modified.
1427 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1431 // Make sure the model is up-to-date before layouting.
1432 ProcessModifyEvents();
1433 bool updated = mImpl->UpdateModel( mImpl->mOperationsPending );
1437 updated = DoRelayout( size,
1438 mImpl->mOperationsPending,
1439 layoutSize ) || updated;
1443 updateTextType = MODEL_UPDATED;
1446 // Do not re-do any operation until something changes.
1447 mImpl->mOperationsPending = NO_OPERATION;
1449 // Whether the text control is editable
1450 const bool isEditable = NULL != mImpl->mEventData;
1452 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
1454 if( newSize && isEditable )
1456 offset = mImpl->mScrollPosition;
1459 if( !isEditable || !IsMultiLineEnabled() )
1461 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
1462 CalculateVerticalOffset( size );
1469 // If there is a new size, the scroll position needs to be clamped.
1470 mImpl->ClampHorizontalScroll( layoutSize );
1472 // Update the decorator's positions is needed if there is a new size.
1473 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mScrollPosition - offset );
1476 // Move the cursor, grab handle etc.
1477 if( mImpl->ProcessInputEvents() )
1479 updateTextType = static_cast<UpdateTextType>( updateTextType | DECORATOR_UPDATED );
1483 // Clear the update info. This info will be set the next time the text is updated.
1484 mImpl->mTextUpdateInfo.Clear();
1485 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1487 return updateTextType;
1490 void Controller::ProcessModifyEvents()
1492 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
1494 if( 0u == events.Count() )
1500 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
1501 endIt = events.End();
1505 const ModifyEvent& event = *it;
1507 if( ModifyEvent::TEXT_REPLACED == event.type )
1509 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
1510 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
1512 TextReplacedEvent();
1514 else if( ModifyEvent::TEXT_INSERTED == event.type )
1516 TextInsertedEvent();
1518 else if( ModifyEvent::TEXT_DELETED == event.type )
1520 // Placeholder-text cannot be deleted
1521 if( !mImpl->IsShowingPlaceholderText() )
1528 if( NULL != mImpl->mEventData )
1530 // When the text is being modified, delay cursor blinking
1531 mImpl->mEventData->mDecorator->DelayCursorBlink();
1534 // Discard temporary text
1538 void Controller::ResetText()
1541 mImpl->mLogicalModel->mText.Clear();
1543 // We have cleared everything including the placeholder-text
1544 mImpl->PlaceholderCleared();
1546 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1547 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1548 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
1550 // Clear any previous text.
1551 mImpl->mTextUpdateInfo.mClearAll = true;
1553 // The natural size needs to be re-calculated.
1554 mImpl->mRecalculateNaturalSize = true;
1556 // Apply modifications to the model
1557 mImpl->mOperationsPending = ALL_OPERATIONS;
1560 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
1562 // Reset the cursor position
1563 if( NULL != mImpl->mEventData )
1565 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
1567 // Update the cursor if it's in editing mode.
1568 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1570 mImpl->mEventData->mUpdateCursorPosition = true;
1575 void Controller::ResetScrollPosition()
1577 if( NULL != mImpl->mEventData )
1579 // Reset the scroll position.
1580 mImpl->mScrollPosition = Vector2::ZERO;
1581 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1585 void Controller::TextReplacedEvent()
1587 // The natural size needs to be re-calculated.
1588 mImpl->mRecalculateNaturalSize = true;
1590 // Apply modifications to the model
1591 mImpl->mOperationsPending = ALL_OPERATIONS;
1594 void Controller::TextInsertedEvent()
1596 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
1598 if( NULL == mImpl->mEventData )
1603 // The natural size needs to be re-calculated.
1604 mImpl->mRecalculateNaturalSize = true;
1606 // Apply modifications to the model; TODO - Optimize this
1607 mImpl->mOperationsPending = ALL_OPERATIONS;
1610 void Controller::TextDeletedEvent()
1612 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
1614 if( NULL == mImpl->mEventData )
1619 // The natural size needs to be re-calculated.
1620 mImpl->mRecalculateNaturalSize = true;
1622 // Apply modifications to the model; TODO - Optimize this
1623 mImpl->mOperationsPending = ALL_OPERATIONS;
1626 bool Controller::DoRelayout( const Size& size,
1627 OperationsMask operationsRequired,
1630 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
1631 bool viewUpdated( false );
1633 // Calculate the operations to be done.
1634 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
1636 const CharacterIndex startIndex = mImpl->mTextUpdateInfo.mParagraphCharacterIndex;
1637 const Length requestedNumberOfCharacters = mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters;
1639 if( NO_OPERATION != ( LAYOUT & operations ) )
1641 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
1643 // Some vectors with data needed to layout and reorder may be void
1644 // after the first time the text has been laid out.
1645 // Fill the vectors again.
1647 // Calculate the number of glyphs to layout.
1648 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1649 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1650 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
1651 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
1653 const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
1654 const GlyphIndex startGlyphIndex = mImpl->mTextUpdateInfo.mStartGlyphIndex;
1655 const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
1656 const Length totalNumberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
1658 if( 0u == totalNumberOfGlyphs )
1660 if( NO_OPERATION != ( UPDATE_ACTUAL_SIZE & operations ) )
1662 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
1665 // Nothing else to do if there is no glyphs.
1666 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1670 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1671 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1672 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1673 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1674 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1675 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1676 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1678 // Set the layout parameters.
1679 LayoutParameters layoutParameters( size,
1681 lineBreakInfo.Begin(),
1682 wordBreakInfo.Begin(),
1683 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1685 glyphsToCharactersMap.Begin(),
1686 charactersPerGlyph.Begin(),
1687 charactersToGlyphBuffer,
1688 glyphsPerCharacterBuffer,
1689 totalNumberOfGlyphs );
1691 // Resize the vector of positions to have the same size than the vector of glyphs.
1692 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1693 glyphPositions.Resize( totalNumberOfGlyphs );
1695 // Whether the last character is a new paragraph character.
1696 mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1697 layoutParameters.isLastNewParagraph = mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph;
1699 // The initial glyph and the number of glyphs to layout.
1700 layoutParameters.startGlyphIndex = startGlyphIndex;
1701 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1702 layoutParameters.startLineIndex = mImpl->mTextUpdateInfo.mStartLineIndex;
1703 layoutParameters.estimatedNumberOfLines = mImpl->mTextUpdateInfo.mEstimatedNumberOfLines;
1705 // Update the visual model.
1706 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1708 mImpl->mVisualModel->mLines,
1714 if ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) )
1716 mImpl->mAutoScrollDirectionRTL = false;
1719 // Reorder the lines
1720 if( NO_OPERATION != ( REORDER & operations ) )
1722 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1723 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1725 // Check first if there are paragraphs with bidirectional info.
1726 if( 0u != bidirectionalInfo.Count() )
1729 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1731 // Reorder the lines.
1732 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1733 ReorderLines( bidirectionalInfo,
1735 requestedNumberOfCharacters,
1736 mImpl->mVisualModel->mLines,
1737 bidirectionalLineInfo );
1739 // Set the bidirectional info per line into the layout parameters.
1740 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1741 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1743 // Re-layout the text. Reorder those lines with right to left characters.
1744 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1746 requestedNumberOfCharacters,
1749 if ( ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) ) && ( numberOfLines > 0 ) )
1751 const LineRun* const firstline = mImpl->mVisualModel->mLines.Begin();
1754 mImpl->mAutoScrollDirectionRTL = firstline->direction;
1760 // Sets the actual size.
1761 if( NO_OPERATION != ( UPDATE_ACTUAL_SIZE & operations ) )
1763 mImpl->mVisualModel->SetLayoutSize( layoutSize );
1767 // Store the size used to layout the text.
1768 mImpl->mVisualModel->mControlSize = size;
1772 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1775 if( NO_OPERATION != ( ALIGN & operations ) )
1777 // The laid-out lines.
1778 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1780 mImpl->mLayoutEngine.Align( size,
1782 requestedNumberOfCharacters,
1787 #if defined(DEBUG_ENABLED)
1788 std::string currentText;
1789 GetText( currentText );
1790 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mAutoScrollDirectionRTL[%s] [%s]\n", this, (mImpl->mAutoScrollDirectionRTL)?"true":"false", currentText.c_str() );
1792 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1796 void Controller::SetMultiLineEnabled( bool enable )
1798 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1800 if( layout != mImpl->mLayoutEngine.GetLayout() )
1802 // Set the layout type.
1803 mImpl->mLayoutEngine.SetLayout( layout );
1805 // Set the flags to redo the layout operations
1806 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1807 UPDATE_ACTUAL_SIZE |
1811 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1813 mImpl->RequestRelayout();
1817 bool Controller::IsMultiLineEnabled() const
1819 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1822 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1824 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1826 // Set the alignment.
1827 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1829 // Set the flag to redo the alignment operation.
1830 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1832 mImpl->RequestRelayout();
1836 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1838 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1841 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1843 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1845 // Set the alignment.
1846 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1848 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1850 mImpl->RequestRelayout();
1854 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1856 return mImpl->mLayoutEngine.GetVerticalAlignment();
1859 void Controller::CalculateVerticalOffset( const Size& controlSize )
1861 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1863 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1865 // Get the line height of the default font.
1866 layoutSize.height = mImpl->GetDefaultFontLineHeight();
1869 switch( mImpl->mLayoutEngine.GetVerticalAlignment() )
1871 case LayoutEngine::VERTICAL_ALIGN_TOP:
1873 mImpl->mScrollPosition.y = 0.f;
1876 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1878 mImpl->mScrollPosition.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
1881 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1883 mImpl->mScrollPosition.y = controlSize.height - layoutSize.height;
1889 LayoutEngine& Controller::GetLayoutEngine()
1891 return mImpl->mLayoutEngine;
1894 View& Controller::GetView()
1896 return mImpl->mView;
1899 void Controller::KeyboardFocusGainEvent()
1901 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1903 if( NULL != mImpl->mEventData )
1905 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1906 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1908 mImpl->ChangeState( EventData::EDITING );
1909 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1912 if( mImpl->IsShowingPlaceholderText() )
1914 // Show alternative placeholder-text when editing
1915 ShowPlaceholderText();
1918 mImpl->RequestRelayout();
1922 void Controller::KeyboardFocusLostEvent()
1924 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1926 if( NULL != mImpl->mEventData )
1928 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1930 mImpl->ChangeState( EventData::INACTIVE );
1932 if( !mImpl->IsShowingRealText() )
1934 // Revert to regular placeholder-text when not editing
1935 ShowPlaceholderText();
1939 mImpl->RequestRelayout();
1942 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1944 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1946 bool textChanged( false );
1948 if( ( NULL != mImpl->mEventData ) &&
1949 ( keyEvent.state == KeyEvent::Down ) )
1951 int keyCode = keyEvent.keyCode;
1952 const std::string& keyString = keyEvent.keyPressed;
1954 // Pre-process to separate modifying events from non-modifying input events.
1955 if( Dali::DALI_KEY_ESCAPE == keyCode )
1957 // Escape key is a special case which causes focus loss
1958 KeyboardFocusLostEvent();
1960 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
1961 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1962 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
1963 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
1965 Event event( Event::CURSOR_KEY_EVENT );
1966 event.p1.mInt = keyCode;
1967 mImpl->mEventData->mEventQueue.push_back( event );
1969 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1971 textChanged = BackspaceKeyEvent();
1973 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1975 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1976 // Avoids calling the InsertText() method which can delete selected text
1978 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1979 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1981 mImpl->ChangeState( EventData::INACTIVE );
1982 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1983 // Avoids calling the InsertText() method which can delete selected text
1985 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1987 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1988 // and a character is typed after the type of a upper case latin character.
1994 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1996 // IMF manager is no longer handling key-events
1997 mImpl->ClearPreEditFlag();
1999 InsertText( keyString, COMMIT );
2003 if ( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
2004 ( mImpl->mEventData->mState != EventData::INACTIVE ) &&
2005 ( Dali::DALI_KEY_SHIFT_LEFT != keyCode ) )
2007 // Should not change the state if the key is the shift send by the imf manager.
2008 // Otherwise, when the state is SELECTING the text controller can't send the right
2009 // surrounding info to the imf.
2010 mImpl->ChangeState( EventData::EDITING );
2013 mImpl->RequestRelayout();
2018 // Do this last since it provides callbacks into application code
2019 mImpl->mControlInterface.TextChanged();
2025 void Controller::InsertText( const std::string& text, Controller::InsertType type )
2027 bool removedPrevious( false );
2028 bool maxLengthReached( false );
2030 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
2032 if( NULL == mImpl->mEventData )
2037 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
2038 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
2039 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2041 // TODO: At the moment the underline runs are only for pre-edit.
2042 mImpl->mVisualModel->mUnderlineRuns.Clear();
2044 // Keep the current number of characters.
2045 const Length currentNumberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2047 // Remove the previous IMF pre-edit.
2048 if( mImpl->mEventData->mPreEditFlag && ( 0u != mImpl->mEventData->mPreEditLength ) )
2050 removedPrevious = RemoveText( -static_cast<int>( mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition ),
2051 mImpl->mEventData->mPreEditLength,
2052 DONT_UPDATE_INPUT_STYLE );
2054 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
2055 mImpl->mEventData->mPreEditLength = 0u;
2059 // Remove the previous Selection.
2060 removedPrevious = RemoveSelectedText();
2063 Vector<Character> utf32Characters;
2064 Length characterCount = 0u;
2068 // Convert text into UTF-32
2069 utf32Characters.Resize( text.size() );
2071 // This is a bit horrible but std::string returns a (signed) char*
2072 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
2074 // Transform a text array encoded in utf8 into an array encoded in utf32.
2075 // It returns the actual number of characters.
2076 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
2077 utf32Characters.Resize( characterCount );
2079 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
2080 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
2083 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
2085 // The placeholder text is no longer needed
2086 if( mImpl->IsShowingPlaceholderText() )
2091 mImpl->ChangeState( EventData::EDITING );
2093 // Handle the IMF (predicitive text) state changes
2094 if( COMMIT == type )
2096 // IMF manager is no longer handling key-events
2097 mImpl->ClearPreEditFlag();
2101 if( !mImpl->mEventData->mPreEditFlag )
2103 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
2105 // Record the start of the pre-edit text
2106 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
2109 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
2110 mImpl->mEventData->mPreEditFlag = true;
2112 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2115 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
2117 // Restrict new text to fit within Maximum characters setting.
2118 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
2119 maxLengthReached = ( characterCount > maxSizeOfNewText );
2121 // The cursor position.
2122 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
2124 // Update the text's style.
2126 // Updates the text style runs by adding characters.
2127 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
2129 // Get the character index from the cursor index.
2130 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
2132 // Retrieve the text's style for the given index.
2134 mImpl->RetrieveDefaultInputStyle( style );
2135 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
2137 // Whether to add a new text color run.
2138 const bool addColorRun = ( style.textColor != mImpl->mEventData->mInputStyle.textColor );
2140 // Whether to add a new font run.
2141 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
2142 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
2143 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
2144 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
2145 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
2150 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
2151 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
2153 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
2154 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
2155 colorRun.characterRun.characterIndex = cursorIndex;
2156 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2159 if( addFontNameRun ||
2165 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
2166 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
2168 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
2170 if( addFontNameRun )
2172 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
2173 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
2174 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
2175 fontDescriptionRun.familyDefined = true;
2177 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
2180 if( addFontWeightRun )
2182 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
2183 fontDescriptionRun.weightDefined = true;
2186 if( addFontWidthRun )
2188 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
2189 fontDescriptionRun.widthDefined = true;
2192 if( addFontSlantRun )
2194 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
2195 fontDescriptionRun.slantDefined = true;
2198 if( addFontSizeRun )
2200 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
2201 fontDescriptionRun.sizeDefined = true;
2204 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
2205 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2208 // Insert at current cursor position.
2209 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
2211 if( cursorIndex < numberOfCharactersInModel )
2213 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2217 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2220 // Mark the first paragraph to be updated.
2221 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
2222 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
2224 // Update the cursor index.
2225 cursorIndex += maxSizeOfNewText;
2227 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
2230 const Length numberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2232 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
2233 mImpl->IsPlaceholderAvailable() )
2235 // Show place-holder if empty after removing the pre-edit text
2236 ShowPlaceholderText();
2237 mImpl->mEventData->mUpdateCursorPosition = true;
2238 mImpl->ClearPreEditFlag();
2240 else if( removedPrevious ||
2241 ( 0 != utf32Characters.Count() ) )
2243 // Queue an inserted event
2244 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
2246 mImpl->mEventData->mUpdateCursorPosition = true;
2247 if( numberOfCharacters < currentNumberOfCharacters )
2249 mImpl->mEventData->mScrollAfterDelete = true;
2253 mImpl->mEventData->mScrollAfterUpdatePosition = true;
2257 if( maxLengthReached )
2259 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
2261 mImpl->ResetImfManager();
2263 // Do this last since it provides callbacks into application code
2264 mImpl->mControlInterface.MaxLengthReached();
2268 bool Controller::RemoveSelectedText()
2270 bool textRemoved( false );
2272 if( EventData::SELECTING == mImpl->mEventData->mState )
2274 std::string removedString;
2275 mImpl->RetrieveSelection( removedString, true );
2277 if( !removedString.empty() )
2280 mImpl->ChangeState( EventData::EDITING );
2287 void Controller::TapEvent( unsigned int tapCount, float x, float y )
2289 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
2291 if( NULL != mImpl->mEventData )
2293 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
2295 if( 1u == tapCount )
2297 // This is to avoid unnecessary relayouts when tapping an empty text-field
2298 bool relayoutNeeded( false );
2300 if( ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
2301 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
2303 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE ); // If Popup shown hide it here so can be shown again if required.
2306 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
2308 // Already in an active state so show a popup
2309 if( !mImpl->IsClipboardEmpty() )
2311 // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
2312 mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
2316 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2318 relayoutNeeded = true;
2322 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2324 // Hide placeholder text
2328 if( EventData::INACTIVE == mImpl->mEventData->mState )
2330 mImpl->ChangeState( EventData::EDITING );
2332 else if( !mImpl->IsClipboardEmpty() )
2334 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2336 relayoutNeeded = true;
2339 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2340 if( relayoutNeeded )
2342 Event event( Event::TAP_EVENT );
2343 event.p1.mUint = tapCount;
2344 event.p2.mFloat = x;
2345 event.p3.mFloat = y;
2346 mImpl->mEventData->mEventQueue.push_back( event );
2348 mImpl->RequestRelayout();
2351 else if( 2u == tapCount )
2353 if( mImpl->mEventData->mSelectionEnabled &&
2354 mImpl->IsShowingRealText() )
2356 SelectEvent( x, y, false );
2361 // Reset keyboard as tap event has occurred.
2362 mImpl->ResetImfManager();
2365 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2366 // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2368 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2370 if( NULL != mImpl->mEventData )
2372 Event event( Event::PAN_EVENT );
2373 event.p1.mInt = state;
2374 event.p2.mFloat = displacement.x;
2375 event.p3.mFloat = displacement.y;
2376 mImpl->mEventData->mEventQueue.push_back( event );
2378 mImpl->RequestRelayout();
2382 void Controller::LongPressEvent( Gesture::State state, float x, float y )
2384 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2386 if( ( state == Gesture::Started ) &&
2387 ( NULL != mImpl->mEventData ) )
2389 if( !mImpl->IsShowingRealText() )
2391 Event event( Event::LONG_PRESS_EVENT );
2392 event.p1.mInt = state;
2393 mImpl->mEventData->mEventQueue.push_back( event );
2394 mImpl->RequestRelayout();
2398 // The 1st long-press on inactive text-field is treated as tap
2399 if( EventData::INACTIVE == mImpl->mEventData->mState )
2401 mImpl->ChangeState( EventData::EDITING );
2403 Event event( Event::TAP_EVENT );
2405 event.p2.mFloat = x;
2406 event.p3.mFloat = y;
2407 mImpl->mEventData->mEventQueue.push_back( event );
2409 mImpl->RequestRelayout();
2413 // Reset the imf manger to commit the pre-edit before selecting the text.
2414 mImpl->ResetImfManager();
2416 SelectEvent( x, y, false );
2422 void Controller::SelectEvent( float x, float y, bool selectAll )
2424 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2426 if( NULL != mImpl->mEventData )
2428 mImpl->ChangeState( EventData::SELECTING );
2432 Event event( Event::SELECT_ALL );
2433 mImpl->mEventData->mEventQueue.push_back( event );
2437 Event event( Event::SELECT );
2438 event.p2.mFloat = x;
2439 event.p3.mFloat = y;
2440 mImpl->mEventData->mEventQueue.push_back( event );
2443 mImpl->RequestRelayout();
2447 void Controller::GetTargetSize( Vector2& targetSize )
2449 targetSize = mImpl->mVisualModel->mControlSize;
2452 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2454 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2457 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2459 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2461 if( NULL != mImpl->mEventData )
2463 switch( handleType )
2467 Event event( Event::GRAB_HANDLE_EVENT );
2468 event.p1.mUint = state;
2469 event.p2.mFloat = x;
2470 event.p3.mFloat = y;
2472 mImpl->mEventData->mEventQueue.push_back( event );
2475 case LEFT_SELECTION_HANDLE:
2477 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2478 event.p1.mUint = state;
2479 event.p2.mFloat = x;
2480 event.p3.mFloat = y;
2482 mImpl->mEventData->mEventQueue.push_back( event );
2485 case RIGHT_SELECTION_HANDLE:
2487 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2488 event.p1.mUint = state;
2489 event.p2.mFloat = x;
2490 event.p3.mFloat = y;
2492 mImpl->mEventData->mEventQueue.push_back( event );
2495 case LEFT_SELECTION_HANDLE_MARKER:
2496 case RIGHT_SELECTION_HANDLE_MARKER:
2498 // Markers do not move the handles.
2501 case HANDLE_TYPE_COUNT:
2503 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2507 mImpl->RequestRelayout();
2511 void Controller::PasteText( const std::string& stringToPaste )
2513 InsertText( stringToPaste, Text::Controller::COMMIT );
2514 mImpl->ChangeState( EventData::EDITING );
2515 mImpl->RequestRelayout();
2517 // Do this last since it provides callbacks into application code
2518 mImpl->mControlInterface.TextChanged();
2521 void Controller::PasteClipboardItemEvent()
2523 // Retrieve the clipboard contents first
2524 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2525 std::string stringToPaste( notifier.GetContent() );
2527 // Commit the current pre-edit text; the contents of the clipboard should be appended
2528 mImpl->ResetImfManager();
2530 // Temporary disable hiding clipboard
2531 mImpl->SetClipboardHideEnable( false );
2534 PasteText( stringToPaste );
2536 mImpl->SetClipboardHideEnable( true );
2539 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2541 if( NULL == mImpl->mEventData )
2548 case Toolkit::TextSelectionPopup::CUT:
2550 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2551 mImpl->mOperationsPending = ALL_OPERATIONS;
2553 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2554 !mImpl->IsPlaceholderAvailable() )
2556 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2560 ShowPlaceholderText();
2563 mImpl->mEventData->mUpdateCursorPosition = true;
2564 mImpl->mEventData->mScrollAfterDelete = true;
2566 mImpl->RequestRelayout();
2567 mImpl->mControlInterface.TextChanged();
2570 case Toolkit::TextSelectionPopup::COPY:
2572 mImpl->SendSelectionToClipboard( false ); // Text not modified
2573 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
2576 case Toolkit::TextSelectionPopup::PASTE:
2578 std::string stringToPaste("");
2579 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2580 PasteText( stringToPaste );
2583 case Toolkit::TextSelectionPopup::SELECT:
2585 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2587 if( mImpl->mEventData->mSelectionEnabled )
2589 // Creates a SELECT event.
2590 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2594 case Toolkit::TextSelectionPopup::SELECT_ALL:
2596 // Creates a SELECT_ALL event
2597 SelectEvent( 0.f, 0.f, true );
2600 case Toolkit::TextSelectionPopup::CLIPBOARD:
2602 mImpl->ShowClipboard();
2605 case Toolkit::TextSelectionPopup::NONE:
2613 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2615 // Whether the text needs to be relaid-out.
2616 bool requestRelayout = false;
2618 // Whether to retrieve the text and cursor position to be sent to the IMF manager.
2619 bool retrieveText = false;
2620 bool retrieveCursor = false;
2622 switch( imfEvent.eventName )
2624 case ImfManager::COMMIT:
2626 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2627 requestRelayout = true;
2628 retrieveCursor = true;
2631 case ImfManager::PREEDIT:
2633 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2634 requestRelayout = true;
2635 retrieveCursor = true;
2638 case ImfManager::DELETESURROUNDING:
2640 const bool textDeleted = RemoveText( imfEvent.cursorOffset,
2641 imfEvent.numberOfChars,
2642 DONT_UPDATE_INPUT_STYLE );
2646 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2647 !mImpl->IsPlaceholderAvailable() )
2649 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2653 ShowPlaceholderText();
2655 mImpl->mEventData->mUpdateCursorPosition = true;
2656 mImpl->mEventData->mScrollAfterDelete = true;
2658 requestRelayout = true;
2662 case ImfManager::GETSURROUNDING:
2664 retrieveText = true;
2665 retrieveCursor = true;
2668 case ImfManager::VOID:
2675 if( requestRelayout )
2677 mImpl->mOperationsPending = ALL_OPERATIONS;
2678 mImpl->RequestRelayout();
2680 // Do this last since it provides callbacks into application code
2681 mImpl->mControlInterface.TextChanged();
2685 CharacterIndex cursorPosition = 0u;
2686 Length numberOfWhiteSpaces = 0u;
2688 if( retrieveCursor )
2690 numberOfWhiteSpaces = mImpl->GetNumberOfWhiteSpaces( 0u );
2692 cursorPosition = mImpl->GetLogicalCursorPosition();
2694 if( cursorPosition < numberOfWhiteSpaces )
2696 cursorPosition = 0u;
2700 cursorPosition -= numberOfWhiteSpaces;
2706 mImpl->GetText( numberOfWhiteSpaces, text );
2709 ImfManager::ImfCallbackData callbackData( ( retrieveText || retrieveCursor ), cursorPosition, text, false );
2711 return callbackData;
2714 Controller::~Controller()
2719 bool Controller::BackspaceKeyEvent()
2721 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2723 bool removed = false;
2725 if( NULL == mImpl->mEventData )
2730 // IMF manager is no longer handling key-events
2731 mImpl->ClearPreEditFlag();
2733 if( EventData::SELECTING == mImpl->mEventData->mState )
2735 removed = RemoveSelectedText();
2737 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2739 // Remove the character before the current cursor position
2740 removed = RemoveText( -1,
2742 UPDATE_INPUT_STYLE );
2747 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2748 !mImpl->IsPlaceholderAvailable() )
2750 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2754 ShowPlaceholderText();
2756 mImpl->mEventData->mUpdateCursorPosition = true;
2757 mImpl->mEventData->mScrollAfterDelete = true;
2763 void Controller::ShowPlaceholderText()
2765 if( mImpl->IsPlaceholderAvailable() )
2767 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2769 if( NULL == mImpl->mEventData )
2774 mImpl->mEventData->mIsShowingPlaceholderText = true;
2776 // Disable handles when showing place-holder text
2777 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2778 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2779 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2781 const char* text( NULL );
2784 // TODO - Switch placeholder text styles when changing state
2785 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2786 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2788 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2789 size = mImpl->mEventData->mPlaceholderTextActive.size();
2793 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2794 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2797 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2798 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2800 // Reset model for showing placeholder.
2801 mImpl->mLogicalModel->mText.Clear();
2802 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2804 // Convert text into UTF-32
2805 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2806 utf32Characters.Resize( size );
2808 // This is a bit horrible but std::string returns a (signed) char*
2809 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2811 // Transform a text array encoded in utf8 into an array encoded in utf32.
2812 // It returns the actual number of characters.
2813 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2814 utf32Characters.Resize( characterCount );
2816 // The characters to be added.
2817 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = characterCount;
2819 // Reset the cursor position
2820 mImpl->mEventData->mPrimaryCursorPosition = 0;
2822 // The natural size needs to be re-calculated.
2823 mImpl->mRecalculateNaturalSize = true;
2825 // Apply modifications to the model
2826 mImpl->mOperationsPending = ALL_OPERATIONS;
2828 // Update the rest of the model during size negotiation
2829 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2833 void Controller::ClearFontData()
2835 if( mImpl->mFontDefaults )
2837 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2840 // Set flags to update the model.
2841 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2842 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2843 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
2845 mImpl->mTextUpdateInfo.mClearAll = true;
2846 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
2847 mImpl->mRecalculateNaturalSize = true;
2849 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2854 UPDATE_ACTUAL_SIZE |
2859 void Controller::ClearStyleData()
2861 mImpl->mLogicalModel->mColorRuns.Clear();
2862 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2865 Controller::Controller( ControlInterface& controlInterface )
2868 mImpl = new Controller::Impl( controlInterface );
2873 } // namespace Toolkit