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>
34 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
39 #if defined(DEBUG_ENABLED)
40 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
43 const float MAX_FLOAT = std::numeric_limits<float>::max();
45 const std::string EMPTY_STRING("");
47 float ConvertToEven( float value )
49 int intValue(static_cast<int>( value ));
50 return static_cast<float>(intValue % 2 == 0) ? intValue : (intValue + 1);
65 * @brief Adds a new font description run for the selected text.
67 * The new font parameters are added after the call to this method.
69 * @param[in] eventData The event data pointer.
70 * @param[in] logicalModel The logical model where to add the new font description run.
71 * @param[out] startOfSelectedText Index to the first selected character.
72 * @param[out] lengthOfSelectedText Number of selected characters.
74 FontDescriptionRun& UpdateSelectionFontStyleRun( EventData* eventData,
75 LogicalModelPtr logicalModel,
76 CharacterIndex& startOfSelectedText,
77 Length& lengthOfSelectedText )
79 const bool handlesCrossed = eventData->mLeftSelectionPosition > eventData->mRightSelectionPosition;
81 // Get start and end position of selection
82 startOfSelectedText = handlesCrossed ? eventData->mRightSelectionPosition : eventData->mLeftSelectionPosition;
83 lengthOfSelectedText = ( handlesCrossed ? eventData->mLeftSelectionPosition : eventData->mRightSelectionPosition ) - startOfSelectedText;
86 const VectorBase::SizeType numberOfRuns = logicalModel->mFontDescriptionRuns.Count();
87 logicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
89 FontDescriptionRun& fontDescriptionRun = *( logicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
91 fontDescriptionRun.characterRun.characterIndex = startOfSelectedText;
92 fontDescriptionRun.characterRun.numberOfCharacters = lengthOfSelectedText;
94 // Recalculate the selection highlight as the metrics may have changed.
95 eventData->mUpdateLeftSelectionPosition = true;
96 eventData->mUpdateRightSelectionPosition = true;
97 eventData->mUpdateHighlightBox = true;
99 return fontDescriptionRun;
102 // public : Constructor.
104 ControllerPtr Controller::New()
106 return ControllerPtr( new Controller() );
109 ControllerPtr Controller::New( ControlInterface* controlInterface )
111 return ControllerPtr( new Controller( controlInterface ) );
114 ControllerPtr Controller::New( ControlInterface* controlInterface,
115 EditableControlInterface* editableControlInterface )
117 return ControllerPtr( new Controller( controlInterface,
118 editableControlInterface ) );
121 // public : Configure the text controller.
123 void Controller::EnableTextInput( DecoratorPtr decorator )
125 if( NULL == mImpl->mEventData )
127 mImpl->mEventData = new EventData( decorator );
131 void Controller::SetGlyphType( TextAbstraction::GlyphType glyphType )
133 // Metrics for bitmap & vector based glyphs are different
134 mImpl->mMetrics->SetGlyphType( glyphType );
136 // Clear the font-specific data
139 mImpl->RequestRelayout();
142 void Controller::SetMarkupProcessorEnabled( bool enable )
144 mImpl->mMarkupProcessorEnabled = enable;
147 bool Controller::IsMarkupProcessorEnabled() const
149 return mImpl->mMarkupProcessorEnabled;
152 void Controller::SetAutoScrollEnabled( bool enable )
154 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 );
156 if ( mImpl->mLayoutEngine.GetLayout() == LayoutEngine::SINGLE_LINE_BOX )
160 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled for SINGLE_LINE_BOX\n" );
161 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
171 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled Disabling autoscroll\n");
172 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
179 mImpl->mAutoScrollEnabled = enable;
180 mImpl->RequestRelayout();
184 DALI_LOG_WARNING( "Attempted AutoScrolling on a non SINGLE_LINE_BOX, request ignored\n" );
185 mImpl->mAutoScrollEnabled = false;
189 bool Controller::IsAutoScrollEnabled() const
191 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::IsAutoScrollEnabled[%s]\n", (mImpl->mAutoScrollEnabled)?"true":"false" );
193 return mImpl->mAutoScrollEnabled;
196 CharacterDirection Controller::GetAutoScrollDirection() const
198 return mImpl->mAutoScrollDirectionRTL;
201 float Controller::GetAutoScrollLineAlignment() const
205 if( mImpl->mVisualModel &&
206 ( 0u != mImpl->mVisualModel->mLines.Count() ) )
208 offset = ( *mImpl->mVisualModel->mLines.Begin() ).alignmentOffset;
214 void Controller::SetHorizontalScrollEnabled( bool enable )
216 if( ( NULL != mImpl->mEventData ) &&
217 mImpl->mEventData->mDecorator )
219 mImpl->mEventData->mDecorator->SetHorizontalScrollEnabled( enable );
223 bool Controller::IsHorizontalScrollEnabled() const
225 if( ( NULL != mImpl->mEventData ) &&
226 mImpl->mEventData->mDecorator )
228 return mImpl->mEventData->mDecorator->IsHorizontalScrollEnabled();
234 void Controller::SetVerticalScrollEnabled( bool enable )
236 if( ( NULL != mImpl->mEventData ) &&
237 mImpl->mEventData->mDecorator )
239 if( mImpl->mEventData->mDecorator )
241 mImpl->mEventData->mDecorator->SetVerticalScrollEnabled( enable );
246 bool Controller::IsVerticalScrollEnabled() const
248 if( ( NULL != mImpl->mEventData ) &&
249 mImpl->mEventData->mDecorator )
251 return mImpl->mEventData->mDecorator->IsVerticalScrollEnabled();
257 void Controller::SetSmoothHandlePanEnabled( bool enable )
259 if( ( NULL != mImpl->mEventData ) &&
260 mImpl->mEventData->mDecorator )
262 mImpl->mEventData->mDecorator->SetSmoothHandlePanEnabled( enable );
266 bool Controller::IsSmoothHandlePanEnabled() const
268 if( ( NULL != mImpl->mEventData ) &&
269 mImpl->mEventData->mDecorator )
271 return mImpl->mEventData->mDecorator->IsSmoothHandlePanEnabled();
277 void Controller::SetMaximumNumberOfCharacters( Length maxCharacters )
279 mImpl->mMaximumNumberOfCharacters = maxCharacters;
282 int Controller::GetMaximumNumberOfCharacters()
284 return mImpl->mMaximumNumberOfCharacters;
287 void Controller::SetEnableCursorBlink( bool enable )
289 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
291 if( NULL != mImpl->mEventData )
293 mImpl->mEventData->mCursorBlinkEnabled = enable;
296 mImpl->mEventData->mDecorator )
298 mImpl->mEventData->mDecorator->StopCursorBlink();
303 bool Controller::GetEnableCursorBlink() const
305 if( NULL != mImpl->mEventData )
307 return mImpl->mEventData->mCursorBlinkEnabled;
313 void Controller::SetMultiLineEnabled( bool enable )
315 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
317 if( layout != mImpl->mLayoutEngine.GetLayout() )
319 // Set the layout type.
320 mImpl->mLayoutEngine.SetLayout( layout );
322 // Set the flags to redo the layout operations
323 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
328 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
330 mImpl->RequestRelayout();
334 bool Controller::IsMultiLineEnabled() const
336 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
339 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
341 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
343 // Set the alignment.
344 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
346 // Set the flag to redo the alignment operation.
347 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
349 mImpl->RequestRelayout();
353 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
355 return mImpl->mLayoutEngine.GetHorizontalAlignment();
358 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
360 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
362 // Set the alignment.
363 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
365 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
367 mImpl->RequestRelayout();
371 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
373 return mImpl->mLayoutEngine.GetVerticalAlignment();
378 void Controller::SetText( const std::string& text )
380 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
382 // Reset keyboard as text changed
383 mImpl->ResetImfManager();
385 // Remove the previously set text and style.
391 CharacterIndex lastCursorIndex = 0u;
393 if( NULL != mImpl->mEventData )
395 // If popup shown then hide it by switching to Editing state
396 if( ( EventData::SELECTING == mImpl->mEventData->mState ) ||
397 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
398 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
399 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
401 mImpl->ChangeState( EventData::EDITING );
407 mImpl->mVisualModel->SetTextColor( mImpl->mTextColor );
409 MarkupProcessData markupProcessData( mImpl->mLogicalModel->mColorRuns,
410 mImpl->mLogicalModel->mFontDescriptionRuns );
412 Length textSize = 0u;
413 const uint8_t* utf8 = NULL;
414 if( mImpl->mMarkupProcessorEnabled )
416 ProcessMarkupString( text, markupProcessData );
417 textSize = markupProcessData.markupProcessedText.size();
419 // This is a bit horrible but std::string returns a (signed) char*
420 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
424 textSize = text.size();
426 // This is a bit horrible but std::string returns a (signed) char*
427 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
430 // Convert text into UTF-32
431 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
432 utf32Characters.Resize( textSize );
434 // Transform a text array encoded in utf8 into an array encoded in utf32.
435 // It returns the actual number of characters.
436 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
437 utf32Characters.Resize( characterCount );
439 DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
440 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mLogicalModel->mText.Count() );
442 // The characters to be added.
443 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
445 // To reset the cursor position
446 lastCursorIndex = characterCount;
448 // Update the rest of the model during size negotiation
449 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
451 // The natural size needs to be re-calculated.
452 mImpl->mRecalculateNaturalSize = true;
454 // Apply modifications to the model
455 mImpl->mOperationsPending = ALL_OPERATIONS;
459 ShowPlaceholderText();
462 // Resets the cursor position.
463 ResetCursorPosition( lastCursorIndex );
465 // Scrolls the text to make the cursor visible.
466 ResetScrollPosition();
468 mImpl->RequestRelayout();
470 if( NULL != mImpl->mEventData )
472 // Cancel previously queued events
473 mImpl->mEventData->mEventQueue.clear();
476 // Do this last since it provides callbacks into application code.
477 if( NULL != mImpl->mEditableControlInterface )
479 mImpl->mEditableControlInterface->TextChanged();
483 void Controller::GetText( std::string& text ) const
485 if( !mImpl->IsShowingPlaceholderText() )
487 // Retrieves the text string.
488 mImpl->GetText( 0u, text );
492 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
496 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
498 if( NULL != mImpl->mEventData )
500 if( PLACEHOLDER_TYPE_INACTIVE == type )
502 mImpl->mEventData->mPlaceholderTextInactive = text;
506 mImpl->mEventData->mPlaceholderTextActive = text;
509 // Update placeholder if there is no text
510 if( mImpl->IsShowingPlaceholderText() ||
511 ( 0u == mImpl->mLogicalModel->mText.Count() ) )
513 ShowPlaceholderText();
518 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
520 if( NULL != mImpl->mEventData )
522 if( PLACEHOLDER_TYPE_INACTIVE == type )
524 text = mImpl->mEventData->mPlaceholderTextInactive;
528 text = mImpl->mEventData->mPlaceholderTextActive;
533 void Controller::UpdateAfterFontChange( const std::string& newDefaultFont )
535 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::UpdateAfterFontChange\n");
537 if( !mImpl->mFontDefaults->familyDefined ) // If user defined font then should not update when system font changes
539 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
540 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
544 mImpl->RequestRelayout();
548 // public : Default style & Input style
550 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
552 if( NULL == mImpl->mFontDefaults )
554 mImpl->mFontDefaults = new FontDefaults();
557 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
558 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
559 mImpl->mFontDefaults->familyDefined = !defaultFontFamily.empty();
561 // Clear the font-specific data
564 mImpl->RequestRelayout();
567 const std::string& Controller::GetDefaultFontFamily() const
569 if( NULL != mImpl->mFontDefaults )
571 return mImpl->mFontDefaults->mFontDescription.family;
577 void Controller::SetDefaultFontWeight( FontWeight weight )
579 if( NULL == mImpl->mFontDefaults )
581 mImpl->mFontDefaults = new FontDefaults();
584 mImpl->mFontDefaults->mFontDescription.weight = weight;
585 mImpl->mFontDefaults->weightDefined = true;
587 // Clear the font-specific data
590 mImpl->RequestRelayout();
593 bool Controller::IsDefaultFontWeightDefined() const
595 return mImpl->mFontDefaults->weightDefined;
598 FontWeight Controller::GetDefaultFontWeight() const
600 if( NULL != mImpl->mFontDefaults )
602 return mImpl->mFontDefaults->mFontDescription.weight;
605 return TextAbstraction::FontWeight::NORMAL;
608 void Controller::SetDefaultFontWidth( FontWidth width )
610 if( NULL == mImpl->mFontDefaults )
612 mImpl->mFontDefaults = new FontDefaults();
615 mImpl->mFontDefaults->mFontDescription.width = width;
616 mImpl->mFontDefaults->widthDefined = true;
618 // Clear the font-specific data
621 mImpl->RequestRelayout();
624 bool Controller::IsDefaultFontWidthDefined() const
626 return mImpl->mFontDefaults->widthDefined;
629 FontWidth Controller::GetDefaultFontWidth() const
631 if( NULL != mImpl->mFontDefaults )
633 return mImpl->mFontDefaults->mFontDescription.width;
636 return TextAbstraction::FontWidth::NORMAL;
639 void Controller::SetDefaultFontSlant( FontSlant slant )
641 if( NULL == mImpl->mFontDefaults )
643 mImpl->mFontDefaults = new FontDefaults();
646 mImpl->mFontDefaults->mFontDescription.slant = slant;
647 mImpl->mFontDefaults->slantDefined = true;
649 // Clear the font-specific data
652 mImpl->RequestRelayout();
655 bool Controller::IsDefaultFontSlantDefined() const
657 return mImpl->mFontDefaults->slantDefined;
660 FontSlant Controller::GetDefaultFontSlant() const
662 if( NULL != mImpl->mFontDefaults )
664 return mImpl->mFontDefaults->mFontDescription.slant;
667 return TextAbstraction::FontSlant::NORMAL;
670 void Controller::SetDefaultPointSize( float pointSize )
672 if( NULL == mImpl->mFontDefaults )
674 mImpl->mFontDefaults = new FontDefaults();
677 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
678 mImpl->mFontDefaults->sizeDefined = true;
680 // Clear the font-specific data
683 mImpl->RequestRelayout();
686 float Controller::GetDefaultPointSize() const
688 if( NULL != mImpl->mFontDefaults )
690 return mImpl->mFontDefaults->mDefaultPointSize;
696 void Controller::SetTextColor( const Vector4& textColor )
698 mImpl->mTextColor = textColor;
700 if( !mImpl->IsShowingPlaceholderText() )
702 mImpl->mVisualModel->SetTextColor( textColor );
704 mImpl->RequestRelayout();
708 const Vector4& Controller::GetTextColor() const
710 return mImpl->mTextColor;
713 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
715 if( NULL != mImpl->mEventData )
717 mImpl->mEventData->mPlaceholderTextColor = textColor;
720 if( mImpl->IsShowingPlaceholderText() )
722 mImpl->mVisualModel->SetTextColor( textColor );
723 mImpl->RequestRelayout();
727 const Vector4& Controller::GetPlaceholderTextColor() const
729 if( NULL != mImpl->mEventData )
731 return mImpl->mEventData->mPlaceholderTextColor;
737 void Controller::SetShadowOffset( const Vector2& shadowOffset )
739 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
741 mImpl->RequestRelayout();
744 const Vector2& Controller::GetShadowOffset() const
746 return mImpl->mVisualModel->GetShadowOffset();
749 void Controller::SetShadowColor( const Vector4& shadowColor )
751 mImpl->mVisualModel->SetShadowColor( shadowColor );
753 mImpl->RequestRelayout();
756 const Vector4& Controller::GetShadowColor() const
758 return mImpl->mVisualModel->GetShadowColor();
761 void Controller::SetUnderlineColor( const Vector4& color )
763 mImpl->mVisualModel->SetUnderlineColor( color );
765 mImpl->RequestRelayout();
768 const Vector4& Controller::GetUnderlineColor() const
770 return mImpl->mVisualModel->GetUnderlineColor();
773 void Controller::SetUnderlineEnabled( bool enabled )
775 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
777 mImpl->RequestRelayout();
780 bool Controller::IsUnderlineEnabled() const
782 return mImpl->mVisualModel->IsUnderlineEnabled();
785 void Controller::SetUnderlineHeight( float height )
787 mImpl->mVisualModel->SetUnderlineHeight( height );
789 mImpl->RequestRelayout();
792 float Controller::GetUnderlineHeight() const
794 return mImpl->mVisualModel->GetUnderlineHeight();
797 void Controller::SetDefaultEmbossProperties( const std::string& embossProperties )
799 if( NULL == mImpl->mEmbossDefaults )
801 mImpl->mEmbossDefaults = new EmbossDefaults();
804 mImpl->mEmbossDefaults->properties = embossProperties;
807 const std::string& Controller::GetDefaultEmbossProperties() const
809 if( NULL != mImpl->mEmbossDefaults )
811 return mImpl->mEmbossDefaults->properties;
817 void Controller::SetDefaultOutlineProperties( const std::string& outlineProperties )
819 if( NULL == mImpl->mOutlineDefaults )
821 mImpl->mOutlineDefaults = new OutlineDefaults();
824 mImpl->mOutlineDefaults->properties = outlineProperties;
827 const std::string& Controller::GetDefaultOutlineProperties() const
829 if( NULL != mImpl->mOutlineDefaults )
831 return mImpl->mOutlineDefaults->properties;
837 void Controller::SetDefaultLineSpacing( float lineSpacing )
839 //TODO finish implementation
840 mImpl->mLayoutEngine.SetDefaultLineSpacing( lineSpacing );
843 float Controller::GetDefaultLineSpacing() const
845 return mImpl->mLayoutEngine.GetDefaultLineSpacing();
848 void Controller::SetInputColor( const Vector4& color )
850 if( NULL != mImpl->mEventData )
852 mImpl->mEventData->mInputStyle.textColor = color;
853 mImpl->mEventData->mInputStyle.isDefaultColor = false;
855 if( EventData::SELECTING == mImpl->mEventData->mState )
857 const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
859 // Get start and end position of selection
860 const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
861 const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
863 // Add the color run.
864 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
865 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
867 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
868 colorRun.color = color;
869 colorRun.characterRun.characterIndex = startOfSelectedText;
870 colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
872 // Request to relayout.
873 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
874 mImpl->RequestRelayout();
876 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
877 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
878 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
883 const Vector4& Controller::GetInputColor() const
885 if( NULL != mImpl->mEventData )
887 return mImpl->mEventData->mInputStyle.textColor;
890 // Return the default text's color if there is no EventData.
891 return mImpl->mTextColor;
895 void Controller::SetInputFontFamily( const std::string& fontFamily )
897 if( NULL != mImpl->mEventData )
899 mImpl->mEventData->mInputStyle.familyName = fontFamily;
900 mImpl->mEventData->mInputStyle.isFamilyDefined = true;
902 if( EventData::SELECTING == mImpl->mEventData->mState )
904 CharacterIndex startOfSelectedText = 0u;
905 Length lengthOfSelectedText = 0u;
906 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
907 mImpl->mLogicalModel,
909 lengthOfSelectedText );
911 fontDescriptionRun.familyLength = fontFamily.size();
912 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
913 memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
914 fontDescriptionRun.familyDefined = true;
916 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
918 // Request to relayout.
919 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
927 mImpl->mRecalculateNaturalSize = true;
928 mImpl->RequestRelayout();
930 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
931 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
932 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
934 // As the font changes, recalculate the handle positions is needed.
935 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
936 mImpl->mEventData->mUpdateRightSelectionPosition = true;
937 mImpl->mEventData->mUpdateHighlightBox = true;
938 mImpl->mEventData->mScrollAfterUpdatePosition = true;
943 const std::string& Controller::GetInputFontFamily() const
945 if( NULL != mImpl->mEventData )
947 return mImpl->mEventData->mInputStyle.familyName;
950 // Return the default font's family if there is no EventData.
951 return GetDefaultFontFamily();
954 void Controller::SetInputFontWeight( FontWeight weight )
956 if( NULL != mImpl->mEventData )
958 mImpl->mEventData->mInputStyle.weight = weight;
959 mImpl->mEventData->mInputStyle.isWeightDefined = true;
961 if( EventData::SELECTING == mImpl->mEventData->mState )
963 CharacterIndex startOfSelectedText = 0u;
964 Length lengthOfSelectedText = 0u;
965 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
966 mImpl->mLogicalModel,
968 lengthOfSelectedText );
970 fontDescriptionRun.weight = weight;
971 fontDescriptionRun.weightDefined = true;
973 // Request to relayout.
974 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
982 mImpl->mRecalculateNaturalSize = true;
983 mImpl->RequestRelayout();
985 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
986 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
987 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
989 // As the font might change, recalculate the handle positions is needed.
990 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
991 mImpl->mEventData->mUpdateRightSelectionPosition = true;
992 mImpl->mEventData->mUpdateHighlightBox = true;
993 mImpl->mEventData->mScrollAfterUpdatePosition = true;
998 bool Controller::IsInputFontWeightDefined() const
1000 bool defined = false;
1002 if( NULL != mImpl->mEventData )
1004 defined = mImpl->mEventData->mInputStyle.isWeightDefined;
1010 FontWeight Controller::GetInputFontWeight() const
1012 if( NULL != mImpl->mEventData )
1014 return mImpl->mEventData->mInputStyle.weight;
1017 return GetDefaultFontWeight();
1020 void Controller::SetInputFontWidth( FontWidth width )
1022 if( NULL != mImpl->mEventData )
1024 mImpl->mEventData->mInputStyle.width = width;
1025 mImpl->mEventData->mInputStyle.isWidthDefined = true;
1027 if( EventData::SELECTING == mImpl->mEventData->mState )
1029 CharacterIndex startOfSelectedText = 0u;
1030 Length lengthOfSelectedText = 0u;
1031 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1032 mImpl->mLogicalModel,
1033 startOfSelectedText,
1034 lengthOfSelectedText );
1036 fontDescriptionRun.width = width;
1037 fontDescriptionRun.widthDefined = true;
1039 // Request to relayout.
1040 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1045 UPDATE_LAYOUT_SIZE |
1048 mImpl->mRecalculateNaturalSize = true;
1049 mImpl->RequestRelayout();
1051 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1052 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1053 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1055 // As the font might change, recalculate the handle positions is needed.
1056 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1057 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1058 mImpl->mEventData->mUpdateHighlightBox = true;
1059 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1064 bool Controller::IsInputFontWidthDefined() const
1066 bool defined = false;
1068 if( NULL != mImpl->mEventData )
1070 defined = mImpl->mEventData->mInputStyle.isWidthDefined;
1076 FontWidth Controller::GetInputFontWidth() const
1078 if( NULL != mImpl->mEventData )
1080 return mImpl->mEventData->mInputStyle.width;
1083 return GetDefaultFontWidth();
1086 void Controller::SetInputFontSlant( FontSlant slant )
1088 if( NULL != mImpl->mEventData )
1090 mImpl->mEventData->mInputStyle.slant = slant;
1091 mImpl->mEventData->mInputStyle.isSlantDefined = true;
1093 if( EventData::SELECTING == mImpl->mEventData->mState )
1095 CharacterIndex startOfSelectedText = 0u;
1096 Length lengthOfSelectedText = 0u;
1097 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1098 mImpl->mLogicalModel,
1099 startOfSelectedText,
1100 lengthOfSelectedText );
1102 fontDescriptionRun.slant = slant;
1103 fontDescriptionRun.slantDefined = true;
1105 // Request to relayout.
1106 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1111 UPDATE_LAYOUT_SIZE |
1114 mImpl->mRecalculateNaturalSize = true;
1115 mImpl->RequestRelayout();
1117 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1118 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1119 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1121 // As the font might change, recalculate the handle positions is needed.
1122 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1123 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1124 mImpl->mEventData->mUpdateHighlightBox = true;
1125 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1130 bool Controller::IsInputFontSlantDefined() const
1132 bool defined = false;
1134 if( NULL != mImpl->mEventData )
1136 defined = mImpl->mEventData->mInputStyle.isSlantDefined;
1142 FontSlant Controller::GetInputFontSlant() const
1144 if( NULL != mImpl->mEventData )
1146 return mImpl->mEventData->mInputStyle.slant;
1149 return GetDefaultFontSlant();
1152 void Controller::SetInputFontPointSize( float size )
1154 if( NULL != mImpl->mEventData )
1156 mImpl->mEventData->mInputStyle.size = size;
1157 mImpl->mEventData->mInputStyle.isSizeDefined = true;
1159 if( EventData::SELECTING == mImpl->mEventData->mState )
1161 CharacterIndex startOfSelectedText = 0u;
1162 Length lengthOfSelectedText = 0u;
1163 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1164 mImpl->mLogicalModel,
1165 startOfSelectedText,
1166 lengthOfSelectedText );
1168 fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
1169 fontDescriptionRun.sizeDefined = true;
1171 // Request to relayout.
1172 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1177 UPDATE_LAYOUT_SIZE |
1180 mImpl->mRecalculateNaturalSize = true;
1181 mImpl->RequestRelayout();
1183 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1184 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1185 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1187 // As the font might change, recalculate the handle positions is needed.
1188 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1189 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1190 mImpl->mEventData->mUpdateHighlightBox = true;
1191 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1196 float Controller::GetInputFontPointSize() const
1198 if( NULL != mImpl->mEventData )
1200 return mImpl->mEventData->mInputStyle.size;
1203 // Return the default font's point size if there is no EventData.
1204 return GetDefaultPointSize();
1207 void Controller::SetInputLineSpacing( float lineSpacing )
1209 if( NULL != mImpl->mEventData )
1211 mImpl->mEventData->mInputStyle.lineSpacing = lineSpacing;
1212 mImpl->mEventData->mInputStyle.isLineSpacingDefined = true;
1216 float Controller::GetInputLineSpacing() const
1218 if( NULL != mImpl->mEventData )
1220 return mImpl->mEventData->mInputStyle.lineSpacing;
1226 void Controller::SetInputShadowProperties( const std::string& shadowProperties )
1228 if( NULL != mImpl->mEventData )
1230 mImpl->mEventData->mInputStyle.shadowProperties = shadowProperties;
1234 const std::string& Controller::GetInputShadowProperties() const
1236 if( NULL != mImpl->mEventData )
1238 return mImpl->mEventData->mInputStyle.shadowProperties;
1241 return EMPTY_STRING;
1244 void Controller::SetInputUnderlineProperties( const std::string& underlineProperties )
1246 if( NULL != mImpl->mEventData )
1248 mImpl->mEventData->mInputStyle.underlineProperties = underlineProperties;
1252 const std::string& Controller::GetInputUnderlineProperties() const
1254 if( NULL != mImpl->mEventData )
1256 return mImpl->mEventData->mInputStyle.underlineProperties;
1259 return EMPTY_STRING;
1262 void Controller::SetInputEmbossProperties( const std::string& embossProperties )
1264 if( NULL != mImpl->mEventData )
1266 mImpl->mEventData->mInputStyle.embossProperties = embossProperties;
1270 const std::string& Controller::GetInputEmbossProperties() const
1272 if( NULL != mImpl->mEventData )
1274 return mImpl->mEventData->mInputStyle.embossProperties;
1277 return GetDefaultEmbossProperties();
1280 void Controller::SetInputOutlineProperties( const std::string& outlineProperties )
1282 if( NULL != mImpl->mEventData )
1284 mImpl->mEventData->mInputStyle.outlineProperties = outlineProperties;
1288 const std::string& Controller::GetInputOutlineProperties() const
1290 if( NULL != mImpl->mEventData )
1292 return mImpl->mEventData->mInputStyle.outlineProperties;
1295 return GetDefaultOutlineProperties();
1298 // public : Queries & retrieves.
1300 LayoutEngine& Controller::GetLayoutEngine()
1302 return mImpl->mLayoutEngine;
1305 View& Controller::GetView()
1307 return mImpl->mView;
1310 const Vector2& Controller::GetScrollPosition() const
1312 return mImpl->mScrollPosition;
1315 Vector3 Controller::GetNaturalSize()
1317 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
1318 Vector3 naturalSize;
1320 // Make sure the model is up-to-date before layouting
1321 ProcessModifyEvents();
1323 if( mImpl->mRecalculateNaturalSize )
1325 // Operations that can be done only once until the text changes.
1326 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1333 GET_GLYPH_METRICS );
1334 // Make sure the model is up-to-date before layouting
1335 mImpl->UpdateModel( onlyOnceOperations );
1337 // Layout the text for the new width.
1338 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1340 // Set the update info to relayout the whole text.
1341 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1342 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1344 // Store the actual control's size to restore later.
1345 const Size actualControlSize = mImpl->mVisualModel->mControlSize;
1347 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
1348 static_cast<OperationsMask>( onlyOnceOperations |
1350 naturalSize.GetVectorXY() );
1352 // Do not do again the only once operations.
1353 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1355 // Do the size related operations again.
1356 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1359 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1361 // Stores the natural size to avoid recalculate it again
1362 // unless the text/style changes.
1363 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
1365 mImpl->mRecalculateNaturalSize = false;
1367 // Clear the update info. This info will be set the next time the text is updated.
1368 mImpl->mTextUpdateInfo.Clear();
1370 // Restore the actual control's size.
1371 mImpl->mVisualModel->mControlSize = actualControlSize;
1373 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1377 naturalSize = mImpl->mVisualModel->GetNaturalSize();
1379 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1382 naturalSize.x = ConvertToEven( naturalSize.x );
1383 naturalSize.y = ConvertToEven( naturalSize.y );
1388 float Controller::GetHeightForWidth( float width )
1390 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1391 // Make sure the model is up-to-date before layouting
1392 ProcessModifyEvents();
1395 if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
1397 // Operations that can be done only once until the text changes.
1398 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1405 GET_GLYPH_METRICS );
1406 // Make sure the model is up-to-date before layouting
1407 mImpl->UpdateModel( onlyOnceOperations );
1410 // Layout the text for the new width.
1411 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1413 // Set the update info to relayout the whole text.
1414 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1415 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1417 // Store the actual control's width.
1418 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
1420 DoRelayout( Size( width, MAX_FLOAT ),
1421 static_cast<OperationsMask>( onlyOnceOperations |
1425 // Do not do again the only once operations.
1426 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1428 // Do the size related operations again.
1429 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1433 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1435 // Clear the update info. This info will be set the next time the text is updated.
1436 mImpl->mTextUpdateInfo.Clear();
1438 // Restore the actual control's width.
1439 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1441 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1445 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1446 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1449 return layoutSize.height;
1452 // public : Relayout.
1454 Controller::UpdateTextType Controller::Relayout( const Size& size )
1456 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", this, size.width, size.height, (mImpl->mAutoScrollEnabled)?"true":"false" );
1458 UpdateTextType updateTextType = NONE_UPDATED;
1460 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1462 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1464 mImpl->mVisualModel->mGlyphPositions.Clear();
1465 updateTextType = MODEL_UPDATED;
1468 // Clear the update info. This info will be set the next time the text is updated.
1469 mImpl->mTextUpdateInfo.Clear();
1471 // Not worth to relayout if width or height is equal to zero.
1472 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1474 return updateTextType;
1477 // Whether a new size has been set.
1478 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1482 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1484 // Layout operations that need to be done if the size changes.
1485 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1488 UPDATE_LAYOUT_SIZE |
1490 // Set the update info to relayout the whole text.
1491 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
1492 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1495 // Whether there are modify events.
1496 if( 0u != mImpl->mModifyEvents.Count() )
1498 // Style operations that need to be done if the text is modified.
1499 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1503 // Make sure the model is up-to-date before layouting.
1504 ProcessModifyEvents();
1505 bool updated = mImpl->UpdateModel( mImpl->mOperationsPending );
1509 updated = DoRelayout( size,
1510 mImpl->mOperationsPending,
1511 layoutSize ) || updated;
1515 updateTextType = MODEL_UPDATED;
1518 // Do not re-do any operation until something changes.
1519 mImpl->mOperationsPending = NO_OPERATION;
1521 // Whether the text control is editable
1522 const bool isEditable = NULL != mImpl->mEventData;
1524 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
1526 if( newSize && isEditable )
1528 offset = mImpl->mScrollPosition;
1531 if( !isEditable || !IsMultiLineEnabled() )
1533 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
1534 CalculateVerticalOffset( size );
1541 // If there is a new size, the scroll position needs to be clamped.
1542 mImpl->ClampHorizontalScroll( layoutSize );
1544 // Update the decorator's positions is needed if there is a new size.
1545 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mScrollPosition - offset );
1548 // Move the cursor, grab handle etc.
1549 if( mImpl->ProcessInputEvents() )
1551 updateTextType = static_cast<UpdateTextType>( updateTextType | DECORATOR_UPDATED );
1555 // Clear the update info. This info will be set the next time the text is updated.
1556 mImpl->mTextUpdateInfo.Clear();
1557 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1559 return updateTextType;
1562 // public : Input style change signals.
1564 bool Controller::IsInputStyleChangedSignalsQueueEmpty()
1566 return ( NULL == mImpl->mEventData ) || ( 0u == mImpl->mEventData->mInputStyleChangedQueue.Count() );
1569 void Controller::ProcessInputStyleChangedSignals()
1571 if( NULL == mImpl->mEventData )
1577 for( Vector<InputStyle::Mask>::ConstIterator it = mImpl->mEventData->mInputStyleChangedQueue.Begin(),
1578 endIt = mImpl->mEventData->mInputStyleChangedQueue.End();
1582 const InputStyle::Mask mask = *it;
1584 if( NULL != mImpl->mEditableControlInterface )
1586 // Emit the input style changed signal.
1587 mImpl->mEditableControlInterface->InputStyleChanged( mask );
1591 mImpl->mEventData->mInputStyleChangedQueue.Clear();
1594 // public : Text-input Event Queuing.
1596 void Controller::KeyboardFocusGainEvent()
1598 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1600 if( NULL != mImpl->mEventData )
1602 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1603 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1605 mImpl->ChangeState( EventData::EDITING );
1606 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1608 mImpl->NotifyImfMultiLineStatus();
1609 if( mImpl->IsShowingPlaceholderText() )
1611 // Show alternative placeholder-text when editing
1612 ShowPlaceholderText();
1615 mImpl->RequestRelayout();
1619 void Controller::KeyboardFocusLostEvent()
1621 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1623 if( NULL != mImpl->mEventData )
1625 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1627 mImpl->ChangeState( EventData::INACTIVE );
1629 if( !mImpl->IsShowingRealText() )
1631 // Revert to regular placeholder-text when not editing
1632 ShowPlaceholderText();
1636 mImpl->RequestRelayout();
1639 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1641 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1643 bool textChanged( false );
1645 if( ( NULL != mImpl->mEventData ) &&
1646 ( keyEvent.state == KeyEvent::Down ) )
1648 int keyCode = keyEvent.keyCode;
1649 const std::string& keyString = keyEvent.keyPressed;
1651 // Pre-process to separate modifying events from non-modifying input events.
1652 if( Dali::DALI_KEY_ESCAPE == keyCode )
1654 // Escape key is a special case which causes focus loss
1655 KeyboardFocusLostEvent();
1657 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
1658 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1659 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
1660 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
1662 Event event( Event::CURSOR_KEY_EVENT );
1663 event.p1.mInt = keyCode;
1664 mImpl->mEventData->mEventQueue.push_back( event );
1666 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1668 textChanged = BackspaceKeyEvent();
1670 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1672 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1673 // Avoids calling the InsertText() method which can delete selected text
1675 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1676 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1678 mImpl->ChangeState( EventData::INACTIVE );
1679 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1680 // Avoids calling the InsertText() method which can delete selected text
1682 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1684 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1685 // and a character is typed after the type of a upper case latin character.
1691 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1693 // IMF manager is no longer handling key-events
1694 mImpl->ClearPreEditFlag();
1696 InsertText( keyString, COMMIT );
1700 if ( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1701 ( mImpl->mEventData->mState != EventData::INACTIVE ) &&
1702 ( Dali::DALI_KEY_SHIFT_LEFT != keyCode ) )
1704 // Should not change the state if the key is the shift send by the imf manager.
1705 // Otherwise, when the state is SELECTING the text controller can't send the right
1706 // surrounding info to the imf.
1707 mImpl->ChangeState( EventData::EDITING );
1710 mImpl->RequestRelayout();
1714 ( NULL != mImpl->mEditableControlInterface ) )
1716 // Do this last since it provides callbacks into application code
1717 mImpl->mEditableControlInterface->TextChanged();
1723 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1725 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1727 if( NULL != mImpl->mEventData )
1729 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
1730 EventData::State state( mImpl->mEventData->mState );
1731 bool relayoutNeeded( false ); // to avoid unnecessary relayouts when tapping an empty text-field
1733 if( mImpl->IsClipboardVisible() )
1735 if( EventData::INACTIVE == state || EventData::EDITING == state)
1737 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
1739 relayoutNeeded = true;
1741 else if( 1u == tapCount )
1743 if( EventData::EDITING_WITH_POPUP == state || EventData::EDITING_WITH_PASTE_POPUP == state )
1745 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE ); // If Popup shown hide it here so can be shown again if required.
1748 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != state ) )
1750 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
1751 relayoutNeeded = true;
1755 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
1757 // Hide placeholder text
1761 if( EventData::INACTIVE == state )
1763 mImpl->ChangeState( EventData::EDITING );
1765 else if( !mImpl->IsClipboardEmpty() )
1767 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
1769 relayoutNeeded = true;
1772 else if( 2u == tapCount )
1774 if( mImpl->mEventData->mSelectionEnabled &&
1775 mImpl->IsShowingRealText() )
1777 SelectEvent( x, y, false );
1780 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
1781 if( relayoutNeeded )
1783 Event event( Event::TAP_EVENT );
1784 event.p1.mUint = tapCount;
1785 event.p2.mFloat = x;
1786 event.p3.mFloat = y;
1787 mImpl->mEventData->mEventQueue.push_back( event );
1789 mImpl->RequestRelayout();
1793 // Reset keyboard as tap event has occurred.
1794 mImpl->ResetImfManager();
1797 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
1799 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1801 if( NULL != mImpl->mEventData )
1803 Event event( Event::PAN_EVENT );
1804 event.p1.mInt = state;
1805 event.p2.mFloat = displacement.x;
1806 event.p3.mFloat = displacement.y;
1807 mImpl->mEventData->mEventQueue.push_back( event );
1809 mImpl->RequestRelayout();
1813 void Controller::LongPressEvent( Gesture::State state, float x, float y )
1815 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
1817 if( ( state == Gesture::Started ) &&
1818 ( NULL != mImpl->mEventData ) )
1820 if( !mImpl->IsShowingRealText() )
1822 Event event( Event::LONG_PRESS_EVENT );
1823 event.p1.mInt = state;
1824 mImpl->mEventData->mEventQueue.push_back( event );
1825 mImpl->RequestRelayout();
1829 // The 1st long-press on inactive text-field is treated as tap
1830 if( EventData::INACTIVE == mImpl->mEventData->mState )
1832 mImpl->ChangeState( EventData::EDITING );
1834 Event event( Event::TAP_EVENT );
1836 event.p2.mFloat = x;
1837 event.p3.mFloat = y;
1838 mImpl->mEventData->mEventQueue.push_back( event );
1840 mImpl->RequestRelayout();
1842 else if( !mImpl->IsClipboardVisible() )
1844 // Reset the imf manger to commit the pre-edit before selecting the text.
1845 mImpl->ResetImfManager();
1847 SelectEvent( x, y, false );
1853 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
1855 // Whether the text needs to be relaid-out.
1856 bool requestRelayout = false;
1858 // Whether to retrieve the text and cursor position to be sent to the IMF manager.
1859 bool retrieveText = false;
1860 bool retrieveCursor = false;
1862 switch( imfEvent.eventName )
1864 case ImfManager::COMMIT:
1866 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
1867 requestRelayout = true;
1868 retrieveCursor = true;
1871 case ImfManager::PREEDIT:
1873 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
1874 requestRelayout = true;
1875 retrieveCursor = true;
1878 case ImfManager::DELETESURROUNDING:
1880 const bool textDeleted = RemoveText( imfEvent.cursorOffset,
1881 imfEvent.numberOfChars,
1882 DONT_UPDATE_INPUT_STYLE );
1886 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
1887 !mImpl->IsPlaceholderAvailable() )
1889 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1893 ShowPlaceholderText();
1895 mImpl->mEventData->mUpdateCursorPosition = true;
1896 mImpl->mEventData->mScrollAfterDelete = true;
1898 requestRelayout = true;
1902 case ImfManager::GETSURROUNDING:
1904 retrieveText = true;
1905 retrieveCursor = true;
1908 case ImfManager::VOID:
1915 if( requestRelayout )
1917 mImpl->mOperationsPending = ALL_OPERATIONS;
1918 mImpl->RequestRelayout();
1922 CharacterIndex cursorPosition = 0u;
1923 Length numberOfWhiteSpaces = 0u;
1925 if( retrieveCursor )
1927 numberOfWhiteSpaces = mImpl->GetNumberOfWhiteSpaces( 0u );
1929 cursorPosition = mImpl->GetLogicalCursorPosition();
1931 if( cursorPosition < numberOfWhiteSpaces )
1933 cursorPosition = 0u;
1937 cursorPosition -= numberOfWhiteSpaces;
1943 mImpl->GetText( numberOfWhiteSpaces, text );
1946 ImfManager::ImfCallbackData callbackData( ( retrieveText || retrieveCursor ), cursorPosition, text, false );
1948 if( requestRelayout &&
1949 ( NULL != mImpl->mEditableControlInterface ) )
1951 // Do this last since it provides callbacks into application code
1952 mImpl->mEditableControlInterface->TextChanged();
1955 return callbackData;
1958 void Controller::PasteClipboardItemEvent()
1960 // Retrieve the clipboard contents first
1961 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
1962 std::string stringToPaste( notifier.GetContent() );
1964 // Commit the current pre-edit text; the contents of the clipboard should be appended
1965 mImpl->ResetImfManager();
1967 // Temporary disable hiding clipboard
1968 mImpl->SetClipboardHideEnable( false );
1971 PasteText( stringToPaste );
1973 mImpl->SetClipboardHideEnable( true );
1976 // protected : Inherit from Text::Decorator::ControllerInterface.
1978 void Controller::GetTargetSize( Vector2& targetSize )
1980 targetSize = mImpl->mVisualModel->mControlSize;
1983 void Controller::AddDecoration( Actor& actor, bool needsClipping )
1985 if( NULL != mImpl->mEditableControlInterface )
1987 mImpl->mEditableControlInterface->AddDecoration( actor, needsClipping );
1991 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
1993 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
1995 if( NULL != mImpl->mEventData )
1997 switch( handleType )
2001 Event event( Event::GRAB_HANDLE_EVENT );
2002 event.p1.mUint = state;
2003 event.p2.mFloat = x;
2004 event.p3.mFloat = y;
2006 mImpl->mEventData->mEventQueue.push_back( event );
2009 case LEFT_SELECTION_HANDLE:
2011 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2012 event.p1.mUint = state;
2013 event.p2.mFloat = x;
2014 event.p3.mFloat = y;
2016 mImpl->mEventData->mEventQueue.push_back( event );
2019 case RIGHT_SELECTION_HANDLE:
2021 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2022 event.p1.mUint = state;
2023 event.p2.mFloat = x;
2024 event.p3.mFloat = y;
2026 mImpl->mEventData->mEventQueue.push_back( event );
2029 case LEFT_SELECTION_HANDLE_MARKER:
2030 case RIGHT_SELECTION_HANDLE_MARKER:
2032 // Markers do not move the handles.
2035 case HANDLE_TYPE_COUNT:
2037 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2041 mImpl->RequestRelayout();
2045 // protected : Inherit from TextSelectionPopup::TextPopupButtonCallbackInterface.
2047 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2049 if( NULL == mImpl->mEventData )
2056 case Toolkit::TextSelectionPopup::CUT:
2058 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2059 mImpl->mOperationsPending = ALL_OPERATIONS;
2061 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2062 !mImpl->IsPlaceholderAvailable() )
2064 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2068 ShowPlaceholderText();
2071 mImpl->mEventData->mUpdateCursorPosition = true;
2072 mImpl->mEventData->mScrollAfterDelete = true;
2074 mImpl->RequestRelayout();
2076 if( NULL != mImpl->mEditableControlInterface )
2078 mImpl->mEditableControlInterface->TextChanged();
2082 case Toolkit::TextSelectionPopup::COPY:
2084 mImpl->SendSelectionToClipboard( false ); // Text not modified
2086 mImpl->mEventData->mUpdateCursorPosition = true;
2088 mImpl->RequestRelayout(); // Cursor, Handles, Selection Highlight, Popup
2091 case Toolkit::TextSelectionPopup::PASTE:
2093 mImpl->RequestGetTextFromClipboard(); // Request clipboard service to retrieve an item
2096 case Toolkit::TextSelectionPopup::SELECT:
2098 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2100 if( mImpl->mEventData->mSelectionEnabled )
2102 // Creates a SELECT event.
2103 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2107 case Toolkit::TextSelectionPopup::SELECT_ALL:
2109 // Creates a SELECT_ALL event
2110 SelectEvent( 0.f, 0.f, true );
2113 case Toolkit::TextSelectionPopup::CLIPBOARD:
2115 mImpl->ShowClipboard();
2118 case Toolkit::TextSelectionPopup::NONE:
2126 // private : Update.
2128 void Controller::InsertText( const std::string& text, Controller::InsertType type )
2130 bool removedPrevious( false );
2131 bool maxLengthReached( false );
2133 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
2135 if( NULL == mImpl->mEventData )
2140 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
2141 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
2142 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2144 // TODO: At the moment the underline runs are only for pre-edit.
2145 mImpl->mVisualModel->mUnderlineRuns.Clear();
2147 // Keep the current number of characters.
2148 const Length currentNumberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2150 // Remove the previous IMF pre-edit.
2151 if( mImpl->mEventData->mPreEditFlag && ( 0u != mImpl->mEventData->mPreEditLength ) )
2153 removedPrevious = RemoveText( -static_cast<int>( mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition ),
2154 mImpl->mEventData->mPreEditLength,
2155 DONT_UPDATE_INPUT_STYLE );
2157 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
2158 mImpl->mEventData->mPreEditLength = 0u;
2162 // Remove the previous Selection.
2163 removedPrevious = RemoveSelectedText();
2166 Vector<Character> utf32Characters;
2167 Length characterCount = 0u;
2171 // Convert text into UTF-32
2172 utf32Characters.Resize( text.size() );
2174 // This is a bit horrible but std::string returns a (signed) char*
2175 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
2177 // Transform a text array encoded in utf8 into an array encoded in utf32.
2178 // It returns the actual number of characters.
2179 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
2180 utf32Characters.Resize( characterCount );
2182 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
2183 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
2186 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
2188 // The placeholder text is no longer needed
2189 if( mImpl->IsShowingPlaceholderText() )
2194 mImpl->ChangeState( EventData::EDITING );
2196 // Handle the IMF (predicitive text) state changes
2197 if( COMMIT == type )
2199 // IMF manager is no longer handling key-events
2200 mImpl->ClearPreEditFlag();
2204 if( !mImpl->mEventData->mPreEditFlag )
2206 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state\n" );
2208 // Record the start of the pre-edit text
2209 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
2212 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
2213 mImpl->mEventData->mPreEditFlag = true;
2215 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2218 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
2220 // Restrict new text to fit within Maximum characters setting.
2221 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
2222 maxLengthReached = ( characterCount > maxSizeOfNewText );
2224 // The cursor position.
2225 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
2227 // Update the text's style.
2229 // Updates the text style runs by adding characters.
2230 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
2232 // Get the character index from the cursor index.
2233 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
2235 // Retrieve the text's style for the given index.
2237 mImpl->RetrieveDefaultInputStyle( style );
2238 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
2240 // Whether to add a new text color run.
2241 const bool addColorRun = ( style.textColor != mImpl->mEventData->mInputStyle.textColor );
2243 // Whether to add a new font run.
2244 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
2245 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
2246 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
2247 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
2248 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
2253 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
2254 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
2256 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
2257 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
2258 colorRun.characterRun.characterIndex = cursorIndex;
2259 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2262 if( addFontNameRun ||
2268 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
2269 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
2271 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
2273 if( addFontNameRun )
2275 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
2276 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
2277 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
2278 fontDescriptionRun.familyDefined = true;
2280 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
2283 if( addFontWeightRun )
2285 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
2286 fontDescriptionRun.weightDefined = true;
2289 if( addFontWidthRun )
2291 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
2292 fontDescriptionRun.widthDefined = true;
2295 if( addFontSlantRun )
2297 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
2298 fontDescriptionRun.slantDefined = true;
2301 if( addFontSizeRun )
2303 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
2304 fontDescriptionRun.sizeDefined = true;
2307 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
2308 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2311 // Insert at current cursor position.
2312 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
2314 if( cursorIndex < numberOfCharactersInModel )
2316 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2320 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2323 // Mark the first paragraph to be updated.
2324 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
2325 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
2327 // Update the cursor index.
2328 cursorIndex += maxSizeOfNewText;
2330 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
2333 const Length numberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2335 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
2336 mImpl->IsPlaceholderAvailable() )
2338 // Show place-holder if empty after removing the pre-edit text
2339 ShowPlaceholderText();
2340 mImpl->mEventData->mUpdateCursorPosition = true;
2341 mImpl->ClearPreEditFlag();
2343 else if( removedPrevious ||
2344 ( 0 != utf32Characters.Count() ) )
2346 // Queue an inserted event
2347 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
2349 mImpl->mEventData->mUpdateCursorPosition = true;
2350 if( numberOfCharacters < currentNumberOfCharacters )
2352 mImpl->mEventData->mScrollAfterDelete = true;
2356 mImpl->mEventData->mScrollAfterUpdatePosition = true;
2360 if( maxLengthReached )
2362 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
2364 mImpl->ResetImfManager();
2366 if( NULL != mImpl->mEditableControlInterface )
2368 // Do this last since it provides callbacks into application code
2369 mImpl->mEditableControlInterface->MaxLengthReached();
2374 void Controller::PasteText( const std::string& stringToPaste )
2376 InsertText( stringToPaste, Text::Controller::COMMIT );
2377 mImpl->ChangeState( EventData::EDITING );
2378 mImpl->RequestRelayout();
2380 if( NULL != mImpl->mEditableControlInterface )
2382 // Do this last since it provides callbacks into application code
2383 mImpl->mEditableControlInterface->TextChanged();
2387 bool Controller::RemoveText( int cursorOffset,
2388 int numberOfCharacters,
2389 UpdateInputStyleType type )
2391 bool removed = false;
2393 if( NULL == mImpl->mEventData )
2398 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfCharacters %d\n",
2399 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters );
2401 if( !mImpl->IsShowingPlaceholderText() )
2403 // Delete at current cursor position
2404 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
2405 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
2407 CharacterIndex cursorIndex = oldCursorIndex;
2409 // Validate the cursor position & number of characters
2410 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
2412 cursorIndex = oldCursorIndex + cursorOffset;
2415 if( ( cursorIndex + numberOfCharacters ) > currentText.Count() )
2417 numberOfCharacters = currentText.Count() - cursorIndex;
2420 if( ( cursorIndex + numberOfCharacters ) <= mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters )
2422 // Mark the paragraphs to be updated.
2423 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
2424 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove += numberOfCharacters;
2426 // Update the input style and remove the text's style before removing the text.
2428 if( UPDATE_INPUT_STYLE == type )
2430 // Keep a copy of the current input style.
2431 InputStyle currentInputStyle;
2432 currentInputStyle.Copy( mImpl->mEventData->mInputStyle );
2434 // Set first the default input style.
2435 mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
2437 // Update the input style.
2438 mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
2440 // Compare if the input style has changed.
2441 const bool hasInputStyleChanged = !currentInputStyle.Equal( mImpl->mEventData->mInputStyle );
2443 if( hasInputStyleChanged )
2445 const InputStyle::Mask styleChangedMask = currentInputStyle.GetInputStyleChangeMask( mImpl->mEventData->mInputStyle );
2446 // Queue the input style changed signal.
2447 mImpl->mEventData->mInputStyleChangedQueue.PushBack( styleChangedMask );
2451 // Updates the text style runs by removing characters. Runs with no characters are removed.
2452 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfCharacters );
2454 // Remove the characters.
2455 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
2456 Vector<Character>::Iterator last = first + numberOfCharacters;
2458 currentText.Erase( first, last );
2460 // Cursor position retreat
2461 oldCursorIndex = cursorIndex;
2463 mImpl->mEventData->mScrollAfterDelete = true;
2465 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfCharacters );
2473 bool Controller::RemoveSelectedText()
2475 bool textRemoved( false );
2477 if( EventData::SELECTING == mImpl->mEventData->mState )
2479 std::string removedString;
2480 mImpl->RetrieveSelection( removedString, true );
2482 if( !removedString.empty() )
2485 mImpl->ChangeState( EventData::EDITING );
2492 // private : Relayout.
2494 bool Controller::DoRelayout( const Size& size,
2495 OperationsMask operationsRequired,
2498 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
2499 bool viewUpdated( false );
2501 // Calculate the operations to be done.
2502 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
2504 const CharacterIndex startIndex = mImpl->mTextUpdateInfo.mParagraphCharacterIndex;
2505 const Length requestedNumberOfCharacters = mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters;
2507 // Get the current layout size.
2508 layoutSize = mImpl->mVisualModel->GetLayoutSize();
2510 if( NO_OPERATION != ( LAYOUT & operations ) )
2512 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
2514 // Some vectors with data needed to layout and reorder may be void
2515 // after the first time the text has been laid out.
2516 // Fill the vectors again.
2518 // Calculate the number of glyphs to layout.
2519 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
2520 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
2521 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
2522 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
2524 const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
2525 const GlyphIndex startGlyphIndex = mImpl->mTextUpdateInfo.mStartGlyphIndex;
2526 const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
2527 const Length totalNumberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
2529 if( 0u == totalNumberOfGlyphs )
2531 if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
2533 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
2536 // Nothing else to do if there is no glyphs.
2537 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
2541 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
2542 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
2543 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
2544 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
2545 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
2546 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
2547 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
2549 // Set the layout parameters.
2550 LayoutParameters layoutParameters( size,
2552 lineBreakInfo.Begin(),
2553 wordBreakInfo.Begin(),
2554 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
2556 glyphsToCharactersMap.Begin(),
2557 charactersPerGlyph.Begin(),
2558 charactersToGlyphBuffer,
2559 glyphsPerCharacterBuffer,
2560 totalNumberOfGlyphs );
2562 // Resize the vector of positions to have the same size than the vector of glyphs.
2563 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
2564 glyphPositions.Resize( totalNumberOfGlyphs );
2566 // Whether the last character is a new paragraph character.
2567 mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
2568 layoutParameters.isLastNewParagraph = mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph;
2570 // The initial glyph and the number of glyphs to layout.
2571 layoutParameters.startGlyphIndex = startGlyphIndex;
2572 layoutParameters.numberOfGlyphs = numberOfGlyphs;
2573 layoutParameters.startLineIndex = mImpl->mTextUpdateInfo.mStartLineIndex;
2574 layoutParameters.estimatedNumberOfLines = mImpl->mTextUpdateInfo.mEstimatedNumberOfLines;
2576 // Update the visual model.
2578 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
2580 mImpl->mVisualModel->mLines,
2583 viewUpdated = viewUpdated || ( newLayoutSize != layoutSize );
2587 layoutSize = newLayoutSize;
2589 if ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) )
2591 mImpl->mAutoScrollDirectionRTL = false;
2594 // Reorder the lines
2595 if( NO_OPERATION != ( REORDER & operations ) )
2597 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
2598 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
2600 // Check first if there are paragraphs with bidirectional info.
2601 if( 0u != bidirectionalInfo.Count() )
2604 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
2606 // Reorder the lines.
2607 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
2608 ReorderLines( bidirectionalInfo,
2610 requestedNumberOfCharacters,
2611 mImpl->mVisualModel->mLines,
2612 bidirectionalLineInfo );
2614 // Set the bidirectional info per line into the layout parameters.
2615 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
2616 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
2618 // Re-layout the text. Reorder those lines with right to left characters.
2619 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
2621 requestedNumberOfCharacters,
2624 if ( ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) ) && ( numberOfLines > 0 ) )
2626 const LineRun* const firstline = mImpl->mVisualModel->mLines.Begin();
2629 mImpl->mAutoScrollDirectionRTL = firstline->direction;
2635 // Sets the layout size.
2636 if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
2638 mImpl->mVisualModel->SetLayoutSize( layoutSize );
2642 // Store the size used to layout the text.
2643 mImpl->mVisualModel->mControlSize = size;
2646 if( NO_OPERATION != ( ALIGN & operations ) )
2648 // The laid-out lines.
2649 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
2651 mImpl->mLayoutEngine.Align( size,
2653 requestedNumberOfCharacters,
2658 #if defined(DEBUG_ENABLED)
2659 std::string currentText;
2660 GetText( currentText );
2661 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mAutoScrollDirectionRTL[%s] [%s]\n", this, (mImpl->mAutoScrollDirectionRTL)?"true":"false", currentText.c_str() );
2663 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
2667 void Controller::CalculateVerticalOffset( const Size& controlSize )
2669 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
2671 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
2673 // Get the line height of the default font.
2674 layoutSize.height = mImpl->GetDefaultFontLineHeight();
2677 switch( mImpl->mLayoutEngine.GetVerticalAlignment() )
2679 case LayoutEngine::VERTICAL_ALIGN_TOP:
2681 mImpl->mScrollPosition.y = 0.f;
2684 case LayoutEngine::VERTICAL_ALIGN_CENTER:
2686 mImpl->mScrollPosition.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
2689 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
2691 mImpl->mScrollPosition.y = controlSize.height - layoutSize.height;
2697 // private : Events.
2699 void Controller::ProcessModifyEvents()
2701 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
2703 if( 0u == events.Count() )
2709 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
2710 endIt = events.End();
2714 const ModifyEvent& event = *it;
2716 if( ModifyEvent::TEXT_REPLACED == event.type )
2718 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
2719 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
2721 TextReplacedEvent();
2723 else if( ModifyEvent::TEXT_INSERTED == event.type )
2725 TextInsertedEvent();
2727 else if( ModifyEvent::TEXT_DELETED == event.type )
2729 // Placeholder-text cannot be deleted
2730 if( !mImpl->IsShowingPlaceholderText() )
2737 if( NULL != mImpl->mEventData )
2739 // When the text is being modified, delay cursor blinking
2740 mImpl->mEventData->mDecorator->DelayCursorBlink();
2743 // Discard temporary text
2747 void Controller::TextReplacedEvent()
2749 // The natural size needs to be re-calculated.
2750 mImpl->mRecalculateNaturalSize = true;
2752 // Apply modifications to the model
2753 mImpl->mOperationsPending = ALL_OPERATIONS;
2756 void Controller::TextInsertedEvent()
2758 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
2760 if( NULL == mImpl->mEventData )
2765 // The natural size needs to be re-calculated.
2766 mImpl->mRecalculateNaturalSize = true;
2768 // Apply modifications to the model; TODO - Optimize this
2769 mImpl->mOperationsPending = ALL_OPERATIONS;
2772 void Controller::TextDeletedEvent()
2774 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
2776 if( NULL == mImpl->mEventData )
2781 // The natural size needs to be re-calculated.
2782 mImpl->mRecalculateNaturalSize = true;
2784 // Apply modifications to the model; TODO - Optimize this
2785 mImpl->mOperationsPending = ALL_OPERATIONS;
2788 void Controller::SelectEvent( float x, float y, bool selectAll )
2790 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2792 if( NULL != mImpl->mEventData )
2796 Event event( Event::SELECT_ALL );
2797 mImpl->mEventData->mEventQueue.push_back( event );
2801 Event event( Event::SELECT );
2802 event.p2.mFloat = x;
2803 event.p3.mFloat = y;
2804 mImpl->mEventData->mEventQueue.push_back( event );
2807 mImpl->RequestRelayout();
2811 bool Controller::BackspaceKeyEvent()
2813 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2815 bool removed = false;
2817 if( NULL == mImpl->mEventData )
2822 // IMF manager is no longer handling key-events
2823 mImpl->ClearPreEditFlag();
2825 if( EventData::SELECTING == mImpl->mEventData->mState )
2827 removed = RemoveSelectedText();
2829 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2831 // Remove the character before the current cursor position
2832 removed = RemoveText( -1,
2834 UPDATE_INPUT_STYLE );
2839 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2840 !mImpl->IsPlaceholderAvailable() )
2842 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2846 ShowPlaceholderText();
2848 mImpl->mEventData->mUpdateCursorPosition = true;
2849 mImpl->mEventData->mScrollAfterDelete = true;
2855 // private : Helpers.
2857 void Controller::ResetText()
2860 mImpl->mLogicalModel->mText.Clear();
2862 // We have cleared everything including the placeholder-text
2863 mImpl->PlaceholderCleared();
2865 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2866 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2867 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
2869 // Clear any previous text.
2870 mImpl->mTextUpdateInfo.mClearAll = true;
2872 // The natural size needs to be re-calculated.
2873 mImpl->mRecalculateNaturalSize = true;
2875 // Apply modifications to the model
2876 mImpl->mOperationsPending = ALL_OPERATIONS;
2879 void Controller::ShowPlaceholderText()
2881 if( mImpl->IsPlaceholderAvailable() )
2883 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2885 if( NULL == mImpl->mEventData )
2890 mImpl->mEventData->mIsShowingPlaceholderText = true;
2892 // Disable handles when showing place-holder text
2893 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2894 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2895 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2897 const char* text( NULL );
2900 // TODO - Switch placeholder text styles when changing state
2901 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2902 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2904 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2905 size = mImpl->mEventData->mPlaceholderTextActive.size();
2909 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2910 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2913 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2914 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2916 // Reset model for showing placeholder.
2917 mImpl->mLogicalModel->mText.Clear();
2918 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2920 // Convert text into UTF-32
2921 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2922 utf32Characters.Resize( size );
2924 // This is a bit horrible but std::string returns a (signed) char*
2925 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2927 // Transform a text array encoded in utf8 into an array encoded in utf32.
2928 // It returns the actual number of characters.
2929 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2930 utf32Characters.Resize( characterCount );
2932 // The characters to be added.
2933 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = characterCount;
2935 // Reset the cursor position
2936 mImpl->mEventData->mPrimaryCursorPosition = 0;
2938 // The natural size needs to be re-calculated.
2939 mImpl->mRecalculateNaturalSize = true;
2941 // Apply modifications to the model
2942 mImpl->mOperationsPending = ALL_OPERATIONS;
2944 // Update the rest of the model during size negotiation
2945 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2949 void Controller::ClearFontData()
2951 if( mImpl->mFontDefaults )
2953 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2956 // Set flags to update the model.
2957 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2958 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2959 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
2961 mImpl->mTextUpdateInfo.mClearAll = true;
2962 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
2963 mImpl->mRecalculateNaturalSize = true;
2965 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2970 UPDATE_LAYOUT_SIZE |
2975 void Controller::ClearStyleData()
2977 mImpl->mLogicalModel->mColorRuns.Clear();
2978 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2981 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
2983 // Reset the cursor position
2984 if( NULL != mImpl->mEventData )
2986 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
2988 // Update the cursor if it's in editing mode.
2989 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
2991 mImpl->mEventData->mUpdateCursorPosition = true;
2996 void Controller::ResetScrollPosition()
2998 if( NULL != mImpl->mEventData )
3000 // Reset the scroll position.
3001 mImpl->mScrollPosition = Vector2::ZERO;
3002 mImpl->mEventData->mScrollAfterUpdatePosition = true;
3006 // private : Private contructors & copy operator.
3008 Controller::Controller()
3011 mImpl = new Controller::Impl( NULL, NULL );
3014 Controller::Controller( ControlInterface* controlInterface )
3016 mImpl = new Controller::Impl( controlInterface, NULL );
3019 Controller::Controller( ControlInterface* controlInterface,
3020 EditableControlInterface* editableControlInterface )
3022 mImpl = new Controller::Impl( controlInterface,
3023 editableControlInterface );
3026 // The copy constructor and operator are left unimplemented.
3028 // protected : Destructor.
3030 Controller::~Controller()
3037 } // namespace Toolkit