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 // Keep a copy of the current input style.
644 InputStyle currentInputStyle;
645 currentInputStyle.Copy( mImpl->mEventData->mInputStyle );
647 // Set first the default input style.
648 mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
650 // Update the input style.
651 mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
653 // Compare if the input style has changed.
654 const bool hasInputStyleChanged = !currentInputStyle.Equal( mImpl->mEventData->mInputStyle );
656 if( hasInputStyleChanged )
658 const InputStyle::Mask styleChangedMask = currentInputStyle.GetInputStyleChangeMask( mImpl->mEventData->mInputStyle );
659 // Queue the input style changed signal.
660 mImpl->mEventData->mInputStyleChangedQueue.PushBack( styleChangedMask );
664 // Updates the text style runs by removing characters. Runs with no characters are removed.
665 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfCharacters );
667 // Remove the characters.
668 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
669 Vector<Character>::Iterator last = first + numberOfCharacters;
671 currentText.Erase( first, last );
673 // Cursor position retreat
674 oldCursorIndex = cursorIndex;
676 mImpl->mEventData->mScrollAfterDelete = true;
678 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfCharacters );
686 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
688 if( NULL != mImpl->mEventData )
690 mImpl->mEventData->mPlaceholderTextColor = textColor;
693 if( mImpl->IsShowingPlaceholderText() )
695 mImpl->mVisualModel->SetTextColor( textColor );
696 mImpl->RequestRelayout();
700 const Vector4& Controller::GetPlaceholderTextColor() const
702 if( NULL != mImpl->mEventData )
704 return mImpl->mEventData->mPlaceholderTextColor;
710 void Controller::SetShadowOffset( const Vector2& shadowOffset )
712 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
714 mImpl->RequestRelayout();
717 const Vector2& Controller::GetShadowOffset() const
719 return mImpl->mVisualModel->GetShadowOffset();
722 void Controller::SetShadowColor( const Vector4& shadowColor )
724 mImpl->mVisualModel->SetShadowColor( shadowColor );
726 mImpl->RequestRelayout();
729 const Vector4& Controller::GetShadowColor() const
731 return mImpl->mVisualModel->GetShadowColor();
734 void Controller::SetDefaultShadowProperties( const std::string& shadowProperties )
736 if( NULL == mImpl->mShadowDefaults )
738 mImpl->mShadowDefaults = new ShadowDefaults();
741 mImpl->mShadowDefaults->properties = shadowProperties;
744 const std::string& Controller::GetDefaultShadowProperties() const
746 if( NULL != mImpl->mShadowDefaults )
748 return mImpl->mShadowDefaults->properties;
754 void Controller::SetUnderlineColor( const Vector4& color )
756 mImpl->mVisualModel->SetUnderlineColor( color );
758 mImpl->RequestRelayout();
761 const Vector4& Controller::GetUnderlineColor() const
763 return mImpl->mVisualModel->GetUnderlineColor();
766 void Controller::SetUnderlineEnabled( bool enabled )
768 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
770 mImpl->RequestRelayout();
773 bool Controller::IsUnderlineEnabled() const
775 return mImpl->mVisualModel->IsUnderlineEnabled();
778 void Controller::SetUnderlineHeight( float height )
780 mImpl->mVisualModel->SetUnderlineHeight( height );
782 mImpl->RequestRelayout();
785 float Controller::GetUnderlineHeight() const
787 return mImpl->mVisualModel->GetUnderlineHeight();
790 void Controller::SetDefaultUnderlineProperties( const std::string& underlineProperties )
792 if( NULL == mImpl->mUnderlineDefaults )
794 mImpl->mUnderlineDefaults = new UnderlineDefaults();
797 mImpl->mUnderlineDefaults->properties = underlineProperties;
800 const std::string& Controller::GetDefaultUnderlineProperties() const
802 if( NULL != mImpl->mUnderlineDefaults )
804 return mImpl->mUnderlineDefaults->properties;
810 void Controller::SetDefaultEmbossProperties( const std::string& embossProperties )
812 if( NULL == mImpl->mEmbossDefaults )
814 mImpl->mEmbossDefaults = new EmbossDefaults();
817 mImpl->mEmbossDefaults->properties = embossProperties;
820 const std::string& Controller::GetDefaultEmbossProperties() const
822 if( NULL != mImpl->mEmbossDefaults )
824 return mImpl->mEmbossDefaults->properties;
830 void Controller::SetDefaultOutlineProperties( const std::string& outlineProperties )
832 if( NULL == mImpl->mOutlineDefaults )
834 mImpl->mOutlineDefaults = new OutlineDefaults();
837 mImpl->mOutlineDefaults->properties = outlineProperties;
840 const std::string& Controller::GetDefaultOutlineProperties() const
842 if( NULL != mImpl->mOutlineDefaults )
844 return mImpl->mOutlineDefaults->properties;
850 void Controller::SetDefaultLineSpacing( float lineSpacing )
852 //TODO finish implementation
853 mImpl->mLayoutEngine.SetDefaultLineSpacing( lineSpacing );
856 float Controller::GetDefaultLineSpacing() const
858 return mImpl->mLayoutEngine.GetDefaultLineSpacing();
861 void Controller::SetInputColor( const Vector4& color )
863 if( NULL != mImpl->mEventData )
865 mImpl->mEventData->mInputStyle.textColor = color;
866 mImpl->mEventData->mInputStyle.isDefaultColor = false;
868 if( EventData::SELECTING == mImpl->mEventData->mState )
870 const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
872 // Get start and end position of selection
873 const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
874 const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
876 // Add the color run.
877 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
878 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
880 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
881 colorRun.color = color;
882 colorRun.characterRun.characterIndex = startOfSelectedText;
883 colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
885 // Request to relayout.
886 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
887 mImpl->RequestRelayout();
889 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
890 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
891 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
896 const Vector4& Controller::GetInputColor() const
898 if( NULL != mImpl->mEventData )
900 return mImpl->mEventData->mInputStyle.textColor;
903 // Return the default text's color if there is no EventData.
904 return mImpl->mTextColor;
908 void Controller::SetInputFontFamily( const std::string& fontFamily )
910 if( NULL != mImpl->mEventData )
912 mImpl->mEventData->mInputStyle.familyName = fontFamily;
913 mImpl->mEventData->mInputStyle.isFamilyDefined = true;
915 if( EventData::SELECTING == mImpl->mEventData->mState )
917 CharacterIndex startOfSelectedText = 0u;
918 Length lengthOfSelectedText = 0u;
919 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
920 mImpl->mLogicalModel,
922 lengthOfSelectedText );
924 fontDescriptionRun.familyLength = fontFamily.size();
925 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
926 memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
927 fontDescriptionRun.familyDefined = true;
929 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
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 changes, recalculate the handle positions is needed.
948 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
949 mImpl->mEventData->mUpdateRightSelectionPosition = true;
950 mImpl->mEventData->mUpdateHighlightBox = true;
951 mImpl->mEventData->mScrollAfterUpdatePosition = true;
956 const std::string& Controller::GetInputFontFamily() const
958 if( NULL != mImpl->mEventData )
960 return mImpl->mEventData->mInputStyle.familyName;
963 // Return the default font's family if there is no EventData.
964 return GetDefaultFontFamily();
967 void Controller::SetInputFontWeight( FontWeight weight )
969 if( NULL != mImpl->mEventData )
971 mImpl->mEventData->mInputStyle.weight = weight;
972 mImpl->mEventData->mInputStyle.isWeightDefined = true;
974 if( EventData::SELECTING == mImpl->mEventData->mState )
976 CharacterIndex startOfSelectedText = 0u;
977 Length lengthOfSelectedText = 0u;
978 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
979 mImpl->mLogicalModel,
981 lengthOfSelectedText );
983 fontDescriptionRun.weight = weight;
984 fontDescriptionRun.weightDefined = true;
986 // Request to relayout.
987 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
995 mImpl->mRecalculateNaturalSize = true;
996 mImpl->RequestRelayout();
998 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
999 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1000 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1002 // As the font might change, recalculate the handle positions is needed.
1003 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1004 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1005 mImpl->mEventData->mUpdateHighlightBox = true;
1006 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1011 bool Controller::IsInputFontWeightDefined() const
1013 bool defined = false;
1015 if( NULL != mImpl->mEventData )
1017 defined = mImpl->mEventData->mInputStyle.isWeightDefined;
1023 FontWeight Controller::GetInputFontWeight() const
1025 if( NULL != mImpl->mEventData )
1027 return mImpl->mEventData->mInputStyle.weight;
1030 return GetDefaultFontWeight();
1033 void Controller::SetInputFontWidth( FontWidth width )
1035 if( NULL != mImpl->mEventData )
1037 mImpl->mEventData->mInputStyle.width = width;
1038 mImpl->mEventData->mInputStyle.isWidthDefined = true;
1040 if( EventData::SELECTING == mImpl->mEventData->mState )
1042 CharacterIndex startOfSelectedText = 0u;
1043 Length lengthOfSelectedText = 0u;
1044 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1045 mImpl->mLogicalModel,
1046 startOfSelectedText,
1047 lengthOfSelectedText );
1049 fontDescriptionRun.width = width;
1050 fontDescriptionRun.widthDefined = true;
1052 // Request to relayout.
1053 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1058 UPDATE_LAYOUT_SIZE |
1061 mImpl->mRecalculateNaturalSize = true;
1062 mImpl->RequestRelayout();
1064 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1065 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1066 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1068 // As the font might change, recalculate the handle positions is needed.
1069 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1070 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1071 mImpl->mEventData->mUpdateHighlightBox = true;
1072 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1077 bool Controller::IsInputFontWidthDefined() const
1079 bool defined = false;
1081 if( NULL != mImpl->mEventData )
1083 defined = mImpl->mEventData->mInputStyle.isWidthDefined;
1089 FontWidth Controller::GetInputFontWidth() const
1091 if( NULL != mImpl->mEventData )
1093 return mImpl->mEventData->mInputStyle.width;
1096 return GetDefaultFontWidth();
1099 void Controller::SetInputFontSlant( FontSlant slant )
1101 if( NULL != mImpl->mEventData )
1103 mImpl->mEventData->mInputStyle.slant = slant;
1104 mImpl->mEventData->mInputStyle.isSlantDefined = true;
1106 if( EventData::SELECTING == mImpl->mEventData->mState )
1108 CharacterIndex startOfSelectedText = 0u;
1109 Length lengthOfSelectedText = 0u;
1110 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1111 mImpl->mLogicalModel,
1112 startOfSelectedText,
1113 lengthOfSelectedText );
1115 fontDescriptionRun.slant = slant;
1116 fontDescriptionRun.slantDefined = true;
1118 // Request to relayout.
1119 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1124 UPDATE_LAYOUT_SIZE |
1127 mImpl->mRecalculateNaturalSize = true;
1128 mImpl->RequestRelayout();
1130 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1131 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1132 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1134 // As the font might change, recalculate the handle positions is needed.
1135 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1136 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1137 mImpl->mEventData->mUpdateHighlightBox = true;
1138 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1143 bool Controller::IsInputFontSlantDefined() const
1145 bool defined = false;
1147 if( NULL != mImpl->mEventData )
1149 defined = mImpl->mEventData->mInputStyle.isSlantDefined;
1155 FontSlant Controller::GetInputFontSlant() const
1157 if( NULL != mImpl->mEventData )
1159 return mImpl->mEventData->mInputStyle.slant;
1162 return GetDefaultFontSlant();
1165 void Controller::SetInputFontPointSize( float size )
1167 if( NULL != mImpl->mEventData )
1169 mImpl->mEventData->mInputStyle.size = size;
1170 mImpl->mEventData->mInputStyle.isSizeDefined = true;
1172 if( EventData::SELECTING == mImpl->mEventData->mState )
1174 CharacterIndex startOfSelectedText = 0u;
1175 Length lengthOfSelectedText = 0u;
1176 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1177 mImpl->mLogicalModel,
1178 startOfSelectedText,
1179 lengthOfSelectedText );
1181 fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
1182 fontDescriptionRun.sizeDefined = true;
1184 // Request to relayout.
1185 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1190 UPDATE_LAYOUT_SIZE |
1193 mImpl->mRecalculateNaturalSize = true;
1194 mImpl->RequestRelayout();
1196 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1197 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1198 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1200 // As the font might change, recalculate the handle positions is needed.
1201 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1202 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1203 mImpl->mEventData->mUpdateHighlightBox = true;
1204 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1209 float Controller::GetInputFontPointSize() const
1211 if( NULL != mImpl->mEventData )
1213 return mImpl->mEventData->mInputStyle.size;
1216 // Return the default font's point size if there is no EventData.
1217 return GetDefaultPointSize();
1220 void Controller::SetInputLineSpacing( float lineSpacing )
1222 if( NULL != mImpl->mEventData )
1224 mImpl->mEventData->mInputStyle.lineSpacing = lineSpacing;
1225 mImpl->mEventData->mInputStyle.isLineSpacingDefined = true;
1229 float Controller::GetInputLineSpacing() const
1231 if( NULL != mImpl->mEventData )
1233 return mImpl->mEventData->mInputStyle.lineSpacing;
1239 void Controller::SetInputShadowProperties( const std::string& shadowProperties )
1241 if( NULL != mImpl->mEventData )
1243 mImpl->mEventData->mInputStyle.shadowProperties = shadowProperties;
1247 const std::string& Controller::GetInputShadowProperties() const
1249 if( NULL != mImpl->mEventData )
1251 return mImpl->mEventData->mInputStyle.shadowProperties;
1254 return GetDefaultShadowProperties();
1257 void Controller::SetInputUnderlineProperties( const std::string& underlineProperties )
1259 if( NULL != mImpl->mEventData )
1261 mImpl->mEventData->mInputStyle.underlineProperties = underlineProperties;
1265 const std::string& Controller::GetInputUnderlineProperties() const
1267 if( NULL != mImpl->mEventData )
1269 return mImpl->mEventData->mInputStyle.underlineProperties;
1272 return GetDefaultUnderlineProperties();
1275 void Controller::SetInputEmbossProperties( const std::string& embossProperties )
1277 if( NULL != mImpl->mEventData )
1279 mImpl->mEventData->mInputStyle.embossProperties = embossProperties;
1283 const std::string& Controller::GetInputEmbossProperties() const
1285 if( NULL != mImpl->mEventData )
1287 return mImpl->mEventData->mInputStyle.embossProperties;
1290 return GetDefaultEmbossProperties();
1293 void Controller::SetInputOutlineProperties( const std::string& outlineProperties )
1295 if( NULL != mImpl->mEventData )
1297 mImpl->mEventData->mInputStyle.outlineProperties = outlineProperties;
1301 const std::string& Controller::GetInputOutlineProperties() const
1303 if( NULL != mImpl->mEventData )
1305 return mImpl->mEventData->mInputStyle.outlineProperties;
1308 return GetDefaultOutlineProperties();
1311 void Controller::SetEnableCursorBlink( bool enable )
1313 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
1315 if( NULL != mImpl->mEventData )
1317 mImpl->mEventData->mCursorBlinkEnabled = enable;
1320 mImpl->mEventData->mDecorator )
1322 mImpl->mEventData->mDecorator->StopCursorBlink();
1327 bool Controller::GetEnableCursorBlink() const
1329 if( NULL != mImpl->mEventData )
1331 return mImpl->mEventData->mCursorBlinkEnabled;
1337 const Vector2& Controller::GetScrollPosition() const
1339 return mImpl->mScrollPosition;
1342 Vector3 Controller::GetNaturalSize()
1344 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
1345 Vector3 naturalSize;
1347 // Make sure the model is up-to-date before layouting
1348 ProcessModifyEvents();
1350 if( mImpl->mRecalculateNaturalSize )
1352 // Operations that can be done only once until the text changes.
1353 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1360 GET_GLYPH_METRICS );
1361 // Make sure the model is up-to-date before layouting
1362 mImpl->UpdateModel( onlyOnceOperations );
1364 // Layout the text for the new width.
1365 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1367 // Set the update info to relayout the whole text.
1368 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1369 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1371 // Store the actual control's size to restore later.
1372 const Size actualControlSize = mImpl->mVisualModel->mControlSize;
1374 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
1375 static_cast<OperationsMask>( onlyOnceOperations |
1377 naturalSize.GetVectorXY() );
1379 // Do not do again the only once operations.
1380 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1382 // Do the size related operations again.
1383 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1386 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1388 // Stores the natural size to avoid recalculate it again
1389 // unless the text/style changes.
1390 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
1392 mImpl->mRecalculateNaturalSize = false;
1394 // Clear the update info. This info will be set the next time the text is updated.
1395 mImpl->mTextUpdateInfo.Clear();
1397 // Restore the actual control's size.
1398 mImpl->mVisualModel->mControlSize = actualControlSize;
1400 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1404 naturalSize = mImpl->mVisualModel->GetNaturalSize();
1406 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1409 naturalSize.x = ConvertToEven( naturalSize.x );
1410 naturalSize.y = ConvertToEven( naturalSize.y );
1415 float Controller::GetHeightForWidth( float width )
1417 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1418 // Make sure the model is up-to-date before layouting
1419 ProcessModifyEvents();
1422 if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
1424 // Operations that can be done only once until the text changes.
1425 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1432 GET_GLYPH_METRICS );
1433 // Make sure the model is up-to-date before layouting
1434 mImpl->UpdateModel( onlyOnceOperations );
1437 // Layout the text for the new width.
1438 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1440 // Set the update info to relayout the whole text.
1441 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1442 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1444 // Store the actual control's width.
1445 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
1447 DoRelayout( Size( width, MAX_FLOAT ),
1448 static_cast<OperationsMask>( onlyOnceOperations |
1452 // Do not do again the only once operations.
1453 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1455 // Do the size related operations again.
1456 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1460 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1462 // Clear the update info. This info will be set the next time the text is updated.
1463 mImpl->mTextUpdateInfo.Clear();
1465 // Restore the actual control's width.
1466 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1468 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1472 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1473 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1476 return layoutSize.height;
1479 Controller::UpdateTextType Controller::Relayout( const Size& size )
1481 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", this, size.width, size.height, (mImpl->mAutoScrollEnabled)?"true":"false" );
1483 UpdateTextType updateTextType = NONE_UPDATED;
1485 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1487 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1489 mImpl->mVisualModel->mGlyphPositions.Clear();
1490 updateTextType = MODEL_UPDATED;
1493 // Clear the update info. This info will be set the next time the text is updated.
1494 mImpl->mTextUpdateInfo.Clear();
1496 // Not worth to relayout if width or height is equal to zero.
1497 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1499 return updateTextType;
1502 // Whether a new size has been set.
1503 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1507 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1509 // Layout operations that need to be done if the size changes.
1510 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1513 UPDATE_LAYOUT_SIZE |
1515 // Set the update info to relayout the whole text.
1516 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
1517 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1520 // Whether there are modify events.
1521 if( 0u != mImpl->mModifyEvents.Count() )
1523 // Style operations that need to be done if the text is modified.
1524 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1528 // Make sure the model is up-to-date before layouting.
1529 ProcessModifyEvents();
1530 bool updated = mImpl->UpdateModel( mImpl->mOperationsPending );
1534 updated = DoRelayout( size,
1535 mImpl->mOperationsPending,
1536 layoutSize ) || updated;
1540 updateTextType = MODEL_UPDATED;
1543 // Do not re-do any operation until something changes.
1544 mImpl->mOperationsPending = NO_OPERATION;
1546 // Whether the text control is editable
1547 const bool isEditable = NULL != mImpl->mEventData;
1549 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
1551 if( newSize && isEditable )
1553 offset = mImpl->mScrollPosition;
1556 if( !isEditable || !IsMultiLineEnabled() )
1558 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
1559 CalculateVerticalOffset( size );
1566 // If there is a new size, the scroll position needs to be clamped.
1567 mImpl->ClampHorizontalScroll( layoutSize );
1569 // Update the decorator's positions is needed if there is a new size.
1570 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mScrollPosition - offset );
1573 // Move the cursor, grab handle etc.
1574 if( mImpl->ProcessInputEvents() )
1576 updateTextType = static_cast<UpdateTextType>( updateTextType | DECORATOR_UPDATED );
1580 // Clear the update info. This info will be set the next time the text is updated.
1581 mImpl->mTextUpdateInfo.Clear();
1582 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1584 return updateTextType;
1587 void Controller::ProcessModifyEvents()
1589 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
1591 if( 0u == events.Count() )
1597 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
1598 endIt = events.End();
1602 const ModifyEvent& event = *it;
1604 if( ModifyEvent::TEXT_REPLACED == event.type )
1606 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
1607 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
1609 TextReplacedEvent();
1611 else if( ModifyEvent::TEXT_INSERTED == event.type )
1613 TextInsertedEvent();
1615 else if( ModifyEvent::TEXT_DELETED == event.type )
1617 // Placeholder-text cannot be deleted
1618 if( !mImpl->IsShowingPlaceholderText() )
1625 if( NULL != mImpl->mEventData )
1627 // When the text is being modified, delay cursor blinking
1628 mImpl->mEventData->mDecorator->DelayCursorBlink();
1631 // Discard temporary text
1635 bool Controller::IsInputStyleChangedSignalsQueueEmpty()
1637 return ( NULL == mImpl->mEventData ) || ( 0u == mImpl->mEventData->mInputStyleChangedQueue.Count() );
1640 void Controller::ProcessInputStyleChangedSignals()
1642 if( NULL == mImpl->mEventData )
1648 for( Vector<InputStyle::Mask>::ConstIterator it = mImpl->mEventData->mInputStyleChangedQueue.Begin(),
1649 endIt = mImpl->mEventData->mInputStyleChangedQueue.End();
1653 const InputStyle::Mask mask = *it;
1655 // Emit the input style changed signal.
1656 mImpl->mControlInterface.InputStyleChanged( mask );
1659 mImpl->mEventData->mInputStyleChangedQueue.Clear();
1662 void Controller::ResetText()
1665 mImpl->mLogicalModel->mText.Clear();
1667 // We have cleared everything including the placeholder-text
1668 mImpl->PlaceholderCleared();
1670 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1671 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1672 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
1674 // Clear any previous text.
1675 mImpl->mTextUpdateInfo.mClearAll = true;
1677 // The natural size needs to be re-calculated.
1678 mImpl->mRecalculateNaturalSize = true;
1680 // Apply modifications to the model
1681 mImpl->mOperationsPending = ALL_OPERATIONS;
1684 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
1686 // Reset the cursor position
1687 if( NULL != mImpl->mEventData )
1689 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
1691 // Update the cursor if it's in editing mode.
1692 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1694 mImpl->mEventData->mUpdateCursorPosition = true;
1699 void Controller::ResetScrollPosition()
1701 if( NULL != mImpl->mEventData )
1703 // Reset the scroll position.
1704 mImpl->mScrollPosition = Vector2::ZERO;
1705 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1709 void Controller::TextReplacedEvent()
1711 // The natural size needs to be re-calculated.
1712 mImpl->mRecalculateNaturalSize = true;
1714 // Apply modifications to the model
1715 mImpl->mOperationsPending = ALL_OPERATIONS;
1718 void Controller::TextInsertedEvent()
1720 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
1722 if( NULL == mImpl->mEventData )
1727 // The natural size needs to be re-calculated.
1728 mImpl->mRecalculateNaturalSize = true;
1730 // Apply modifications to the model; TODO - Optimize this
1731 mImpl->mOperationsPending = ALL_OPERATIONS;
1734 void Controller::TextDeletedEvent()
1736 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
1738 if( NULL == mImpl->mEventData )
1743 // The natural size needs to be re-calculated.
1744 mImpl->mRecalculateNaturalSize = true;
1746 // Apply modifications to the model; TODO - Optimize this
1747 mImpl->mOperationsPending = ALL_OPERATIONS;
1750 bool Controller::DoRelayout( const Size& size,
1751 OperationsMask operationsRequired,
1754 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
1755 bool viewUpdated( false );
1757 // Calculate the operations to be done.
1758 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
1760 const CharacterIndex startIndex = mImpl->mTextUpdateInfo.mParagraphCharacterIndex;
1761 const Length requestedNumberOfCharacters = mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters;
1763 // Get the current layout size.
1764 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1766 if( NO_OPERATION != ( LAYOUT & operations ) )
1768 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
1770 // Some vectors with data needed to layout and reorder may be void
1771 // after the first time the text has been laid out.
1772 // Fill the vectors again.
1774 // Calculate the number of glyphs to layout.
1775 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1776 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1777 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
1778 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
1780 const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
1781 const GlyphIndex startGlyphIndex = mImpl->mTextUpdateInfo.mStartGlyphIndex;
1782 const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
1783 const Length totalNumberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
1785 if( 0u == totalNumberOfGlyphs )
1787 if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
1789 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
1792 // Nothing else to do if there is no glyphs.
1793 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1797 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1798 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1799 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1800 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1801 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1802 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1803 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1805 // Set the layout parameters.
1806 LayoutParameters layoutParameters( size,
1808 lineBreakInfo.Begin(),
1809 wordBreakInfo.Begin(),
1810 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1812 glyphsToCharactersMap.Begin(),
1813 charactersPerGlyph.Begin(),
1814 charactersToGlyphBuffer,
1815 glyphsPerCharacterBuffer,
1816 totalNumberOfGlyphs );
1818 // Resize the vector of positions to have the same size than the vector of glyphs.
1819 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1820 glyphPositions.Resize( totalNumberOfGlyphs );
1822 // Whether the last character is a new paragraph character.
1823 mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1824 layoutParameters.isLastNewParagraph = mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph;
1826 // The initial glyph and the number of glyphs to layout.
1827 layoutParameters.startGlyphIndex = startGlyphIndex;
1828 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1829 layoutParameters.startLineIndex = mImpl->mTextUpdateInfo.mStartLineIndex;
1830 layoutParameters.estimatedNumberOfLines = mImpl->mTextUpdateInfo.mEstimatedNumberOfLines;
1832 // Update the visual model.
1834 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1836 mImpl->mVisualModel->mLines,
1839 viewUpdated = viewUpdated || ( newLayoutSize != layoutSize );
1843 layoutSize = newLayoutSize;
1845 if ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) )
1847 mImpl->mAutoScrollDirectionRTL = false;
1850 // Reorder the lines
1851 if( NO_OPERATION != ( REORDER & operations ) )
1853 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1854 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1856 // Check first if there are paragraphs with bidirectional info.
1857 if( 0u != bidirectionalInfo.Count() )
1860 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1862 // Reorder the lines.
1863 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1864 ReorderLines( bidirectionalInfo,
1866 requestedNumberOfCharacters,
1867 mImpl->mVisualModel->mLines,
1868 bidirectionalLineInfo );
1870 // Set the bidirectional info per line into the layout parameters.
1871 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1872 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1874 // Re-layout the text. Reorder those lines with right to left characters.
1875 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1877 requestedNumberOfCharacters,
1880 if ( ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) ) && ( numberOfLines > 0 ) )
1882 const LineRun* const firstline = mImpl->mVisualModel->mLines.Begin();
1885 mImpl->mAutoScrollDirectionRTL = firstline->direction;
1891 // Sets the layout size.
1892 if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
1894 mImpl->mVisualModel->SetLayoutSize( layoutSize );
1898 // Store the size used to layout the text.
1899 mImpl->mVisualModel->mControlSize = size;
1902 if( NO_OPERATION != ( ALIGN & operations ) )
1904 // The laid-out lines.
1905 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1907 mImpl->mLayoutEngine.Align( size,
1909 requestedNumberOfCharacters,
1914 #if defined(DEBUG_ENABLED)
1915 std::string currentText;
1916 GetText( currentText );
1917 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mAutoScrollDirectionRTL[%s] [%s]\n", this, (mImpl->mAutoScrollDirectionRTL)?"true":"false", currentText.c_str() );
1919 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1923 void Controller::SetMultiLineEnabled( bool enable )
1925 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1927 if( layout != mImpl->mLayoutEngine.GetLayout() )
1929 // Set the layout type.
1930 mImpl->mLayoutEngine.SetLayout( layout );
1932 // Set the flags to redo the layout operations
1933 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1934 UPDATE_LAYOUT_SIZE |
1938 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1940 mImpl->RequestRelayout();
1944 bool Controller::IsMultiLineEnabled() const
1946 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1949 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1951 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1953 // Set the alignment.
1954 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1956 // Set the flag to redo the alignment operation.
1957 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1959 mImpl->RequestRelayout();
1963 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1965 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1968 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1970 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1972 // Set the alignment.
1973 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1975 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1977 mImpl->RequestRelayout();
1981 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1983 return mImpl->mLayoutEngine.GetVerticalAlignment();
1986 void Controller::CalculateVerticalOffset( const Size& controlSize )
1988 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1990 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1992 // Get the line height of the default font.
1993 layoutSize.height = mImpl->GetDefaultFontLineHeight();
1996 switch( mImpl->mLayoutEngine.GetVerticalAlignment() )
1998 case LayoutEngine::VERTICAL_ALIGN_TOP:
2000 mImpl->mScrollPosition.y = 0.f;
2003 case LayoutEngine::VERTICAL_ALIGN_CENTER:
2005 mImpl->mScrollPosition.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
2008 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
2010 mImpl->mScrollPosition.y = controlSize.height - layoutSize.height;
2016 LayoutEngine& Controller::GetLayoutEngine()
2018 return mImpl->mLayoutEngine;
2021 View& Controller::GetView()
2023 return mImpl->mView;
2026 void Controller::KeyboardFocusGainEvent()
2028 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
2030 if( NULL != mImpl->mEventData )
2032 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
2033 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
2035 mImpl->ChangeState( EventData::EDITING );
2036 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
2038 mImpl->NotifyImfMultiLineStatus();
2039 if( mImpl->IsShowingPlaceholderText() )
2041 // Show alternative placeholder-text when editing
2042 ShowPlaceholderText();
2045 mImpl->RequestRelayout();
2049 void Controller::KeyboardFocusLostEvent()
2051 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
2053 if( NULL != mImpl->mEventData )
2055 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
2057 mImpl->ChangeState( EventData::INACTIVE );
2059 if( !mImpl->IsShowingRealText() )
2061 // Revert to regular placeholder-text when not editing
2062 ShowPlaceholderText();
2066 mImpl->RequestRelayout();
2069 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
2071 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
2073 bool textChanged( false );
2075 if( ( NULL != mImpl->mEventData ) &&
2076 ( keyEvent.state == KeyEvent::Down ) )
2078 int keyCode = keyEvent.keyCode;
2079 const std::string& keyString = keyEvent.keyPressed;
2081 // Pre-process to separate modifying events from non-modifying input events.
2082 if( Dali::DALI_KEY_ESCAPE == keyCode )
2084 // Escape key is a special case which causes focus loss
2085 KeyboardFocusLostEvent();
2087 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
2088 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
2089 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
2090 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
2092 Event event( Event::CURSOR_KEY_EVENT );
2093 event.p1.mInt = keyCode;
2094 mImpl->mEventData->mEventQueue.push_back( event );
2096 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
2098 textChanged = BackspaceKeyEvent();
2100 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
2102 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
2103 // Avoids calling the InsertText() method which can delete selected text
2105 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
2106 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
2108 mImpl->ChangeState( EventData::INACTIVE );
2109 // Menu/Home key behaviour does not allow edit mode to resume like Power key
2110 // Avoids calling the InsertText() method which can delete selected text
2112 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
2114 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
2115 // and a character is typed after the type of a upper case latin character.
2121 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
2123 // IMF manager is no longer handling key-events
2124 mImpl->ClearPreEditFlag();
2126 InsertText( keyString, COMMIT );
2130 if ( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
2131 ( mImpl->mEventData->mState != EventData::INACTIVE ) &&
2132 ( Dali::DALI_KEY_SHIFT_LEFT != keyCode ) )
2134 // Should not change the state if the key is the shift send by the imf manager.
2135 // Otherwise, when the state is SELECTING the text controller can't send the right
2136 // surrounding info to the imf.
2137 mImpl->ChangeState( EventData::EDITING );
2140 mImpl->RequestRelayout();
2145 // Do this last since it provides callbacks into application code
2146 mImpl->mControlInterface.TextChanged();
2152 void Controller::InsertText( const std::string& text, Controller::InsertType type )
2154 bool removedPrevious( false );
2155 bool maxLengthReached( false );
2157 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
2159 if( NULL == mImpl->mEventData )
2164 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
2165 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
2166 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2168 // TODO: At the moment the underline runs are only for pre-edit.
2169 mImpl->mVisualModel->mUnderlineRuns.Clear();
2171 // Keep the current number of characters.
2172 const Length currentNumberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2174 // Remove the previous IMF pre-edit.
2175 if( mImpl->mEventData->mPreEditFlag && ( 0u != mImpl->mEventData->mPreEditLength ) )
2177 removedPrevious = RemoveText( -static_cast<int>( mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition ),
2178 mImpl->mEventData->mPreEditLength,
2179 DONT_UPDATE_INPUT_STYLE );
2181 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
2182 mImpl->mEventData->mPreEditLength = 0u;
2186 // Remove the previous Selection.
2187 removedPrevious = RemoveSelectedText();
2190 Vector<Character> utf32Characters;
2191 Length characterCount = 0u;
2195 // Convert text into UTF-32
2196 utf32Characters.Resize( text.size() );
2198 // This is a bit horrible but std::string returns a (signed) char*
2199 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
2201 // Transform a text array encoded in utf8 into an array encoded in utf32.
2202 // It returns the actual number of characters.
2203 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
2204 utf32Characters.Resize( characterCount );
2206 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
2207 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
2210 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
2212 // The placeholder text is no longer needed
2213 if( mImpl->IsShowingPlaceholderText() )
2218 mImpl->ChangeState( EventData::EDITING );
2220 // Handle the IMF (predicitive text) state changes
2221 if( COMMIT == type )
2223 // IMF manager is no longer handling key-events
2224 mImpl->ClearPreEditFlag();
2228 if( !mImpl->mEventData->mPreEditFlag )
2230 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state\n" );
2232 // Record the start of the pre-edit text
2233 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
2236 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
2237 mImpl->mEventData->mPreEditFlag = true;
2239 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2242 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
2244 // Restrict new text to fit within Maximum characters setting.
2245 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
2246 maxLengthReached = ( characterCount > maxSizeOfNewText );
2248 // The cursor position.
2249 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
2251 // Update the text's style.
2253 // Updates the text style runs by adding characters.
2254 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
2256 // Get the character index from the cursor index.
2257 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
2259 // Retrieve the text's style for the given index.
2261 mImpl->RetrieveDefaultInputStyle( style );
2262 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
2264 // Whether to add a new text color run.
2265 const bool addColorRun = ( style.textColor != mImpl->mEventData->mInputStyle.textColor );
2267 // Whether to add a new font run.
2268 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
2269 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
2270 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
2271 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
2272 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
2277 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
2278 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
2280 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
2281 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
2282 colorRun.characterRun.characterIndex = cursorIndex;
2283 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2286 if( addFontNameRun ||
2292 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
2293 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
2295 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
2297 if( addFontNameRun )
2299 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
2300 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
2301 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
2302 fontDescriptionRun.familyDefined = true;
2304 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
2307 if( addFontWeightRun )
2309 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
2310 fontDescriptionRun.weightDefined = true;
2313 if( addFontWidthRun )
2315 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
2316 fontDescriptionRun.widthDefined = true;
2319 if( addFontSlantRun )
2321 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
2322 fontDescriptionRun.slantDefined = true;
2325 if( addFontSizeRun )
2327 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
2328 fontDescriptionRun.sizeDefined = true;
2331 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
2332 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2335 // Insert at current cursor position.
2336 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
2338 if( cursorIndex < numberOfCharactersInModel )
2340 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2344 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2347 // Mark the first paragraph to be updated.
2348 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
2349 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
2351 // Update the cursor index.
2352 cursorIndex += maxSizeOfNewText;
2354 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
2357 const Length numberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2359 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
2360 mImpl->IsPlaceholderAvailable() )
2362 // Show place-holder if empty after removing the pre-edit text
2363 ShowPlaceholderText();
2364 mImpl->mEventData->mUpdateCursorPosition = true;
2365 mImpl->ClearPreEditFlag();
2367 else if( removedPrevious ||
2368 ( 0 != utf32Characters.Count() ) )
2370 // Queue an inserted event
2371 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
2373 mImpl->mEventData->mUpdateCursorPosition = true;
2374 if( numberOfCharacters < currentNumberOfCharacters )
2376 mImpl->mEventData->mScrollAfterDelete = true;
2380 mImpl->mEventData->mScrollAfterUpdatePosition = true;
2384 if( maxLengthReached )
2386 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
2388 mImpl->ResetImfManager();
2390 // Do this last since it provides callbacks into application code
2391 mImpl->mControlInterface.MaxLengthReached();
2395 bool Controller::RemoveSelectedText()
2397 bool textRemoved( false );
2399 if( EventData::SELECTING == mImpl->mEventData->mState )
2401 std::string removedString;
2402 mImpl->RetrieveSelection( removedString, true );
2404 if( !removedString.empty() )
2407 mImpl->ChangeState( EventData::EDITING );
2414 void Controller::TapEvent( unsigned int tapCount, float x, float y )
2416 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
2418 if( NULL != mImpl->mEventData )
2420 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
2421 EventData::State state( mImpl->mEventData->mState );
2422 bool relayoutNeeded( false ); // to avoid unnecessary relayouts when tapping an empty text-field
2424 if( mImpl->IsClipboardVisible() )
2426 if( EventData::INACTIVE == state || EventData::EDITING == state)
2428 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2430 relayoutNeeded = true;
2432 else if( 1u == tapCount )
2434 if( EventData::EDITING_WITH_POPUP == state || EventData::EDITING_WITH_PASTE_POPUP == state )
2436 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE ); // If Popup shown hide it here so can be shown again if required.
2439 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != state ) )
2441 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2442 relayoutNeeded = true;
2446 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2448 // Hide placeholder text
2452 if( EventData::INACTIVE == state )
2454 mImpl->ChangeState( EventData::EDITING );
2456 else if( !mImpl->IsClipboardEmpty() )
2458 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2460 relayoutNeeded = true;
2463 else if( 2u == tapCount )
2465 if( mImpl->mEventData->mSelectionEnabled &&
2466 mImpl->IsShowingRealText() )
2468 SelectEvent( x, y, false );
2471 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2472 if( relayoutNeeded )
2474 Event event( Event::TAP_EVENT );
2475 event.p1.mUint = tapCount;
2476 event.p2.mFloat = x;
2477 event.p3.mFloat = y;
2478 mImpl->mEventData->mEventQueue.push_back( event );
2480 mImpl->RequestRelayout();
2484 // Reset keyboard as tap event has occurred.
2485 mImpl->ResetImfManager();
2488 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2490 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2492 if( NULL != mImpl->mEventData )
2494 Event event( Event::PAN_EVENT );
2495 event.p1.mInt = state;
2496 event.p2.mFloat = displacement.x;
2497 event.p3.mFloat = displacement.y;
2498 mImpl->mEventData->mEventQueue.push_back( event );
2500 mImpl->RequestRelayout();
2504 void Controller::LongPressEvent( Gesture::State state, float x, float y )
2506 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2508 if( ( state == Gesture::Started ) &&
2509 ( NULL != mImpl->mEventData ) )
2511 if( !mImpl->IsShowingRealText() )
2513 Event event( Event::LONG_PRESS_EVENT );
2514 event.p1.mInt = state;
2515 mImpl->mEventData->mEventQueue.push_back( event );
2516 mImpl->RequestRelayout();
2520 // The 1st long-press on inactive text-field is treated as tap
2521 if( EventData::INACTIVE == mImpl->mEventData->mState )
2523 mImpl->ChangeState( EventData::EDITING );
2525 Event event( Event::TAP_EVENT );
2527 event.p2.mFloat = x;
2528 event.p3.mFloat = y;
2529 mImpl->mEventData->mEventQueue.push_back( event );
2531 mImpl->RequestRelayout();
2533 else if( !mImpl->IsClipboardVisible() )
2535 // Reset the imf manger to commit the pre-edit before selecting the text.
2536 mImpl->ResetImfManager();
2538 SelectEvent( x, y, false );
2544 void Controller::SelectEvent( float x, float y, bool selectAll )
2546 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2548 if( NULL != mImpl->mEventData )
2552 Event event( Event::SELECT_ALL );
2553 mImpl->mEventData->mEventQueue.push_back( event );
2557 Event event( Event::SELECT );
2558 event.p2.mFloat = x;
2559 event.p3.mFloat = y;
2560 mImpl->mEventData->mEventQueue.push_back( event );
2563 mImpl->RequestRelayout();
2567 void Controller::GetTargetSize( Vector2& targetSize )
2569 targetSize = mImpl->mVisualModel->mControlSize;
2572 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2574 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2577 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2579 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2581 if( NULL != mImpl->mEventData )
2583 switch( handleType )
2587 Event event( Event::GRAB_HANDLE_EVENT );
2588 event.p1.mUint = state;
2589 event.p2.mFloat = x;
2590 event.p3.mFloat = y;
2592 mImpl->mEventData->mEventQueue.push_back( event );
2595 case LEFT_SELECTION_HANDLE:
2597 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2598 event.p1.mUint = state;
2599 event.p2.mFloat = x;
2600 event.p3.mFloat = y;
2602 mImpl->mEventData->mEventQueue.push_back( event );
2605 case RIGHT_SELECTION_HANDLE:
2607 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2608 event.p1.mUint = state;
2609 event.p2.mFloat = x;
2610 event.p3.mFloat = y;
2612 mImpl->mEventData->mEventQueue.push_back( event );
2615 case LEFT_SELECTION_HANDLE_MARKER:
2616 case RIGHT_SELECTION_HANDLE_MARKER:
2618 // Markers do not move the handles.
2621 case HANDLE_TYPE_COUNT:
2623 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2627 mImpl->RequestRelayout();
2631 void Controller::PasteText( const std::string& stringToPaste )
2633 InsertText( stringToPaste, Text::Controller::COMMIT );
2634 mImpl->ChangeState( EventData::EDITING );
2635 mImpl->RequestRelayout();
2637 // Do this last since it provides callbacks into application code
2638 mImpl->mControlInterface.TextChanged();
2641 void Controller::PasteClipboardItemEvent()
2643 // Retrieve the clipboard contents first
2644 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2645 std::string stringToPaste( notifier.GetContent() );
2647 // Commit the current pre-edit text; the contents of the clipboard should be appended
2648 mImpl->ResetImfManager();
2650 // Temporary disable hiding clipboard
2651 mImpl->SetClipboardHideEnable( false );
2654 PasteText( stringToPaste );
2656 mImpl->SetClipboardHideEnable( true );
2659 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2661 if( NULL == mImpl->mEventData )
2668 case Toolkit::TextSelectionPopup::CUT:
2670 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2671 mImpl->mOperationsPending = ALL_OPERATIONS;
2673 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2674 !mImpl->IsPlaceholderAvailable() )
2676 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2680 ShowPlaceholderText();
2683 mImpl->mEventData->mUpdateCursorPosition = true;
2684 mImpl->mEventData->mScrollAfterDelete = true;
2686 mImpl->RequestRelayout();
2687 mImpl->mControlInterface.TextChanged();
2690 case Toolkit::TextSelectionPopup::COPY:
2692 mImpl->SendSelectionToClipboard( false ); // Text not modified
2694 mImpl->mEventData->mUpdateCursorPosition = true;
2696 mImpl->RequestRelayout(); // Cursor, Handles, Selection Highlight, Popup
2699 case Toolkit::TextSelectionPopup::PASTE:
2701 mImpl->RequestGetTextFromClipboard(); // Request clipboard service to retrieve an item
2704 case Toolkit::TextSelectionPopup::SELECT:
2706 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2708 if( mImpl->mEventData->mSelectionEnabled )
2710 // Creates a SELECT event.
2711 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2715 case Toolkit::TextSelectionPopup::SELECT_ALL:
2717 // Creates a SELECT_ALL event
2718 SelectEvent( 0.f, 0.f, true );
2721 case Toolkit::TextSelectionPopup::CLIPBOARD:
2723 mImpl->ShowClipboard();
2726 case Toolkit::TextSelectionPopup::NONE:
2734 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2736 // Whether the text needs to be relaid-out.
2737 bool requestRelayout = false;
2739 // Whether to retrieve the text and cursor position to be sent to the IMF manager.
2740 bool retrieveText = false;
2741 bool retrieveCursor = false;
2743 switch( imfEvent.eventName )
2745 case ImfManager::COMMIT:
2747 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2748 requestRelayout = true;
2749 retrieveCursor = true;
2752 case ImfManager::PREEDIT:
2754 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2755 requestRelayout = true;
2756 retrieveCursor = true;
2759 case ImfManager::DELETESURROUNDING:
2761 const bool textDeleted = RemoveText( imfEvent.cursorOffset,
2762 imfEvent.numberOfChars,
2763 DONT_UPDATE_INPUT_STYLE );
2767 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2768 !mImpl->IsPlaceholderAvailable() )
2770 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2774 ShowPlaceholderText();
2776 mImpl->mEventData->mUpdateCursorPosition = true;
2777 mImpl->mEventData->mScrollAfterDelete = true;
2779 requestRelayout = true;
2783 case ImfManager::GETSURROUNDING:
2785 retrieveText = true;
2786 retrieveCursor = true;
2789 case ImfManager::VOID:
2796 if( requestRelayout )
2798 mImpl->mOperationsPending = ALL_OPERATIONS;
2799 mImpl->RequestRelayout();
2803 CharacterIndex cursorPosition = 0u;
2804 Length numberOfWhiteSpaces = 0u;
2806 if( retrieveCursor )
2808 numberOfWhiteSpaces = mImpl->GetNumberOfWhiteSpaces( 0u );
2810 cursorPosition = mImpl->GetLogicalCursorPosition();
2812 if( cursorPosition < numberOfWhiteSpaces )
2814 cursorPosition = 0u;
2818 cursorPosition -= numberOfWhiteSpaces;
2824 mImpl->GetText( numberOfWhiteSpaces, text );
2827 ImfManager::ImfCallbackData callbackData( ( retrieveText || retrieveCursor ), cursorPosition, text, false );
2829 if( requestRelayout )
2831 // Do this last since it provides callbacks into application code
2832 mImpl->mControlInterface.TextChanged();
2835 return callbackData;
2838 Controller::~Controller()
2843 bool Controller::BackspaceKeyEvent()
2845 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2847 bool removed = false;
2849 if( NULL == mImpl->mEventData )
2854 // IMF manager is no longer handling key-events
2855 mImpl->ClearPreEditFlag();
2857 if( EventData::SELECTING == mImpl->mEventData->mState )
2859 removed = RemoveSelectedText();
2861 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2863 // Remove the character before the current cursor position
2864 removed = RemoveText( -1,
2866 UPDATE_INPUT_STYLE );
2871 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2872 !mImpl->IsPlaceholderAvailable() )
2874 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2878 ShowPlaceholderText();
2880 mImpl->mEventData->mUpdateCursorPosition = true;
2881 mImpl->mEventData->mScrollAfterDelete = true;
2887 void Controller::ShowPlaceholderText()
2889 if( mImpl->IsPlaceholderAvailable() )
2891 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2893 if( NULL == mImpl->mEventData )
2898 mImpl->mEventData->mIsShowingPlaceholderText = true;
2900 // Disable handles when showing place-holder text
2901 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2902 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2903 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2905 const char* text( NULL );
2908 // TODO - Switch placeholder text styles when changing state
2909 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2910 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2912 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2913 size = mImpl->mEventData->mPlaceholderTextActive.size();
2917 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2918 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2921 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2922 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2924 // Reset model for showing placeholder.
2925 mImpl->mLogicalModel->mText.Clear();
2926 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2928 // Convert text into UTF-32
2929 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2930 utf32Characters.Resize( size );
2932 // This is a bit horrible but std::string returns a (signed) char*
2933 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2935 // Transform a text array encoded in utf8 into an array encoded in utf32.
2936 // It returns the actual number of characters.
2937 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2938 utf32Characters.Resize( characterCount );
2940 // The characters to be added.
2941 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = characterCount;
2943 // Reset the cursor position
2944 mImpl->mEventData->mPrimaryCursorPosition = 0;
2946 // The natural size needs to be re-calculated.
2947 mImpl->mRecalculateNaturalSize = true;
2949 // Apply modifications to the model
2950 mImpl->mOperationsPending = ALL_OPERATIONS;
2952 // Update the rest of the model during size negotiation
2953 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2957 void Controller::ClearFontData()
2959 if( mImpl->mFontDefaults )
2961 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2964 // Set flags to update the model.
2965 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2966 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2967 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
2969 mImpl->mTextUpdateInfo.mClearAll = true;
2970 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
2971 mImpl->mRecalculateNaturalSize = true;
2973 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2978 UPDATE_LAYOUT_SIZE |
2983 void Controller::ClearStyleData()
2985 mImpl->mLogicalModel->mColorRuns.Clear();
2986 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2989 Controller::Controller( ControlInterface& controlInterface )
2992 mImpl = new Controller::Impl( controlInterface );
2997 } // namespace Toolkit