2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/text/text-controller.h>
24 #include <dali/public-api/adaptor-framework/key.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/devel-api/adaptor-framework/clipboard-event-notifier.h>
29 #include <dali-toolkit/internal/text/bidirectional-support.h>
30 #include <dali-toolkit/internal/text/character-set-conversion.h>
31 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
32 #include <dali-toolkit/internal/text/markup-processor.h>
33 #include <dali-toolkit/internal/text/text-controller-impl.h>
38 #if defined(DEBUG_ENABLED)
39 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
42 const float MAX_FLOAT = std::numeric_limits<float>::max();
44 const std::string EMPTY_STRING("");
46 float ConvertToEven( float value )
48 int intValue(static_cast<int>( value ));
49 return static_cast<float>(intValue % 2 == 0) ? intValue : (intValue + 1);
64 * @brief Adds a new font description run for the selected text.
66 * The new font parameters are added after the call to this method.
68 * @param[in] eventData The event data pointer.
69 * @param[in] logicalModel The logical model where to add the new font description run.
70 * @param[out] startOfSelectedText Index to the first selected character.
71 * @param[out] lengthOfSelectedText Number of selected characters.
73 FontDescriptionRun& UpdateSelectionFontStyleRun( EventData* eventData,
74 LogicalModelPtr logicalModel,
75 CharacterIndex& startOfSelectedText,
76 Length& lengthOfSelectedText )
78 const bool handlesCrossed = eventData->mLeftSelectionPosition > eventData->mRightSelectionPosition;
80 // Get start and end position of selection
81 startOfSelectedText = handlesCrossed ? eventData->mRightSelectionPosition : eventData->mLeftSelectionPosition;
82 lengthOfSelectedText = ( handlesCrossed ? eventData->mLeftSelectionPosition : eventData->mRightSelectionPosition ) - startOfSelectedText;
85 const VectorBase::SizeType numberOfRuns = logicalModel->mFontDescriptionRuns.Count();
86 logicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
88 FontDescriptionRun& fontDescriptionRun = *( logicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
90 fontDescriptionRun.characterRun.characterIndex = startOfSelectedText;
91 fontDescriptionRun.characterRun.numberOfCharacters = lengthOfSelectedText;
93 // Recalculate the selection highlight as the metrics may have changed.
94 eventData->mUpdateLeftSelectionPosition = true;
95 eventData->mUpdateRightSelectionPosition = true;
96 eventData->mUpdateHighlightBox = true;
98 return fontDescriptionRun;
101 ControllerPtr Controller::New( ControlInterface& controlInterface )
103 return ControllerPtr( new Controller( controlInterface ) );
106 void Controller::EnableTextInput( DecoratorPtr decorator )
108 if( NULL == mImpl->mEventData )
110 mImpl->mEventData = new EventData( decorator );
114 void Controller::SetGlyphType( TextAbstraction::GlyphType glyphType )
116 // Metrics for bitmap & vector based glyphs are different
117 mImpl->mMetrics->SetGlyphType( glyphType );
119 // Clear the font-specific data
122 mImpl->RequestRelayout();
125 void Controller::SetMarkupProcessorEnabled( bool enable )
127 mImpl->mMarkupProcessorEnabled = enable;
130 bool Controller::IsMarkupProcessorEnabled() const
132 return mImpl->mMarkupProcessorEnabled;
135 void Controller::SetAutoScrollEnabled( bool enable )
137 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled[%s] SingleBox[%s]-> [%p]\n", (enable)?"true":"false", ( mImpl->mLayoutEngine.GetLayout() == LayoutEngine::SINGLE_LINE_BOX)?"true":"false", this );
139 if ( mImpl->mLayoutEngine.GetLayout() == LayoutEngine::SINGLE_LINE_BOX )
143 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled for SINGLE_LINE_BOX\n" );
144 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
154 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled Disabling autoscroll\n");
155 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
162 mImpl->mAutoScrollEnabled = enable;
163 mImpl->RequestRelayout();
167 DALI_LOG_WARNING( "Attempted AutoScrolling on a non SINGLE_LINE_BOX, request ignored\n" );
168 mImpl->mAutoScrollEnabled = false;
172 bool Controller::IsAutoScrollEnabled() const
174 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::IsAutoScrollEnabled[%s]\n", (mImpl->mAutoScrollEnabled)?"true":"false" );
176 return mImpl->mAutoScrollEnabled;
179 CharacterDirection Controller::GetAutoScrollDirection() const
181 return mImpl->mAutoScrollDirectionRTL;
184 float Controller::GetAutoScrollLineAlignment() const
188 if( mImpl->mVisualModel &&
189 ( 0u != mImpl->mVisualModel->mLines.Count() ) )
191 offset = ( *mImpl->mVisualModel->mLines.Begin() ).alignmentOffset;
197 void Controller::SetHorizontalScrollEnabled( bool enable )
199 if( ( NULL != mImpl->mEventData ) &&
200 mImpl->mEventData->mDecorator )
202 mImpl->mEventData->mDecorator->SetHorizontalScrollEnabled( enable );
206 bool Controller::IsHorizontalScrollEnabled() const
208 if( ( NULL != mImpl->mEventData ) &&
209 mImpl->mEventData->mDecorator )
211 return mImpl->mEventData->mDecorator->IsHorizontalScrollEnabled();
217 void Controller::SetVerticalScrollEnabled( bool enable )
219 if( ( NULL != mImpl->mEventData ) &&
220 mImpl->mEventData->mDecorator )
222 if( mImpl->mEventData->mDecorator )
224 mImpl->mEventData->mDecorator->SetVerticalScrollEnabled( enable );
229 bool Controller::IsVerticalScrollEnabled() const
231 if( ( NULL != mImpl->mEventData ) &&
232 mImpl->mEventData->mDecorator )
234 return mImpl->mEventData->mDecorator->IsVerticalScrollEnabled();
240 void Controller::SetSmoothHandlePanEnabled( bool enable )
242 if( ( NULL != mImpl->mEventData ) &&
243 mImpl->mEventData->mDecorator )
245 mImpl->mEventData->mDecorator->SetSmoothHandlePanEnabled( enable );
249 bool Controller::IsSmoothHandlePanEnabled() const
251 if( ( NULL != mImpl->mEventData ) &&
252 mImpl->mEventData->mDecorator )
254 return mImpl->mEventData->mDecorator->IsSmoothHandlePanEnabled();
260 void Controller::SetText( const std::string& text )
262 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
264 // Reset keyboard as text changed
265 mImpl->ResetImfManager();
267 // Remove the previously set text and style.
273 CharacterIndex lastCursorIndex = 0u;
275 if( NULL != mImpl->mEventData )
277 // If popup shown then hide it by switching to Editing state
278 if( ( EventData::SELECTING == mImpl->mEventData->mState ) ||
279 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
280 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
281 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
283 mImpl->ChangeState( EventData::EDITING );
289 mImpl->mVisualModel->SetTextColor( mImpl->mTextColor );
291 MarkupProcessData markupProcessData( mImpl->mLogicalModel->mColorRuns,
292 mImpl->mLogicalModel->mFontDescriptionRuns );
294 Length textSize = 0u;
295 const uint8_t* utf8 = NULL;
296 if( mImpl->mMarkupProcessorEnabled )
298 ProcessMarkupString( text, markupProcessData );
299 textSize = markupProcessData.markupProcessedText.size();
301 // This is a bit horrible but std::string returns a (signed) char*
302 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
306 textSize = text.size();
308 // This is a bit horrible but std::string returns a (signed) char*
309 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
312 // Convert text into UTF-32
313 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
314 utf32Characters.Resize( textSize );
316 // Transform a text array encoded in utf8 into an array encoded in utf32.
317 // It returns the actual number of characters.
318 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
319 utf32Characters.Resize( characterCount );
321 DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
322 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mLogicalModel->mText.Count() );
324 // The characters to be added.
325 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
327 // To reset the cursor position
328 lastCursorIndex = characterCount;
330 // Update the rest of the model during size negotiation
331 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
333 // The natural size needs to be re-calculated.
334 mImpl->mRecalculateNaturalSize = true;
336 // Apply modifications to the model
337 mImpl->mOperationsPending = ALL_OPERATIONS;
341 ShowPlaceholderText();
344 // Resets the cursor position.
345 ResetCursorPosition( lastCursorIndex );
347 // Scrolls the text to make the cursor visible.
348 ResetScrollPosition();
350 mImpl->RequestRelayout();
352 if( NULL != mImpl->mEventData )
354 // Cancel previously queued events
355 mImpl->mEventData->mEventQueue.clear();
358 // Do this last since it provides callbacks into application code
359 mImpl->mControlInterface.TextChanged();
362 void Controller::GetText( std::string& text ) const
364 if( !mImpl->IsShowingPlaceholderText() )
366 // Retrieves the text string.
367 mImpl->GetText( 0u, text );
371 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
375 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
377 if( NULL != mImpl->mEventData )
379 if( PLACEHOLDER_TYPE_INACTIVE == type )
381 mImpl->mEventData->mPlaceholderTextInactive = text;
385 mImpl->mEventData->mPlaceholderTextActive = text;
388 // Update placeholder if there is no text
389 if( mImpl->IsShowingPlaceholderText() ||
390 ( 0u == mImpl->mLogicalModel->mText.Count() ) )
392 ShowPlaceholderText();
397 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
399 if( NULL != mImpl->mEventData )
401 if( PLACEHOLDER_TYPE_INACTIVE == type )
403 text = mImpl->mEventData->mPlaceholderTextInactive;
407 text = mImpl->mEventData->mPlaceholderTextActive;
412 void Controller::SetMaximumNumberOfCharacters( Length maxCharacters )
414 mImpl->mMaximumNumberOfCharacters = maxCharacters;
417 int Controller::GetMaximumNumberOfCharacters()
419 return mImpl->mMaximumNumberOfCharacters;
422 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
424 if( NULL == mImpl->mFontDefaults )
426 mImpl->mFontDefaults = new FontDefaults();
429 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
430 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
431 mImpl->mFontDefaults->familyDefined = !defaultFontFamily.empty();
433 // Clear the font-specific data
436 mImpl->RequestRelayout();
439 const std::string& Controller::GetDefaultFontFamily() const
441 if( NULL != mImpl->mFontDefaults )
443 return mImpl->mFontDefaults->mFontDescription.family;
449 void Controller::SetDefaultFontWeight( FontWeight weight )
451 if( NULL == mImpl->mFontDefaults )
453 mImpl->mFontDefaults = new FontDefaults();
456 mImpl->mFontDefaults->mFontDescription.weight = weight;
457 mImpl->mFontDefaults->weightDefined = true;
459 // Clear the font-specific data
462 mImpl->RequestRelayout();
465 bool Controller::IsDefaultFontWeightDefined() const
467 return mImpl->mFontDefaults->weightDefined;
470 FontWeight Controller::GetDefaultFontWeight() const
472 if( NULL != mImpl->mFontDefaults )
474 return mImpl->mFontDefaults->mFontDescription.weight;
477 return TextAbstraction::FontWeight::NORMAL;
480 void Controller::SetDefaultFontWidth( FontWidth width )
482 if( NULL == mImpl->mFontDefaults )
484 mImpl->mFontDefaults = new FontDefaults();
487 mImpl->mFontDefaults->mFontDescription.width = width;
488 mImpl->mFontDefaults->widthDefined = true;
490 // Clear the font-specific data
493 mImpl->RequestRelayout();
496 bool Controller::IsDefaultFontWidthDefined() const
498 return mImpl->mFontDefaults->widthDefined;
501 FontWidth Controller::GetDefaultFontWidth() const
503 if( NULL != mImpl->mFontDefaults )
505 return mImpl->mFontDefaults->mFontDescription.width;
508 return TextAbstraction::FontWidth::NORMAL;
511 void Controller::SetDefaultFontSlant( FontSlant slant )
513 if( NULL == mImpl->mFontDefaults )
515 mImpl->mFontDefaults = new FontDefaults();
518 mImpl->mFontDefaults->mFontDescription.slant = slant;
519 mImpl->mFontDefaults->slantDefined = true;
521 // Clear the font-specific data
524 mImpl->RequestRelayout();
527 bool Controller::IsDefaultFontSlantDefined() const
529 return mImpl->mFontDefaults->slantDefined;
532 FontSlant Controller::GetDefaultFontSlant() const
534 if( NULL != mImpl->mFontDefaults )
536 return mImpl->mFontDefaults->mFontDescription.slant;
539 return TextAbstraction::FontSlant::NORMAL;
542 void Controller::SetDefaultPointSize( float pointSize )
544 if( NULL == mImpl->mFontDefaults )
546 mImpl->mFontDefaults = new FontDefaults();
549 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
550 mImpl->mFontDefaults->sizeDefined = true;
552 // Clear the font-specific data
555 mImpl->RequestRelayout();
558 float Controller::GetDefaultPointSize() const
560 if( NULL != mImpl->mFontDefaults )
562 return mImpl->mFontDefaults->mDefaultPointSize;
568 void Controller::UpdateAfterFontChange( const std::string& newDefaultFont )
570 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::UpdateAfterFontChange\n");
572 if( !mImpl->mFontDefaults->familyDefined ) // If user defined font then should not update when system font changes
574 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
575 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
579 mImpl->RequestRelayout();
583 void Controller::SetTextColor( const Vector4& textColor )
585 mImpl->mTextColor = textColor;
587 if( !mImpl->IsShowingPlaceholderText() )
589 mImpl->mVisualModel->SetTextColor( textColor );
591 mImpl->RequestRelayout();
595 const Vector4& Controller::GetTextColor() const
597 return mImpl->mTextColor;
600 bool Controller::RemoveText( int cursorOffset,
601 int numberOfCharacters,
602 UpdateInputStyleType type )
604 bool removed = false;
606 if( NULL == mImpl->mEventData )
611 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfCharacters %d\n",
612 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters );
614 if( !mImpl->IsShowingPlaceholderText() )
616 // Delete at current cursor position
617 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
618 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
620 CharacterIndex cursorIndex = oldCursorIndex;
622 // Validate the cursor position & number of characters
623 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
625 cursorIndex = oldCursorIndex + cursorOffset;
628 if( ( cursorIndex + numberOfCharacters ) > currentText.Count() )
630 numberOfCharacters = currentText.Count() - cursorIndex;
633 if( ( cursorIndex + numberOfCharacters ) <= mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters )
635 // Mark the paragraphs to be updated.
636 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
637 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove += numberOfCharacters;
639 // Update the input style and remove the text's style before removing the text.
641 if( UPDATE_INPUT_STYLE == type )
643 // Set first the default input style.
644 mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
646 // Update the input style.
647 mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
650 // Updates the text style runs by removing characters. Runs with no characters are removed.
651 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfCharacters );
653 // Remove the characters.
654 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
655 Vector<Character>::Iterator last = first + numberOfCharacters;
657 currentText.Erase( first, last );
659 // Cursor position retreat
660 oldCursorIndex = cursorIndex;
662 mImpl->mEventData->mScrollAfterDelete = true;
664 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfCharacters );
672 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
674 if( NULL != mImpl->mEventData )
676 mImpl->mEventData->mPlaceholderTextColor = textColor;
679 if( mImpl->IsShowingPlaceholderText() )
681 mImpl->mVisualModel->SetTextColor( textColor );
682 mImpl->RequestRelayout();
686 const Vector4& Controller::GetPlaceholderTextColor() const
688 if( NULL != mImpl->mEventData )
690 return mImpl->mEventData->mPlaceholderTextColor;
696 void Controller::SetShadowOffset( const Vector2& shadowOffset )
698 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
700 mImpl->RequestRelayout();
703 const Vector2& Controller::GetShadowOffset() const
705 return mImpl->mVisualModel->GetShadowOffset();
708 void Controller::SetShadowColor( const Vector4& shadowColor )
710 mImpl->mVisualModel->SetShadowColor( shadowColor );
712 mImpl->RequestRelayout();
715 const Vector4& Controller::GetShadowColor() const
717 return mImpl->mVisualModel->GetShadowColor();
720 void Controller::SetDefaultShadowProperties( const std::string& shadowProperties )
722 if( NULL == mImpl->mShadowDefaults )
724 mImpl->mShadowDefaults = new ShadowDefaults();
727 mImpl->mShadowDefaults->properties = shadowProperties;
730 const std::string& Controller::GetDefaultShadowProperties() const
732 if( NULL != mImpl->mShadowDefaults )
734 return mImpl->mShadowDefaults->properties;
740 void Controller::SetUnderlineColor( const Vector4& color )
742 mImpl->mVisualModel->SetUnderlineColor( color );
744 mImpl->RequestRelayout();
747 const Vector4& Controller::GetUnderlineColor() const
749 return mImpl->mVisualModel->GetUnderlineColor();
752 void Controller::SetUnderlineEnabled( bool enabled )
754 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
756 mImpl->RequestRelayout();
759 bool Controller::IsUnderlineEnabled() const
761 return mImpl->mVisualModel->IsUnderlineEnabled();
764 void Controller::SetUnderlineHeight( float height )
766 mImpl->mVisualModel->SetUnderlineHeight( height );
768 mImpl->RequestRelayout();
771 float Controller::GetUnderlineHeight() const
773 return mImpl->mVisualModel->GetUnderlineHeight();
776 void Controller::SetDefaultUnderlineProperties( const std::string& underlineProperties )
778 if( NULL == mImpl->mUnderlineDefaults )
780 mImpl->mUnderlineDefaults = new UnderlineDefaults();
783 mImpl->mUnderlineDefaults->properties = underlineProperties;
786 const std::string& Controller::GetDefaultUnderlineProperties() const
788 if( NULL != mImpl->mUnderlineDefaults )
790 return mImpl->mUnderlineDefaults->properties;
796 void Controller::SetDefaultEmbossProperties( const std::string& embossProperties )
798 if( NULL == mImpl->mEmbossDefaults )
800 mImpl->mEmbossDefaults = new EmbossDefaults();
803 mImpl->mEmbossDefaults->properties = embossProperties;
806 const std::string& Controller::GetDefaultEmbossProperties() const
808 if( NULL != mImpl->mEmbossDefaults )
810 return mImpl->mEmbossDefaults->properties;
816 void Controller::SetDefaultOutlineProperties( const std::string& outlineProperties )
818 if( NULL == mImpl->mOutlineDefaults )
820 mImpl->mOutlineDefaults = new OutlineDefaults();
823 mImpl->mOutlineDefaults->properties = outlineProperties;
826 const std::string& Controller::GetDefaultOutlineProperties() const
828 if( NULL != mImpl->mOutlineDefaults )
830 return mImpl->mOutlineDefaults->properties;
836 void Controller::SetDefaultLineSpacing( float lineSpacing )
838 //TODO finish implementation
839 mImpl->mLayoutEngine.SetDefaultLineSpacing( lineSpacing );
842 float Controller::GetDefaultLineSpacing() const
844 return mImpl->mLayoutEngine.GetDefaultLineSpacing();
847 void Controller::SetInputColor( const Vector4& color )
849 if( NULL != mImpl->mEventData )
851 mImpl->mEventData->mInputStyle.textColor = color;
852 mImpl->mEventData->mInputStyle.isDefaultColor = false;
854 if( EventData::SELECTING == mImpl->mEventData->mState )
856 const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
858 // Get start and end position of selection
859 const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
860 const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
862 // Add the color run.
863 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
864 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
866 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
867 colorRun.color = color;
868 colorRun.characterRun.characterIndex = startOfSelectedText;
869 colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
871 // Request to relayout.
872 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
873 mImpl->RequestRelayout();
875 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
876 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
877 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
882 const Vector4& Controller::GetInputColor() const
884 if( NULL != mImpl->mEventData )
886 return mImpl->mEventData->mInputStyle.textColor;
889 // Return the default text's color if there is no EventData.
890 return mImpl->mTextColor;
894 void Controller::SetInputFontFamily( const std::string& fontFamily )
896 if( NULL != mImpl->mEventData )
898 mImpl->mEventData->mInputStyle.familyName = fontFamily;
899 mImpl->mEventData->mInputStyle.familyDefined = true;
901 if( EventData::SELECTING == mImpl->mEventData->mState )
903 CharacterIndex startOfSelectedText = 0u;
904 Length lengthOfSelectedText = 0u;
905 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
906 mImpl->mLogicalModel,
908 lengthOfSelectedText );
910 fontDescriptionRun.familyLength = fontFamily.size();
911 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
912 memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
913 fontDescriptionRun.familyDefined = true;
915 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
917 // Request to relayout.
918 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
926 mImpl->mRecalculateNaturalSize = true;
927 mImpl->RequestRelayout();
929 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
930 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
931 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
933 // As the font changes, recalculate the handle positions is needed.
934 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
935 mImpl->mEventData->mUpdateRightSelectionPosition = true;
936 mImpl->mEventData->mUpdateHighlightBox = true;
937 mImpl->mEventData->mScrollAfterUpdatePosition = true;
942 const std::string& Controller::GetInputFontFamily() const
944 if( NULL != mImpl->mEventData )
946 return mImpl->mEventData->mInputStyle.familyName;
949 // Return the default font's family if there is no EventData.
950 return GetDefaultFontFamily();
953 void Controller::SetInputFontWeight( FontWeight weight )
955 if( NULL != mImpl->mEventData )
957 mImpl->mEventData->mInputStyle.weight = weight;
958 mImpl->mEventData->mInputStyle.weightDefined = true;
960 if( EventData::SELECTING == mImpl->mEventData->mState )
962 CharacterIndex startOfSelectedText = 0u;
963 Length lengthOfSelectedText = 0u;
964 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
965 mImpl->mLogicalModel,
967 lengthOfSelectedText );
969 fontDescriptionRun.weight = weight;
970 fontDescriptionRun.weightDefined = true;
972 // Request to relayout.
973 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
981 mImpl->mRecalculateNaturalSize = true;
982 mImpl->RequestRelayout();
984 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
985 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
986 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
988 // As the font might change, recalculate the handle positions is needed.
989 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
990 mImpl->mEventData->mUpdateRightSelectionPosition = true;
991 mImpl->mEventData->mUpdateHighlightBox = true;
992 mImpl->mEventData->mScrollAfterUpdatePosition = true;
997 bool Controller::IsInputFontWeightDefined() const
999 bool defined = false;
1001 if( NULL != mImpl->mEventData )
1003 defined = mImpl->mEventData->mInputStyle.weightDefined;
1009 FontWeight Controller::GetInputFontWeight() const
1011 if( NULL != mImpl->mEventData )
1013 return mImpl->mEventData->mInputStyle.weight;
1016 return GetDefaultFontWeight();
1019 void Controller::SetInputFontWidth( FontWidth width )
1021 if( NULL != mImpl->mEventData )
1023 mImpl->mEventData->mInputStyle.width = width;
1024 mImpl->mEventData->mInputStyle.widthDefined = true;
1026 if( EventData::SELECTING == mImpl->mEventData->mState )
1028 CharacterIndex startOfSelectedText = 0u;
1029 Length lengthOfSelectedText = 0u;
1030 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1031 mImpl->mLogicalModel,
1032 startOfSelectedText,
1033 lengthOfSelectedText );
1035 fontDescriptionRun.width = width;
1036 fontDescriptionRun.widthDefined = true;
1038 // Request to relayout.
1039 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1044 UPDATE_LAYOUT_SIZE |
1047 mImpl->mRecalculateNaturalSize = true;
1048 mImpl->RequestRelayout();
1050 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1051 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1052 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1054 // As the font might change, recalculate the handle positions is needed.
1055 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1056 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1057 mImpl->mEventData->mUpdateHighlightBox = true;
1058 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1063 bool Controller::IsInputFontWidthDefined() const
1065 bool defined = false;
1067 if( NULL != mImpl->mEventData )
1069 defined = mImpl->mEventData->mInputStyle.widthDefined;
1075 FontWidth Controller::GetInputFontWidth() const
1077 if( NULL != mImpl->mEventData )
1079 return mImpl->mEventData->mInputStyle.width;
1082 return GetDefaultFontWidth();
1085 void Controller::SetInputFontSlant( FontSlant slant )
1087 if( NULL != mImpl->mEventData )
1089 mImpl->mEventData->mInputStyle.slant = slant;
1090 mImpl->mEventData->mInputStyle.slantDefined = true;
1092 if( EventData::SELECTING == mImpl->mEventData->mState )
1094 CharacterIndex startOfSelectedText = 0u;
1095 Length lengthOfSelectedText = 0u;
1096 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1097 mImpl->mLogicalModel,
1098 startOfSelectedText,
1099 lengthOfSelectedText );
1101 fontDescriptionRun.slant = slant;
1102 fontDescriptionRun.slantDefined = true;
1104 // Request to relayout.
1105 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1110 UPDATE_LAYOUT_SIZE |
1113 mImpl->mRecalculateNaturalSize = true;
1114 mImpl->RequestRelayout();
1116 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1117 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1118 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1120 // As the font might change, recalculate the handle positions is needed.
1121 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1122 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1123 mImpl->mEventData->mUpdateHighlightBox = true;
1124 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1129 bool Controller::IsInputFontSlantDefined() const
1131 bool defined = false;
1133 if( NULL != mImpl->mEventData )
1135 defined = mImpl->mEventData->mInputStyle.slantDefined;
1141 FontSlant Controller::GetInputFontSlant() const
1143 if( NULL != mImpl->mEventData )
1145 return mImpl->mEventData->mInputStyle.slant;
1148 return GetDefaultFontSlant();
1151 void Controller::SetInputFontPointSize( float size )
1153 if( NULL != mImpl->mEventData )
1155 mImpl->mEventData->mInputStyle.size = size;
1157 if( EventData::SELECTING == mImpl->mEventData->mState )
1159 CharacterIndex startOfSelectedText = 0u;
1160 Length lengthOfSelectedText = 0u;
1161 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1162 mImpl->mLogicalModel,
1163 startOfSelectedText,
1164 lengthOfSelectedText );
1166 fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
1167 fontDescriptionRun.sizeDefined = true;
1169 // Request to relayout.
1170 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1175 UPDATE_LAYOUT_SIZE |
1178 mImpl->mRecalculateNaturalSize = true;
1179 mImpl->RequestRelayout();
1181 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1182 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1183 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1185 // As the font might change, recalculate the handle positions is needed.
1186 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1187 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1188 mImpl->mEventData->mUpdateHighlightBox = true;
1189 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1194 float Controller::GetInputFontPointSize() const
1196 if( NULL != mImpl->mEventData )
1198 return mImpl->mEventData->mInputStyle.size;
1201 // Return the default font's point size if there is no EventData.
1202 return GetDefaultPointSize();
1205 void Controller::SetInputLineSpacing( float lineSpacing )
1207 if( NULL != mImpl->mEventData )
1209 mImpl->mEventData->mInputStyle.lineSpacing = lineSpacing;
1213 float Controller::GetInputLineSpacing() const
1215 if( NULL != mImpl->mEventData )
1217 return mImpl->mEventData->mInputStyle.lineSpacing;
1223 void Controller::SetInputShadowProperties( const std::string& shadowProperties )
1225 if( NULL != mImpl->mEventData )
1227 mImpl->mEventData->mInputStyle.shadowProperties = shadowProperties;
1231 const std::string& Controller::GetInputShadowProperties() const
1233 if( NULL != mImpl->mEventData )
1235 return mImpl->mEventData->mInputStyle.shadowProperties;
1238 return GetDefaultShadowProperties();
1241 void Controller::SetInputUnderlineProperties( const std::string& underlineProperties )
1243 if( NULL != mImpl->mEventData )
1245 mImpl->mEventData->mInputStyle.underlineProperties = underlineProperties;
1249 const std::string& Controller::GetInputUnderlineProperties() const
1251 if( NULL != mImpl->mEventData )
1253 return mImpl->mEventData->mInputStyle.underlineProperties;
1256 return GetDefaultUnderlineProperties();
1259 void Controller::SetInputEmbossProperties( const std::string& embossProperties )
1261 if( NULL != mImpl->mEventData )
1263 mImpl->mEventData->mInputStyle.embossProperties = embossProperties;
1267 const std::string& Controller::GetInputEmbossProperties() const
1269 if( NULL != mImpl->mEventData )
1271 return mImpl->mEventData->mInputStyle.embossProperties;
1274 return GetDefaultEmbossProperties();
1277 void Controller::SetInputOutlineProperties( const std::string& outlineProperties )
1279 if( NULL != mImpl->mEventData )
1281 mImpl->mEventData->mInputStyle.outlineProperties = outlineProperties;
1285 const std::string& Controller::GetInputOutlineProperties() const
1287 if( NULL != mImpl->mEventData )
1289 return mImpl->mEventData->mInputStyle.outlineProperties;
1292 return GetDefaultOutlineProperties();
1295 void Controller::SetEnableCursorBlink( bool enable )
1297 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
1299 if( NULL != mImpl->mEventData )
1301 mImpl->mEventData->mCursorBlinkEnabled = enable;
1304 mImpl->mEventData->mDecorator )
1306 mImpl->mEventData->mDecorator->StopCursorBlink();
1311 bool Controller::GetEnableCursorBlink() const
1313 if( NULL != mImpl->mEventData )
1315 return mImpl->mEventData->mCursorBlinkEnabled;
1321 const Vector2& Controller::GetScrollPosition() const
1323 return mImpl->mScrollPosition;
1326 Vector3 Controller::GetNaturalSize()
1328 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
1329 Vector3 naturalSize;
1331 // Make sure the model is up-to-date before layouting
1332 ProcessModifyEvents();
1334 if( mImpl->mRecalculateNaturalSize )
1336 // Operations that can be done only once until the text changes.
1337 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1344 GET_GLYPH_METRICS );
1345 // Make sure the model is up-to-date before layouting
1346 mImpl->UpdateModel( onlyOnceOperations );
1348 // Layout the text for the new width.
1349 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1351 // Set the update info to relayout the whole text.
1352 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1353 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1355 // Store the actual control's size to restore later.
1356 const Size actualControlSize = mImpl->mVisualModel->mControlSize;
1358 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
1359 static_cast<OperationsMask>( onlyOnceOperations |
1361 naturalSize.GetVectorXY() );
1363 // Do not do again the only once operations.
1364 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1366 // Do the size related operations again.
1367 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1370 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1372 // Stores the natural size to avoid recalculate it again
1373 // unless the text/style changes.
1374 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
1376 mImpl->mRecalculateNaturalSize = false;
1378 // Clear the update info. This info will be set the next time the text is updated.
1379 mImpl->mTextUpdateInfo.Clear();
1381 // Restore the actual control's size.
1382 mImpl->mVisualModel->mControlSize = actualControlSize;
1384 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1388 naturalSize = mImpl->mVisualModel->GetNaturalSize();
1390 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1393 naturalSize.x = ConvertToEven( naturalSize.x );
1394 naturalSize.y = ConvertToEven( naturalSize.y );
1399 float Controller::GetHeightForWidth( float width )
1401 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1402 // Make sure the model is up-to-date before layouting
1403 ProcessModifyEvents();
1406 if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
1408 // Operations that can be done only once until the text changes.
1409 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1416 GET_GLYPH_METRICS );
1417 // Make sure the model is up-to-date before layouting
1418 mImpl->UpdateModel( onlyOnceOperations );
1421 // Layout the text for the new width.
1422 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1424 // Set the update info to relayout the whole text.
1425 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1426 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1428 // Store the actual control's width.
1429 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
1431 DoRelayout( Size( width, MAX_FLOAT ),
1432 static_cast<OperationsMask>( onlyOnceOperations |
1436 // Do not do again the only once operations.
1437 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1439 // Do the size related operations again.
1440 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1444 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1446 // Clear the update info. This info will be set the next time the text is updated.
1447 mImpl->mTextUpdateInfo.Clear();
1449 // Restore the actual control's width.
1450 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1452 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1456 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1457 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1460 return layoutSize.height;
1463 Controller::UpdateTextType Controller::Relayout( const Size& size )
1465 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", this, size.width, size.height, (mImpl->mAutoScrollEnabled)?"true":"false" );
1467 UpdateTextType updateTextType = NONE_UPDATED;
1469 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1471 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1473 mImpl->mVisualModel->mGlyphPositions.Clear();
1474 updateTextType = MODEL_UPDATED;
1477 // Clear the update info. This info will be set the next time the text is updated.
1478 mImpl->mTextUpdateInfo.Clear();
1480 // Not worth to relayout if width or height is equal to zero.
1481 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1483 return updateTextType;
1486 // Whether a new size has been set.
1487 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1491 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1493 // Layout operations that need to be done if the size changes.
1494 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1497 UPDATE_LAYOUT_SIZE |
1499 // Set the update info to relayout the whole text.
1500 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
1501 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1504 // Whether there are modify events.
1505 if( 0u != mImpl->mModifyEvents.Count() )
1507 // Style operations that need to be done if the text is modified.
1508 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1512 // Make sure the model is up-to-date before layouting.
1513 ProcessModifyEvents();
1514 bool updated = mImpl->UpdateModel( mImpl->mOperationsPending );
1518 updated = DoRelayout( size,
1519 mImpl->mOperationsPending,
1520 layoutSize ) || updated;
1524 updateTextType = MODEL_UPDATED;
1527 // Do not re-do any operation until something changes.
1528 mImpl->mOperationsPending = NO_OPERATION;
1530 // Whether the text control is editable
1531 const bool isEditable = NULL != mImpl->mEventData;
1533 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
1535 if( newSize && isEditable )
1537 offset = mImpl->mScrollPosition;
1540 if( !isEditable || !IsMultiLineEnabled() )
1542 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
1543 CalculateVerticalOffset( size );
1550 // If there is a new size, the scroll position needs to be clamped.
1551 mImpl->ClampHorizontalScroll( layoutSize );
1553 // Update the decorator's positions is needed if there is a new size.
1554 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mScrollPosition - offset );
1557 // Move the cursor, grab handle etc.
1558 if( mImpl->ProcessInputEvents() )
1560 updateTextType = static_cast<UpdateTextType>( updateTextType | DECORATOR_UPDATED );
1564 // Clear the update info. This info will be set the next time the text is updated.
1565 mImpl->mTextUpdateInfo.Clear();
1566 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1568 return updateTextType;
1571 void Controller::ProcessModifyEvents()
1573 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
1575 if( 0u == events.Count() )
1581 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
1582 endIt = events.End();
1586 const ModifyEvent& event = *it;
1588 if( ModifyEvent::TEXT_REPLACED == event.type )
1590 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
1591 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
1593 TextReplacedEvent();
1595 else if( ModifyEvent::TEXT_INSERTED == event.type )
1597 TextInsertedEvent();
1599 else if( ModifyEvent::TEXT_DELETED == event.type )
1601 // Placeholder-text cannot be deleted
1602 if( !mImpl->IsShowingPlaceholderText() )
1609 if( NULL != mImpl->mEventData )
1611 // When the text is being modified, delay cursor blinking
1612 mImpl->mEventData->mDecorator->DelayCursorBlink();
1615 // Discard temporary text
1619 void Controller::ResetText()
1622 mImpl->mLogicalModel->mText.Clear();
1624 // We have cleared everything including the placeholder-text
1625 mImpl->PlaceholderCleared();
1627 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1628 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1629 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
1631 // Clear any previous text.
1632 mImpl->mTextUpdateInfo.mClearAll = true;
1634 // The natural size needs to be re-calculated.
1635 mImpl->mRecalculateNaturalSize = true;
1637 // Apply modifications to the model
1638 mImpl->mOperationsPending = ALL_OPERATIONS;
1641 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
1643 // Reset the cursor position
1644 if( NULL != mImpl->mEventData )
1646 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
1648 // Update the cursor if it's in editing mode.
1649 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1651 mImpl->mEventData->mUpdateCursorPosition = true;
1656 void Controller::ResetScrollPosition()
1658 if( NULL != mImpl->mEventData )
1660 // Reset the scroll position.
1661 mImpl->mScrollPosition = Vector2::ZERO;
1662 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1666 void Controller::TextReplacedEvent()
1668 // The natural size needs to be re-calculated.
1669 mImpl->mRecalculateNaturalSize = true;
1671 // Apply modifications to the model
1672 mImpl->mOperationsPending = ALL_OPERATIONS;
1675 void Controller::TextInsertedEvent()
1677 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
1679 if( NULL == mImpl->mEventData )
1684 // The natural size needs to be re-calculated.
1685 mImpl->mRecalculateNaturalSize = true;
1687 // Apply modifications to the model; TODO - Optimize this
1688 mImpl->mOperationsPending = ALL_OPERATIONS;
1691 void Controller::TextDeletedEvent()
1693 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
1695 if( NULL == mImpl->mEventData )
1700 // The natural size needs to be re-calculated.
1701 mImpl->mRecalculateNaturalSize = true;
1703 // Apply modifications to the model; TODO - Optimize this
1704 mImpl->mOperationsPending = ALL_OPERATIONS;
1707 bool Controller::DoRelayout( const Size& size,
1708 OperationsMask operationsRequired,
1711 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
1712 bool viewUpdated( false );
1714 // Calculate the operations to be done.
1715 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
1717 const CharacterIndex startIndex = mImpl->mTextUpdateInfo.mParagraphCharacterIndex;
1718 const Length requestedNumberOfCharacters = mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters;
1720 // Get the current layout size.
1721 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1723 if( NO_OPERATION != ( LAYOUT & operations ) )
1725 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
1727 // Some vectors with data needed to layout and reorder may be void
1728 // after the first time the text has been laid out.
1729 // Fill the vectors again.
1731 // Calculate the number of glyphs to layout.
1732 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1733 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1734 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
1735 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
1737 const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
1738 const GlyphIndex startGlyphIndex = mImpl->mTextUpdateInfo.mStartGlyphIndex;
1739 const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
1740 const Length totalNumberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
1742 if( 0u == totalNumberOfGlyphs )
1744 if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
1746 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
1749 // Nothing else to do if there is no glyphs.
1750 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1754 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1755 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1756 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1757 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1758 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1759 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1760 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1762 // Set the layout parameters.
1763 LayoutParameters layoutParameters( size,
1765 lineBreakInfo.Begin(),
1766 wordBreakInfo.Begin(),
1767 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1769 glyphsToCharactersMap.Begin(),
1770 charactersPerGlyph.Begin(),
1771 charactersToGlyphBuffer,
1772 glyphsPerCharacterBuffer,
1773 totalNumberOfGlyphs );
1775 // Resize the vector of positions to have the same size than the vector of glyphs.
1776 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1777 glyphPositions.Resize( totalNumberOfGlyphs );
1779 // Whether the last character is a new paragraph character.
1780 mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1781 layoutParameters.isLastNewParagraph = mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph;
1783 // The initial glyph and the number of glyphs to layout.
1784 layoutParameters.startGlyphIndex = startGlyphIndex;
1785 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1786 layoutParameters.startLineIndex = mImpl->mTextUpdateInfo.mStartLineIndex;
1787 layoutParameters.estimatedNumberOfLines = mImpl->mTextUpdateInfo.mEstimatedNumberOfLines;
1789 // Update the visual model.
1791 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1793 mImpl->mVisualModel->mLines,
1796 viewUpdated = viewUpdated || ( newLayoutSize != layoutSize );
1800 layoutSize = newLayoutSize;
1802 if ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) )
1804 mImpl->mAutoScrollDirectionRTL = false;
1807 // Reorder the lines
1808 if( NO_OPERATION != ( REORDER & operations ) )
1810 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1811 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1813 // Check first if there are paragraphs with bidirectional info.
1814 if( 0u != bidirectionalInfo.Count() )
1817 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1819 // Reorder the lines.
1820 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1821 ReorderLines( bidirectionalInfo,
1823 requestedNumberOfCharacters,
1824 mImpl->mVisualModel->mLines,
1825 bidirectionalLineInfo );
1827 // Set the bidirectional info per line into the layout parameters.
1828 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1829 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1831 // Re-layout the text. Reorder those lines with right to left characters.
1832 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1834 requestedNumberOfCharacters,
1837 if ( ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) ) && ( numberOfLines > 0 ) )
1839 const LineRun* const firstline = mImpl->mVisualModel->mLines.Begin();
1842 mImpl->mAutoScrollDirectionRTL = firstline->direction;
1848 // Sets the layout size.
1849 if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
1851 mImpl->mVisualModel->SetLayoutSize( layoutSize );
1855 // Store the size used to layout the text.
1856 mImpl->mVisualModel->mControlSize = size;
1859 if( NO_OPERATION != ( ALIGN & operations ) )
1861 // The laid-out lines.
1862 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1864 mImpl->mLayoutEngine.Align( size,
1866 requestedNumberOfCharacters,
1871 #if defined(DEBUG_ENABLED)
1872 std::string currentText;
1873 GetText( currentText );
1874 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mAutoScrollDirectionRTL[%s] [%s]\n", this, (mImpl->mAutoScrollDirectionRTL)?"true":"false", currentText.c_str() );
1876 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1880 void Controller::SetMultiLineEnabled( bool enable )
1882 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1884 if( layout != mImpl->mLayoutEngine.GetLayout() )
1886 // Set the layout type.
1887 mImpl->mLayoutEngine.SetLayout( layout );
1889 // Set the flags to redo the layout operations
1890 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1891 UPDATE_LAYOUT_SIZE |
1895 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1897 mImpl->RequestRelayout();
1901 bool Controller::IsMultiLineEnabled() const
1903 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1906 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1908 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1910 // Set the alignment.
1911 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1913 // Set the flag to redo the alignment operation.
1914 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1916 mImpl->RequestRelayout();
1920 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1922 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1925 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1927 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1929 // Set the alignment.
1930 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1932 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1934 mImpl->RequestRelayout();
1938 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1940 return mImpl->mLayoutEngine.GetVerticalAlignment();
1943 void Controller::CalculateVerticalOffset( const Size& controlSize )
1945 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1947 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1949 // Get the line height of the default font.
1950 layoutSize.height = mImpl->GetDefaultFontLineHeight();
1953 switch( mImpl->mLayoutEngine.GetVerticalAlignment() )
1955 case LayoutEngine::VERTICAL_ALIGN_TOP:
1957 mImpl->mScrollPosition.y = 0.f;
1960 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1962 mImpl->mScrollPosition.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
1965 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1967 mImpl->mScrollPosition.y = controlSize.height - layoutSize.height;
1973 LayoutEngine& Controller::GetLayoutEngine()
1975 return mImpl->mLayoutEngine;
1978 View& Controller::GetView()
1980 return mImpl->mView;
1983 void Controller::KeyboardFocusGainEvent()
1985 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1987 if( NULL != mImpl->mEventData )
1989 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1990 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1992 mImpl->ChangeState( EventData::EDITING );
1993 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1995 mImpl->NotifyImfMultiLineStatus();
1996 if( mImpl->IsShowingPlaceholderText() )
1998 // Show alternative placeholder-text when editing
1999 ShowPlaceholderText();
2002 mImpl->RequestRelayout();
2006 void Controller::KeyboardFocusLostEvent()
2008 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
2010 if( NULL != mImpl->mEventData )
2012 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
2014 mImpl->ChangeState( EventData::INACTIVE );
2016 if( !mImpl->IsShowingRealText() )
2018 // Revert to regular placeholder-text when not editing
2019 ShowPlaceholderText();
2023 mImpl->RequestRelayout();
2026 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
2028 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
2030 bool textChanged( false );
2032 if( ( NULL != mImpl->mEventData ) &&
2033 ( keyEvent.state == KeyEvent::Down ) )
2035 int keyCode = keyEvent.keyCode;
2036 const std::string& keyString = keyEvent.keyPressed;
2038 // Pre-process to separate modifying events from non-modifying input events.
2039 if( Dali::DALI_KEY_ESCAPE == keyCode )
2041 // Escape key is a special case which causes focus loss
2042 KeyboardFocusLostEvent();
2044 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
2045 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
2046 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
2047 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
2049 Event event( Event::CURSOR_KEY_EVENT );
2050 event.p1.mInt = keyCode;
2051 mImpl->mEventData->mEventQueue.push_back( event );
2053 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
2055 textChanged = BackspaceKeyEvent();
2057 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
2059 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
2060 // Avoids calling the InsertText() method which can delete selected text
2062 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
2063 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
2065 mImpl->ChangeState( EventData::INACTIVE );
2066 // Menu/Home key behaviour does not allow edit mode to resume like Power key
2067 // Avoids calling the InsertText() method which can delete selected text
2069 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
2071 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
2072 // and a character is typed after the type of a upper case latin character.
2078 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
2080 // IMF manager is no longer handling key-events
2081 mImpl->ClearPreEditFlag();
2083 InsertText( keyString, COMMIT );
2087 if ( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
2088 ( mImpl->mEventData->mState != EventData::INACTIVE ) &&
2089 ( Dali::DALI_KEY_SHIFT_LEFT != keyCode ) )
2091 // Should not change the state if the key is the shift send by the imf manager.
2092 // Otherwise, when the state is SELECTING the text controller can't send the right
2093 // surrounding info to the imf.
2094 mImpl->ChangeState( EventData::EDITING );
2097 mImpl->RequestRelayout();
2102 // Do this last since it provides callbacks into application code
2103 mImpl->mControlInterface.TextChanged();
2109 void Controller::InsertText( const std::string& text, Controller::InsertType type )
2111 bool removedPrevious( false );
2112 bool maxLengthReached( false );
2114 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
2116 if( NULL == mImpl->mEventData )
2121 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
2122 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
2123 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2125 // TODO: At the moment the underline runs are only for pre-edit.
2126 mImpl->mVisualModel->mUnderlineRuns.Clear();
2128 // Keep the current number of characters.
2129 const Length currentNumberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2131 // Remove the previous IMF pre-edit.
2132 if( mImpl->mEventData->mPreEditFlag && ( 0u != mImpl->mEventData->mPreEditLength ) )
2134 removedPrevious = RemoveText( -static_cast<int>( mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition ),
2135 mImpl->mEventData->mPreEditLength,
2136 DONT_UPDATE_INPUT_STYLE );
2138 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
2139 mImpl->mEventData->mPreEditLength = 0u;
2143 // Remove the previous Selection.
2144 removedPrevious = RemoveSelectedText();
2147 Vector<Character> utf32Characters;
2148 Length characterCount = 0u;
2152 // Convert text into UTF-32
2153 utf32Characters.Resize( text.size() );
2155 // This is a bit horrible but std::string returns a (signed) char*
2156 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
2158 // Transform a text array encoded in utf8 into an array encoded in utf32.
2159 // It returns the actual number of characters.
2160 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
2161 utf32Characters.Resize( characterCount );
2163 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
2164 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
2167 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
2169 // The placeholder text is no longer needed
2170 if( mImpl->IsShowingPlaceholderText() )
2175 mImpl->ChangeState( EventData::EDITING );
2177 // Handle the IMF (predicitive text) state changes
2178 if( COMMIT == type )
2180 // IMF manager is no longer handling key-events
2181 mImpl->ClearPreEditFlag();
2185 if( !mImpl->mEventData->mPreEditFlag )
2187 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state\n" );
2189 // Record the start of the pre-edit text
2190 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
2193 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
2194 mImpl->mEventData->mPreEditFlag = true;
2196 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2199 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
2201 // Restrict new text to fit within Maximum characters setting.
2202 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
2203 maxLengthReached = ( characterCount > maxSizeOfNewText );
2205 // The cursor position.
2206 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
2208 // Update the text's style.
2210 // Updates the text style runs by adding characters.
2211 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
2213 // Get the character index from the cursor index.
2214 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
2216 // Retrieve the text's style for the given index.
2218 mImpl->RetrieveDefaultInputStyle( style );
2219 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
2221 // Whether to add a new text color run.
2222 const bool addColorRun = ( style.textColor != mImpl->mEventData->mInputStyle.textColor );
2224 // Whether to add a new font run.
2225 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
2226 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
2227 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
2228 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
2229 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
2234 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
2235 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
2237 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
2238 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
2239 colorRun.characterRun.characterIndex = cursorIndex;
2240 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2243 if( addFontNameRun ||
2249 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
2250 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
2252 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
2254 if( addFontNameRun )
2256 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
2257 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
2258 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
2259 fontDescriptionRun.familyDefined = true;
2261 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
2264 if( addFontWeightRun )
2266 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
2267 fontDescriptionRun.weightDefined = true;
2270 if( addFontWidthRun )
2272 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
2273 fontDescriptionRun.widthDefined = true;
2276 if( addFontSlantRun )
2278 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
2279 fontDescriptionRun.slantDefined = true;
2282 if( addFontSizeRun )
2284 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
2285 fontDescriptionRun.sizeDefined = true;
2288 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
2289 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2292 // Insert at current cursor position.
2293 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
2295 if( cursorIndex < numberOfCharactersInModel )
2297 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2301 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2304 // Mark the first paragraph to be updated.
2305 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
2306 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
2308 // Update the cursor index.
2309 cursorIndex += maxSizeOfNewText;
2311 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
2314 const Length numberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2316 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
2317 mImpl->IsPlaceholderAvailable() )
2319 // Show place-holder if empty after removing the pre-edit text
2320 ShowPlaceholderText();
2321 mImpl->mEventData->mUpdateCursorPosition = true;
2322 mImpl->ClearPreEditFlag();
2324 else if( removedPrevious ||
2325 ( 0 != utf32Characters.Count() ) )
2327 // Queue an inserted event
2328 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
2330 mImpl->mEventData->mUpdateCursorPosition = true;
2331 if( numberOfCharacters < currentNumberOfCharacters )
2333 mImpl->mEventData->mScrollAfterDelete = true;
2337 mImpl->mEventData->mScrollAfterUpdatePosition = true;
2341 if( maxLengthReached )
2343 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
2345 mImpl->ResetImfManager();
2347 // Do this last since it provides callbacks into application code
2348 mImpl->mControlInterface.MaxLengthReached();
2352 bool Controller::RemoveSelectedText()
2354 bool textRemoved( false );
2356 if( EventData::SELECTING == mImpl->mEventData->mState )
2358 std::string removedString;
2359 mImpl->RetrieveSelection( removedString, true );
2361 if( !removedString.empty() )
2364 mImpl->ChangeState( EventData::EDITING );
2371 void Controller::TapEvent( unsigned int tapCount, float x, float y )
2373 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
2375 if( NULL != mImpl->mEventData )
2377 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
2379 if( 1u == tapCount )
2381 // This is to avoid unnecessary relayouts when tapping an empty text-field
2382 bool relayoutNeeded( false );
2384 if( ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
2385 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
2387 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE ); // If Popup shown hide it here so can be shown again if required.
2390 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
2392 // Already in an active state so show a popup
2393 if( !mImpl->IsClipboardEmpty() )
2395 // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
2396 mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
2400 // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2401 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2403 relayoutNeeded = true;
2407 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2409 // Hide placeholder text
2413 if( EventData::INACTIVE == mImpl->mEventData->mState )
2415 mImpl->ChangeState( EventData::EDITING );
2417 else if( !mImpl->IsClipboardEmpty() )
2419 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2421 relayoutNeeded = true;
2424 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2425 if( relayoutNeeded )
2427 Event event( Event::TAP_EVENT );
2428 event.p1.mUint = tapCount;
2429 event.p2.mFloat = x;
2430 event.p3.mFloat = y;
2431 mImpl->mEventData->mEventQueue.push_back( event );
2433 mImpl->RequestRelayout();
2436 else if( 2u == tapCount )
2438 if( mImpl->mEventData->mSelectionEnabled &&
2439 mImpl->IsShowingRealText() )
2441 SelectEvent( x, y, false );
2446 // Reset keyboard as tap event has occurred.
2447 mImpl->ResetImfManager();
2450 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2452 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2454 if( NULL != mImpl->mEventData )
2456 Event event( Event::PAN_EVENT );
2457 event.p1.mInt = state;
2458 event.p2.mFloat = displacement.x;
2459 event.p3.mFloat = displacement.y;
2460 mImpl->mEventData->mEventQueue.push_back( event );
2462 mImpl->RequestRelayout();
2466 void Controller::LongPressEvent( Gesture::State state, float x, float y )
2468 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2470 if( ( state == Gesture::Started ) &&
2471 ( NULL != mImpl->mEventData ) )
2473 if( !mImpl->IsShowingRealText() )
2475 Event event( Event::LONG_PRESS_EVENT );
2476 event.p1.mInt = state;
2477 mImpl->mEventData->mEventQueue.push_back( event );
2478 mImpl->RequestRelayout();
2482 // The 1st long-press on inactive text-field is treated as tap
2483 if( EventData::INACTIVE == mImpl->mEventData->mState )
2485 mImpl->ChangeState( EventData::EDITING );
2487 Event event( Event::TAP_EVENT );
2489 event.p2.mFloat = x;
2490 event.p3.mFloat = y;
2491 mImpl->mEventData->mEventQueue.push_back( event );
2493 mImpl->RequestRelayout();
2497 // Reset the imf manger to commit the pre-edit before selecting the text.
2498 mImpl->ResetImfManager();
2500 SelectEvent( x, y, false );
2506 void Controller::SelectEvent( float x, float y, bool selectAll )
2508 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2510 if( NULL != mImpl->mEventData )
2514 Event event( Event::SELECT_ALL );
2515 mImpl->mEventData->mEventQueue.push_back( event );
2519 Event event( Event::SELECT );
2520 event.p2.mFloat = x;
2521 event.p3.mFloat = y;
2522 mImpl->mEventData->mEventQueue.push_back( event );
2525 mImpl->RequestRelayout();
2529 void Controller::GetTargetSize( Vector2& targetSize )
2531 targetSize = mImpl->mVisualModel->mControlSize;
2534 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2536 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2539 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2541 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2543 if( NULL != mImpl->mEventData )
2545 switch( handleType )
2549 Event event( Event::GRAB_HANDLE_EVENT );
2550 event.p1.mUint = state;
2551 event.p2.mFloat = x;
2552 event.p3.mFloat = y;
2554 mImpl->mEventData->mEventQueue.push_back( event );
2557 case LEFT_SELECTION_HANDLE:
2559 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2560 event.p1.mUint = state;
2561 event.p2.mFloat = x;
2562 event.p3.mFloat = y;
2564 mImpl->mEventData->mEventQueue.push_back( event );
2567 case RIGHT_SELECTION_HANDLE:
2569 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2570 event.p1.mUint = state;
2571 event.p2.mFloat = x;
2572 event.p3.mFloat = y;
2574 mImpl->mEventData->mEventQueue.push_back( event );
2577 case LEFT_SELECTION_HANDLE_MARKER:
2578 case RIGHT_SELECTION_HANDLE_MARKER:
2580 // Markers do not move the handles.
2583 case HANDLE_TYPE_COUNT:
2585 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2589 mImpl->RequestRelayout();
2593 void Controller::PasteText( const std::string& stringToPaste )
2595 InsertText( stringToPaste, Text::Controller::COMMIT );
2596 mImpl->ChangeState( EventData::EDITING );
2597 mImpl->RequestRelayout();
2599 // Do this last since it provides callbacks into application code
2600 mImpl->mControlInterface.TextChanged();
2603 void Controller::PasteClipboardItemEvent()
2605 // Retrieve the clipboard contents first
2606 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2607 std::string stringToPaste( notifier.GetContent() );
2609 // Commit the current pre-edit text; the contents of the clipboard should be appended
2610 mImpl->ResetImfManager();
2612 // Temporary disable hiding clipboard
2613 mImpl->SetClipboardHideEnable( false );
2616 PasteText( stringToPaste );
2618 mImpl->SetClipboardHideEnable( true );
2621 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2623 if( NULL == mImpl->mEventData )
2630 case Toolkit::TextSelectionPopup::CUT:
2632 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2633 mImpl->mOperationsPending = ALL_OPERATIONS;
2635 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2636 !mImpl->IsPlaceholderAvailable() )
2638 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2642 ShowPlaceholderText();
2645 mImpl->mEventData->mUpdateCursorPosition = true;
2646 mImpl->mEventData->mScrollAfterDelete = true;
2648 mImpl->RequestRelayout();
2649 mImpl->mControlInterface.TextChanged();
2652 case Toolkit::TextSelectionPopup::COPY:
2654 mImpl->SendSelectionToClipboard( false ); // Text not modified
2656 mImpl->mEventData->mUpdateCursorPosition = true;
2658 mImpl->RequestRelayout(); // Cursor, Handles, Selection Highlight, Popup
2661 case Toolkit::TextSelectionPopup::PASTE:
2663 std::string stringToPaste("");
2664 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2665 PasteText( stringToPaste );
2668 case Toolkit::TextSelectionPopup::SELECT:
2670 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2672 if( mImpl->mEventData->mSelectionEnabled )
2674 // Creates a SELECT event.
2675 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2679 case Toolkit::TextSelectionPopup::SELECT_ALL:
2681 // Creates a SELECT_ALL event
2682 SelectEvent( 0.f, 0.f, true );
2685 case Toolkit::TextSelectionPopup::CLIPBOARD:
2687 mImpl->ShowClipboard();
2690 case Toolkit::TextSelectionPopup::NONE:
2698 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2700 // Whether the text needs to be relaid-out.
2701 bool requestRelayout = false;
2703 // Whether to retrieve the text and cursor position to be sent to the IMF manager.
2704 bool retrieveText = false;
2705 bool retrieveCursor = false;
2707 switch( imfEvent.eventName )
2709 case ImfManager::COMMIT:
2711 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2712 requestRelayout = true;
2713 retrieveCursor = true;
2716 case ImfManager::PREEDIT:
2718 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2719 requestRelayout = true;
2720 retrieveCursor = true;
2723 case ImfManager::DELETESURROUNDING:
2725 const bool textDeleted = RemoveText( imfEvent.cursorOffset,
2726 imfEvent.numberOfChars,
2727 DONT_UPDATE_INPUT_STYLE );
2731 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2732 !mImpl->IsPlaceholderAvailable() )
2734 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2738 ShowPlaceholderText();
2740 mImpl->mEventData->mUpdateCursorPosition = true;
2741 mImpl->mEventData->mScrollAfterDelete = true;
2743 requestRelayout = true;
2747 case ImfManager::GETSURROUNDING:
2749 retrieveText = true;
2750 retrieveCursor = true;
2753 case ImfManager::VOID:
2760 if( requestRelayout )
2762 mImpl->mOperationsPending = ALL_OPERATIONS;
2763 mImpl->RequestRelayout();
2765 // Do this last since it provides callbacks into application code
2766 mImpl->mControlInterface.TextChanged();
2770 CharacterIndex cursorPosition = 0u;
2771 Length numberOfWhiteSpaces = 0u;
2773 if( retrieveCursor )
2775 numberOfWhiteSpaces = mImpl->GetNumberOfWhiteSpaces( 0u );
2777 cursorPosition = mImpl->GetLogicalCursorPosition();
2779 if( cursorPosition < numberOfWhiteSpaces )
2781 cursorPosition = 0u;
2785 cursorPosition -= numberOfWhiteSpaces;
2791 mImpl->GetText( numberOfWhiteSpaces, text );
2794 ImfManager::ImfCallbackData callbackData( ( retrieveText || retrieveCursor ), cursorPosition, text, false );
2796 return callbackData;
2799 Controller::~Controller()
2804 bool Controller::BackspaceKeyEvent()
2806 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2808 bool removed = false;
2810 if( NULL == mImpl->mEventData )
2815 // IMF manager is no longer handling key-events
2816 mImpl->ClearPreEditFlag();
2818 if( EventData::SELECTING == mImpl->mEventData->mState )
2820 removed = RemoveSelectedText();
2822 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2824 // Remove the character before the current cursor position
2825 removed = RemoveText( -1,
2827 UPDATE_INPUT_STYLE );
2832 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2833 !mImpl->IsPlaceholderAvailable() )
2835 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2839 ShowPlaceholderText();
2841 mImpl->mEventData->mUpdateCursorPosition = true;
2842 mImpl->mEventData->mScrollAfterDelete = true;
2848 void Controller::ShowPlaceholderText()
2850 if( mImpl->IsPlaceholderAvailable() )
2852 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2854 if( NULL == mImpl->mEventData )
2859 mImpl->mEventData->mIsShowingPlaceholderText = true;
2861 // Disable handles when showing place-holder text
2862 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2863 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2864 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2866 const char* text( NULL );
2869 // TODO - Switch placeholder text styles when changing state
2870 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2871 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2873 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2874 size = mImpl->mEventData->mPlaceholderTextActive.size();
2878 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2879 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2882 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2883 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2885 // Reset model for showing placeholder.
2886 mImpl->mLogicalModel->mText.Clear();
2887 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2889 // Convert text into UTF-32
2890 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2891 utf32Characters.Resize( size );
2893 // This is a bit horrible but std::string returns a (signed) char*
2894 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2896 // Transform a text array encoded in utf8 into an array encoded in utf32.
2897 // It returns the actual number of characters.
2898 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2899 utf32Characters.Resize( characterCount );
2901 // The characters to be added.
2902 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = characterCount;
2904 // Reset the cursor position
2905 mImpl->mEventData->mPrimaryCursorPosition = 0;
2907 // The natural size needs to be re-calculated.
2908 mImpl->mRecalculateNaturalSize = true;
2910 // Apply modifications to the model
2911 mImpl->mOperationsPending = ALL_OPERATIONS;
2913 // Update the rest of the model during size negotiation
2914 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2918 void Controller::ClearFontData()
2920 if( mImpl->mFontDefaults )
2922 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2925 // Set flags to update the model.
2926 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2927 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2928 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
2930 mImpl->mTextUpdateInfo.mClearAll = true;
2931 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
2932 mImpl->mRecalculateNaturalSize = true;
2934 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2939 UPDATE_LAYOUT_SIZE |
2944 void Controller::ClearStyleData()
2946 mImpl->mLogicalModel->mColorRuns.Clear();
2947 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2950 Controller::Controller( ControlInterface& controlInterface )
2953 mImpl = new Controller::Impl( controlInterface );
2958 } // namespace Toolkit