2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/text/text-controller.h>
24 #include <dali/public-api/adaptor-framework/key.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/devel-api/adaptor-framework/clipboard-event-notifier.h>
29 #include <dali-toolkit/internal/text/bidirectional-support.h>
30 #include <dali-toolkit/internal/text/character-set-conversion.h>
31 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
32 #include <dali-toolkit/internal/text/markup-processor.h>
33 #include <dali-toolkit/internal/text/text-controller-impl.h>
38 #if defined(DEBUG_ENABLED)
39 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
42 const float MAX_FLOAT = std::numeric_limits<float>::max();
43 const unsigned int POINTS_PER_INCH = 72;
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;
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::SetText( const std::string& text )
137 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
139 // Reset keyboard as text changed
140 mImpl->ResetImfManager();
142 // Remove the previously set text and style.
148 CharacterIndex lastCursorIndex = 0u;
150 if( NULL != mImpl->mEventData )
152 // If popup shown then hide it by switching to Editing state
153 if( ( EventData::SELECTING == mImpl->mEventData->mState ) ||
154 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
155 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
156 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
158 mImpl->ChangeState( EventData::EDITING );
164 mImpl->mVisualModel->SetTextColor( mImpl->mTextColor );
166 MarkupProcessData markupProcessData( mImpl->mLogicalModel->mColorRuns,
167 mImpl->mLogicalModel->mFontDescriptionRuns );
169 Length textSize = 0u;
170 const uint8_t* utf8 = NULL;
171 if( mImpl->mMarkupProcessorEnabled )
173 ProcessMarkupString( text, markupProcessData );
174 textSize = markupProcessData.markupProcessedText.size();
176 // This is a bit horrible but std::string returns a (signed) char*
177 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
181 textSize = text.size();
183 // This is a bit horrible but std::string returns a (signed) char*
184 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
187 // Convert text into UTF-32
188 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
189 utf32Characters.Resize( textSize );
191 // Transform a text array encoded in utf8 into an array encoded in utf32.
192 // It returns the actual number of characters.
193 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
194 utf32Characters.Resize( characterCount );
196 DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
197 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mLogicalModel->mText.Count() );
199 // The characters to be added.
200 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
202 // To reset the cursor position
203 lastCursorIndex = characterCount;
205 // Update the rest of the model during size negotiation
206 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
208 // The natural size needs to be re-calculated.
209 mImpl->mRecalculateNaturalSize = true;
211 // Apply modifications to the model
212 mImpl->mOperationsPending = ALL_OPERATIONS;
216 ShowPlaceholderText();
219 // Resets the cursor position.
220 ResetCursorPosition( lastCursorIndex );
222 // Scrolls the text to make the cursor visible.
223 ResetScrollPosition();
225 mImpl->RequestRelayout();
227 if( NULL != mImpl->mEventData )
229 // Cancel previously queued events
230 mImpl->mEventData->mEventQueue.clear();
233 // Notify IMF as text changed
236 // Do this last since it provides callbacks into application code
237 mImpl->mControlInterface.TextChanged();
240 void Controller::GetText( std::string& text ) const
242 if( !mImpl->IsShowingPlaceholderText() )
244 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
246 if( 0u != utf32Characters.Count() )
248 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
253 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
257 unsigned int Controller::GetLogicalCursorPosition() const
259 if( NULL != mImpl->mEventData )
261 return mImpl->mEventData->mPrimaryCursorPosition;
267 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
269 if( NULL != mImpl->mEventData )
271 if( PLACEHOLDER_TYPE_INACTIVE == type )
273 mImpl->mEventData->mPlaceholderTextInactive = text;
277 mImpl->mEventData->mPlaceholderTextActive = text;
280 // Update placeholder if there is no text
281 if( mImpl->IsShowingPlaceholderText() ||
282 ( 0u == mImpl->mLogicalModel->mText.Count() ) )
284 ShowPlaceholderText();
289 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
291 if( NULL != mImpl->mEventData )
293 if( PLACEHOLDER_TYPE_INACTIVE == type )
295 text = mImpl->mEventData->mPlaceholderTextInactive;
299 text = mImpl->mEventData->mPlaceholderTextActive;
304 void Controller::SetMaximumNumberOfCharacters( Length maxCharacters )
306 mImpl->mMaximumNumberOfCharacters = maxCharacters;
309 int Controller::GetMaximumNumberOfCharacters()
311 return mImpl->mMaximumNumberOfCharacters;
314 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
316 if( NULL == mImpl->mFontDefaults )
318 mImpl->mFontDefaults = new FontDefaults();
321 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
322 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
323 mImpl->mFontDefaults->familyDefined = true;
325 // Clear the font-specific data
328 mImpl->RequestRelayout();
331 const std::string& Controller::GetDefaultFontFamily() const
333 if( NULL != mImpl->mFontDefaults )
335 return mImpl->mFontDefaults->mFontDescription.family;
341 void Controller::SetDefaultFontStyle( const std::string& style )
343 if( NULL == mImpl->mFontDefaults )
345 mImpl->mFontDefaults = new FontDefaults();
348 mImpl->mFontDefaults->mFontStyle = style;
351 const std::string& Controller::GetDefaultFontStyle() const
353 if( NULL != mImpl->mFontDefaults )
355 return mImpl->mFontDefaults->mFontStyle;
361 void Controller::SetDefaultFontWeight( FontWeight weight )
363 if( NULL == mImpl->mFontDefaults )
365 mImpl->mFontDefaults = new FontDefaults();
368 mImpl->mFontDefaults->mFontDescription.weight = weight;
369 mImpl->mFontDefaults->weightDefined = true;
371 // Clear the font-specific data
374 mImpl->RequestRelayout();
377 FontWeight Controller::GetDefaultFontWeight() const
379 if( NULL != mImpl->mFontDefaults )
381 return mImpl->mFontDefaults->mFontDescription.weight;
384 return TextAbstraction::FontWeight::NORMAL;
387 void Controller::SetDefaultFontWidth( FontWidth width )
389 if( NULL == mImpl->mFontDefaults )
391 mImpl->mFontDefaults = new FontDefaults();
394 mImpl->mFontDefaults->mFontDescription.width = width;
395 mImpl->mFontDefaults->widthDefined = true;
397 // Clear the font-specific data
400 mImpl->RequestRelayout();
403 FontWidth Controller::GetDefaultFontWidth() const
405 if( NULL != mImpl->mFontDefaults )
407 return mImpl->mFontDefaults->mFontDescription.width;
410 return TextAbstraction::FontWidth::NORMAL;
413 void Controller::SetDefaultFontSlant( FontSlant slant )
415 if( NULL == mImpl->mFontDefaults )
417 mImpl->mFontDefaults = new FontDefaults();
420 mImpl->mFontDefaults->mFontDescription.slant = slant;
421 mImpl->mFontDefaults->slantDefined = true;
423 // Clear the font-specific data
426 mImpl->RequestRelayout();
429 FontSlant Controller::GetDefaultFontSlant() const
431 if( NULL != mImpl->mFontDefaults )
433 return mImpl->mFontDefaults->mFontDescription.slant;
436 return TextAbstraction::FontSlant::NORMAL;
439 void Controller::SetDefaultPointSize( float pointSize )
441 if( NULL == mImpl->mFontDefaults )
443 mImpl->mFontDefaults = new FontDefaults();
446 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
447 mImpl->mFontDefaults->sizeDefined = true;
449 unsigned int horizontalDpi( 0u );
450 unsigned int verticalDpi( 0u );
451 mImpl->mFontClient.GetDpi( horizontalDpi, verticalDpi );
453 // Adjust the metrics if the fixed-size font should be down-scaled
454 int maxEmojiSize( pointSize/POINTS_PER_INCH * verticalDpi );
455 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultPointSize %p setting MaxEmojiSize %d\n", this, maxEmojiSize );
456 mImpl->mMetrics->SetMaxEmojiSize( maxEmojiSize );
458 // Clear the font-specific data
461 mImpl->RequestRelayout();
464 float Controller::GetDefaultPointSize() const
466 if( NULL != mImpl->mFontDefaults )
468 return mImpl->mFontDefaults->mDefaultPointSize;
474 void Controller::UpdateAfterFontChange( const std::string& newDefaultFont )
476 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange");
478 if( !mImpl->mFontDefaults->familyDefined ) // If user defined font then should not update when system font changes
480 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
481 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
485 mImpl->RequestRelayout();
489 void Controller::SetTextColor( const Vector4& textColor )
491 mImpl->mTextColor = textColor;
493 if( !mImpl->IsShowingPlaceholderText() )
495 mImpl->mVisualModel->SetTextColor( textColor );
497 mImpl->RequestRelayout();
501 const Vector4& Controller::GetTextColor() const
503 return mImpl->mTextColor;
506 bool Controller::RemoveText( int cursorOffset,
507 int numberOfCharacters,
508 UpdateInputStyleType type )
510 bool removed = false;
512 if( NULL == mImpl->mEventData )
517 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfCharacters %d\n",
518 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters );
520 if( !mImpl->IsShowingPlaceholderText() )
522 // Delete at current cursor position
523 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
524 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
526 CharacterIndex cursorIndex = oldCursorIndex;
528 // Validate the cursor position & number of characters
529 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
531 cursorIndex = oldCursorIndex + cursorOffset;
534 if( ( cursorIndex + numberOfCharacters ) > currentText.Count() )
536 numberOfCharacters = currentText.Count() - cursorIndex;
539 if( ( cursorIndex + numberOfCharacters ) <= mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters )
541 // Mark the paragraphs to be updated.
542 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
543 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove += numberOfCharacters;
545 // Update the input style and remove the text's style before removing the text.
547 if( UPDATE_INPUT_STYLE == type )
549 // Set first the default input style.
550 mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
552 // Update the input style.
553 mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
556 // Updates the text style runs by removing characters. Runs with no characters are removed.
557 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfCharacters );
559 // Remove the characters.
560 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
561 Vector<Character>::Iterator last = first + numberOfCharacters;
563 currentText.Erase( first, last );
565 // Cursor position retreat
566 oldCursorIndex = cursorIndex;
568 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfCharacters );
576 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
578 if( NULL != mImpl->mEventData )
580 mImpl->mEventData->mPlaceholderTextColor = textColor;
583 if( mImpl->IsShowingPlaceholderText() )
585 mImpl->mVisualModel->SetTextColor( textColor );
586 mImpl->RequestRelayout();
590 const Vector4& Controller::GetPlaceholderTextColor() const
592 if( NULL != mImpl->mEventData )
594 return mImpl->mEventData->mPlaceholderTextColor;
600 void Controller::SetShadowOffset( const Vector2& shadowOffset )
602 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
604 mImpl->RequestRelayout();
607 const Vector2& Controller::GetShadowOffset() const
609 return mImpl->mVisualModel->GetShadowOffset();
612 void Controller::SetShadowColor( const Vector4& shadowColor )
614 mImpl->mVisualModel->SetShadowColor( shadowColor );
616 mImpl->RequestRelayout();
619 const Vector4& Controller::GetShadowColor() const
621 return mImpl->mVisualModel->GetShadowColor();
624 void Controller::SetUnderlineColor( const Vector4& color )
626 mImpl->mVisualModel->SetUnderlineColor( color );
628 mImpl->RequestRelayout();
631 const Vector4& Controller::GetUnderlineColor() const
633 return mImpl->mVisualModel->GetUnderlineColor();
636 void Controller::SetUnderlineEnabled( bool enabled )
638 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
640 mImpl->RequestRelayout();
643 bool Controller::IsUnderlineEnabled() const
645 return mImpl->mVisualModel->IsUnderlineEnabled();
648 void Controller::SetUnderlineHeight( float height )
650 mImpl->mVisualModel->SetUnderlineHeight( height );
652 mImpl->RequestRelayout();
655 float Controller::GetUnderlineHeight() const
657 return mImpl->mVisualModel->GetUnderlineHeight();
660 void Controller::SetInputColor( const Vector4& color )
662 if( NULL != mImpl->mEventData )
664 mImpl->mEventData->mInputStyle.textColor = color;
665 mImpl->mEventData->mInputStyle.isDefaultColor = false;
667 if( EventData::SELECTING == mImpl->mEventData->mState )
669 const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
671 // Get start and end position of selection
672 const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
673 const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
675 // Add the color run.
676 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
677 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
679 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
680 colorRun.color = color;
681 colorRun.characterRun.characterIndex = startOfSelectedText;
682 colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
684 // Request to relayout.
685 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
686 mImpl->RequestRelayout();
688 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
689 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
690 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
695 const Vector4& Controller::GetInputColor() const
697 if( NULL != mImpl->mEventData )
699 return mImpl->mEventData->mInputStyle.textColor;
702 // Return the default text's color if there is no EventData.
703 return mImpl->mTextColor;
707 void Controller::SetInputFontFamily( const std::string& fontFamily )
709 if( NULL != mImpl->mEventData )
711 mImpl->mEventData->mInputStyle.familyName = fontFamily;
712 mImpl->mEventData->mInputStyle.familyDefined = true;
714 if( EventData::SELECTING == mImpl->mEventData->mState )
716 CharacterIndex startOfSelectedText = 0u;
717 Length lengthOfSelectedText = 0u;
718 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
719 mImpl->mLogicalModel,
721 lengthOfSelectedText );
723 fontDescriptionRun.familyLength = fontFamily.size();
724 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
725 memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
726 fontDescriptionRun.familyDefined = true;
728 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
730 // Request to relayout.
731 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
739 mImpl->mRecalculateNaturalSize = true;
740 mImpl->RequestRelayout();
742 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
743 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
744 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
746 // As the font changes, recalculate the handle positions is needed.
747 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
748 mImpl->mEventData->mUpdateRightSelectionPosition = true;
749 mImpl->mEventData->mScrollAfterUpdatePosition = true;
754 const std::string& Controller::GetInputFontFamily() const
756 if( NULL != mImpl->mEventData )
758 return mImpl->mEventData->mInputStyle.familyName;
761 // Return the default font's family if there is no EventData.
762 return GetDefaultFontFamily();
765 void Controller::SetInputFontStyle( const std::string& fontStyle )
767 if( NULL != mImpl->mEventData )
769 mImpl->mEventData->mInputStyle.fontStyle = fontStyle;
773 const std::string& Controller::GetInputFontStyle() const
775 if( NULL != mImpl->mEventData )
777 return mImpl->mEventData->mInputStyle.fontStyle;
780 // Return the default font's style if there is no EventData.
781 return GetDefaultFontStyle();
784 void Controller::SetInputFontWeight( FontWeight weight )
786 if( NULL != mImpl->mEventData )
788 mImpl->mEventData->mInputStyle.weight = weight;
789 mImpl->mEventData->mInputStyle.weightDefined = true;
791 if( EventData::SELECTING == mImpl->mEventData->mState )
793 CharacterIndex startOfSelectedText = 0u;
794 Length lengthOfSelectedText = 0u;
795 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
796 mImpl->mLogicalModel,
798 lengthOfSelectedText );
800 fontDescriptionRun.weight = weight;
801 fontDescriptionRun.weightDefined = true;
803 // Request to relayout.
804 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
812 mImpl->mRecalculateNaturalSize = true;
813 mImpl->RequestRelayout();
815 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
816 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
817 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
819 // As the font might change, recalculate the handle positions is needed.
820 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
821 mImpl->mEventData->mUpdateRightSelectionPosition = true;
822 mImpl->mEventData->mScrollAfterUpdatePosition = true;
827 FontWeight Controller::GetInputFontWeight() const
829 if( NULL != mImpl->mEventData )
831 return mImpl->mEventData->mInputStyle.weight;
834 return GetDefaultFontWeight();
837 void Controller::SetInputFontWidth( FontWidth width )
839 if( NULL != mImpl->mEventData )
841 mImpl->mEventData->mInputStyle.width = width;
842 mImpl->mEventData->mInputStyle.widthDefined = true;
844 if( EventData::SELECTING == mImpl->mEventData->mState )
846 CharacterIndex startOfSelectedText = 0u;
847 Length lengthOfSelectedText = 0u;
848 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
849 mImpl->mLogicalModel,
851 lengthOfSelectedText );
853 fontDescriptionRun.width = width;
854 fontDescriptionRun.widthDefined = true;
856 // Request to relayout.
857 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
865 mImpl->mRecalculateNaturalSize = true;
866 mImpl->RequestRelayout();
868 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
869 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
870 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
872 // As the font might change, recalculate the handle positions is needed.
873 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
874 mImpl->mEventData->mUpdateRightSelectionPosition = true;
875 mImpl->mEventData->mScrollAfterUpdatePosition = true;
880 FontWidth Controller::GetInputFontWidth() const
882 if( NULL != mImpl->mEventData )
884 return mImpl->mEventData->mInputStyle.width;
887 return GetDefaultFontWidth();
890 void Controller::SetInputFontSlant( FontSlant slant )
892 if( NULL != mImpl->mEventData )
894 mImpl->mEventData->mInputStyle.slant = slant;
895 mImpl->mEventData->mInputStyle.slantDefined = true;
897 if( EventData::SELECTING == mImpl->mEventData->mState )
899 CharacterIndex startOfSelectedText = 0u;
900 Length lengthOfSelectedText = 0u;
901 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
902 mImpl->mLogicalModel,
904 lengthOfSelectedText );
906 fontDescriptionRun.slant = slant;
907 fontDescriptionRun.slantDefined = true;
909 // Request to relayout.
910 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
918 mImpl->mRecalculateNaturalSize = true;
919 mImpl->RequestRelayout();
921 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
922 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
923 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
925 // As the font might change, recalculate the handle positions is needed.
926 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
927 mImpl->mEventData->mUpdateRightSelectionPosition = true;
928 mImpl->mEventData->mScrollAfterUpdatePosition = true;
933 FontSlant Controller::GetInputFontSlant() const
935 if( NULL != mImpl->mEventData )
937 return mImpl->mEventData->mInputStyle.slant;
940 return GetDefaultFontSlant();
943 void Controller::SetInputFontPointSize( float size )
945 if( NULL != mImpl->mEventData )
947 mImpl->mEventData->mInputStyle.size = size;
949 if( EventData::SELECTING == mImpl->mEventData->mState )
951 CharacterIndex startOfSelectedText = 0u;
952 Length lengthOfSelectedText = 0u;
953 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
954 mImpl->mLogicalModel,
956 lengthOfSelectedText );
958 fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
959 fontDescriptionRun.sizeDefined = true;
961 // Request to relayout.
962 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
970 mImpl->mRecalculateNaturalSize = true;
971 mImpl->RequestRelayout();
973 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
974 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
975 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
977 // As the font might change, recalculate the handle positions is needed.
978 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
979 mImpl->mEventData->mUpdateRightSelectionPosition = true;
980 mImpl->mEventData->mScrollAfterUpdatePosition = true;
985 float Controller::GetInputFontPointSize() const
987 if( NULL != mImpl->mEventData )
989 return mImpl->mEventData->mInputStyle.size;
992 // Return the default font's point size if there is no EventData.
993 return GetDefaultPointSize();
996 void Controller::SetEnableCursorBlink( bool enable )
998 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
1000 if( NULL != mImpl->mEventData )
1002 mImpl->mEventData->mCursorBlinkEnabled = enable;
1005 mImpl->mEventData->mDecorator )
1007 mImpl->mEventData->mDecorator->StopCursorBlink();
1012 bool Controller::GetEnableCursorBlink() const
1014 if( NULL != mImpl->mEventData )
1016 return mImpl->mEventData->mCursorBlinkEnabled;
1022 const Vector2& Controller::GetScrollPosition() const
1024 if( NULL != mImpl->mEventData )
1026 return mImpl->mEventData->mScrollPosition;
1029 return Vector2::ZERO;
1032 const Vector2& Controller::GetAlignmentOffset() const
1034 return mImpl->mAlignmentOffset;
1037 Vector3 Controller::GetNaturalSize()
1039 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
1040 Vector3 naturalSize;
1042 // Make sure the model is up-to-date before layouting
1043 ProcessModifyEvents();
1045 if( mImpl->mRecalculateNaturalSize )
1047 // Operations that can be done only once until the text changes.
1048 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1055 GET_GLYPH_METRICS );
1056 // Make sure the model is up-to-date before layouting
1057 mImpl->UpdateModel( onlyOnceOperations );
1059 // Layout the text for the new width.
1060 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1062 // Set the update info to relayout the whole text.
1063 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1064 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1066 // Store the actual control's width.
1067 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
1069 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
1070 static_cast<OperationsMask>( onlyOnceOperations |
1072 naturalSize.GetVectorXY() );
1074 // Do not do again the only once operations.
1075 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1077 // Do the size related operations again.
1078 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1081 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1083 // Stores the natural size to avoid recalculate it again
1084 // unless the text/style changes.
1085 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
1087 mImpl->mRecalculateNaturalSize = false;
1089 // Clear the update info. This info will be set the next time the text is updated.
1090 mImpl->mTextUpdateInfo.Clear();
1092 // Restore the actual control's width.
1093 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1095 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1099 naturalSize = mImpl->mVisualModel->GetNaturalSize();
1101 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1104 naturalSize.x = ConvertToEven( naturalSize.x );
1105 naturalSize.y = ConvertToEven( naturalSize.y );
1110 float Controller::GetHeightForWidth( float width )
1112 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1113 // Make sure the model is up-to-date before layouting
1114 ProcessModifyEvents();
1117 if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
1119 // Operations that can be done only once until the text changes.
1120 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1127 GET_GLYPH_METRICS );
1128 // Make sure the model is up-to-date before layouting
1129 mImpl->UpdateModel( onlyOnceOperations );
1132 // Layout the text for the new width.
1133 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1135 // Set the update info to relayout the whole text.
1136 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1137 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1139 // Store the actual control's width.
1140 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
1142 DoRelayout( Size( width, MAX_FLOAT ),
1143 static_cast<OperationsMask>( onlyOnceOperations |
1147 // Do not do again the only once operations.
1148 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1150 // Do the size related operations again.
1151 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1155 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1157 // Clear the update info. This info will be set the next time the text is updated.
1158 mImpl->mTextUpdateInfo.Clear();
1160 // Restore the actual control's width.
1161 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1163 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1167 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1168 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1171 return layoutSize.height;
1174 bool Controller::Relayout( const Size& size )
1176 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
1178 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1180 bool glyphsRemoved( false );
1181 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1183 mImpl->mVisualModel->mGlyphPositions.Clear();
1184 glyphsRemoved = true;
1187 // Clear the update info. This info will be set the next time the text is updated.
1188 mImpl->mTextUpdateInfo.Clear();
1190 // Not worth to relayout if width or height is equal to zero.
1191 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1193 return glyphsRemoved;
1196 // Whether a new size has been set.
1197 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1201 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1203 // Layout operations that need to be done if the size changes.
1204 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1207 UPDATE_ACTUAL_SIZE |
1209 // Set the update info to relayout the whole text.
1210 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
1211 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1214 // Whether there are modify events.
1215 if( 0u != mImpl->mModifyEvents.Count() )
1217 // Style operations that need to be done if the text is modified.
1218 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1222 // Make sure the model is up-to-date before layouting.
1223 ProcessModifyEvents();
1224 bool updated = mImpl->UpdateModel( mImpl->mOperationsPending );
1228 updated = DoRelayout( size,
1229 mImpl->mOperationsPending,
1230 layoutSize ) || updated;
1232 // Do not re-do any operation until something changes.
1233 mImpl->mOperationsPending = NO_OPERATION;
1235 // Whether the text control is editable
1236 const bool isEditable = NULL != mImpl->mEventData;
1238 // Keep the current offset and alignment as it will be used to update the decorator's positions (if the size changes).
1240 if( newSize && isEditable )
1242 offset = mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition;
1245 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
1246 CalculateTextAlignment( size );
1252 // If there is a new size, the scroll position needs to be clamped.
1253 mImpl->ClampHorizontalScroll( layoutSize );
1255 // Update the decorator's positions is needed if there is a new size.
1256 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
1259 // Move the cursor, grab handle etc.
1260 updated = mImpl->ProcessInputEvents() || updated;
1263 // Clear the update info. This info will be set the next time the text is updated.
1264 mImpl->mTextUpdateInfo.Clear();
1265 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1270 void Controller::ProcessModifyEvents()
1272 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
1274 if( 0u == events.Count() )
1280 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
1281 endIt = events.End();
1285 const ModifyEvent& event = *it;
1287 if( ModifyEvent::TEXT_REPLACED == event.type )
1289 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
1290 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
1292 TextReplacedEvent();
1294 else if( ModifyEvent::TEXT_INSERTED == event.type )
1296 TextInsertedEvent();
1298 else if( ModifyEvent::TEXT_DELETED == event.type )
1300 // Placeholder-text cannot be deleted
1301 if( !mImpl->IsShowingPlaceholderText() )
1308 if( NULL != mImpl->mEventData )
1310 // When the text is being modified, delay cursor blinking
1311 mImpl->mEventData->mDecorator->DelayCursorBlink();
1314 // Discard temporary text
1318 void Controller::ResetText()
1321 mImpl->mLogicalModel->mText.Clear();
1323 // We have cleared everything including the placeholder-text
1324 mImpl->PlaceholderCleared();
1326 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1327 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1328 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
1330 // Clear any previous text.
1331 mImpl->mTextUpdateInfo.mClearAll = true;
1333 // The natural size needs to be re-calculated.
1334 mImpl->mRecalculateNaturalSize = true;
1336 // Apply modifications to the model
1337 mImpl->mOperationsPending = ALL_OPERATIONS;
1340 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
1342 // Reset the cursor position
1343 if( NULL != mImpl->mEventData )
1345 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
1347 // Update the cursor if it's in editing mode.
1348 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1350 mImpl->mEventData->mUpdateCursorPosition = true;
1355 void Controller::ResetScrollPosition()
1357 if( NULL != mImpl->mEventData )
1359 // Reset the scroll position.
1360 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
1361 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1365 void Controller::TextReplacedEvent()
1367 // The natural size needs to be re-calculated.
1368 mImpl->mRecalculateNaturalSize = true;
1370 // Apply modifications to the model
1371 mImpl->mOperationsPending = ALL_OPERATIONS;
1374 void Controller::TextInsertedEvent()
1376 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
1378 if( NULL == mImpl->mEventData )
1383 // The natural size needs to be re-calculated.
1384 mImpl->mRecalculateNaturalSize = true;
1386 // Apply modifications to the model; TODO - Optimize this
1387 mImpl->mOperationsPending = ALL_OPERATIONS;
1389 // Queue a cursor reposition event; this must wait until after DoRelayout()
1390 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1392 mImpl->mEventData->mUpdateCursorPosition = true;
1393 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1397 void Controller::TextDeletedEvent()
1399 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
1401 if( NULL == mImpl->mEventData )
1406 // The natural size needs to be re-calculated.
1407 mImpl->mRecalculateNaturalSize = true;
1409 // Apply modifications to the model; TODO - Optimize this
1410 mImpl->mOperationsPending = ALL_OPERATIONS;
1412 // Queue a cursor reposition event; this must wait until after DoRelayout()
1413 mImpl->mEventData->mUpdateCursorPosition = true;
1414 if( 0u != mImpl->mLogicalModel->mText.Count() )
1416 mImpl->mEventData->mScrollAfterDelete = true;
1420 bool Controller::DoRelayout( const Size& size,
1421 OperationsMask operationsRequired,
1424 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
1425 bool viewUpdated( false );
1427 // Calculate the operations to be done.
1428 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
1430 const CharacterIndex startIndex = mImpl->mTextUpdateInfo.mParagraphCharacterIndex;
1431 const Length requestedNumberOfCharacters = mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters;
1433 if( NO_OPERATION != ( LAYOUT & operations ) )
1435 // Some vectors with data needed to layout and reorder may be void
1436 // after the first time the text has been laid out.
1437 // Fill the vectors again.
1439 // Calculate the number of glyphs to layout.
1440 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1441 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1442 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
1443 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
1445 const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
1446 const GlyphIndex startGlyphIndex = mImpl->mTextUpdateInfo.mStartGlyphIndex;
1447 const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
1448 const Length totalNumberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
1450 if( 0u == totalNumberOfGlyphs )
1452 if( NO_OPERATION != ( UPDATE_ACTUAL_SIZE & operations ) )
1454 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
1457 // Nothing else to do if there is no glyphs.
1458 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1462 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1463 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1464 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1465 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1466 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1467 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1468 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1470 // Set the layout parameters.
1471 LayoutParameters layoutParameters( size,
1473 lineBreakInfo.Begin(),
1474 wordBreakInfo.Begin(),
1475 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1477 glyphsToCharactersMap.Begin(),
1478 charactersPerGlyph.Begin(),
1479 charactersToGlyphBuffer,
1480 glyphsPerCharacterBuffer,
1481 totalNumberOfGlyphs );
1483 // Resize the vector of positions to have the same size than the vector of glyphs.
1484 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1485 glyphPositions.Resize( totalNumberOfGlyphs );
1487 // Whether the last character is a new paragraph character.
1488 mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1489 layoutParameters.isLastNewParagraph = mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph;
1491 // The initial glyph and the number of glyphs to layout.
1492 layoutParameters.startGlyphIndex = startGlyphIndex;
1493 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1494 layoutParameters.startLineIndex = mImpl->mTextUpdateInfo.mStartLineIndex;
1495 layoutParameters.estimatedNumberOfLines = mImpl->mTextUpdateInfo.mEstimatedNumberOfLines;
1497 // Update the visual model.
1498 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1500 mImpl->mVisualModel->mLines,
1505 // Reorder the lines
1506 if( NO_OPERATION != ( REORDER & operations ) )
1508 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1509 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1511 // Check first if there are paragraphs with bidirectional info.
1512 if( 0u != bidirectionalInfo.Count() )
1515 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1517 // Reorder the lines.
1518 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1519 ReorderLines( bidirectionalInfo,
1521 requestedNumberOfCharacters,
1522 mImpl->mVisualModel->mLines,
1523 bidirectionalLineInfo );
1525 // Set the bidirectional info per line into the layout parameters.
1526 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1527 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1529 // Re-layout the text. Reorder those lines with right to left characters.
1530 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1532 requestedNumberOfCharacters,
1538 // Sets the actual size.
1539 if( NO_OPERATION != ( UPDATE_ACTUAL_SIZE & operations ) )
1541 mImpl->mVisualModel->SetLayoutSize( layoutSize );
1545 // Store the size used to layout the text.
1546 mImpl->mVisualModel->mControlSize = size;
1550 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1553 if( NO_OPERATION != ( ALIGN & operations ) )
1555 // The laid-out lines.
1556 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1558 mImpl->mLayoutEngine.Align( size,
1560 requestedNumberOfCharacters,
1566 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1570 void Controller::SetMultiLineEnabled( bool enable )
1572 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1574 if( layout != mImpl->mLayoutEngine.GetLayout() )
1576 // Set the layout type.
1577 mImpl->mLayoutEngine.SetLayout( layout );
1579 // Set the flags to redo the layout operations
1580 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1581 UPDATE_ACTUAL_SIZE |
1585 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1587 mImpl->RequestRelayout();
1591 bool Controller::IsMultiLineEnabled() const
1593 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1596 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1598 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1600 // Set the alignment.
1601 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1603 // Set the flag to redo the alignment operation.
1604 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1606 mImpl->RequestRelayout();
1610 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1612 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1615 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1617 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1619 // Set the alignment.
1620 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1622 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1624 mImpl->RequestRelayout();
1628 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1630 return mImpl->mLayoutEngine.GetVerticalAlignment();
1633 void Controller::CalculateTextAlignment( const Size& controlSize )
1635 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1637 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1639 // Get the line height of the default font.
1640 layoutSize.height = mImpl->GetDefaultFontLineHeight();
1643 if( LayoutEngine::SINGLE_LINE_BOX == mImpl->mLayoutEngine.GetLayout() )
1645 // Get the direction of the first character.
1646 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1648 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1649 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1650 if( firstParagraphDirection )
1652 switch( horizontalAlignment )
1654 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1656 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1659 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1664 case LayoutEngine::HORIZONTAL_ALIGN_END:
1666 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1672 switch( horizontalAlignment )
1674 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1676 mImpl->mAlignmentOffset.x = 0.f;
1679 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1681 mImpl->mAlignmentOffset.x = floorf( 0.5f * ( controlSize.width - layoutSize.width ) ); // try to avoid pixel alignment.
1684 case LayoutEngine::HORIZONTAL_ALIGN_END:
1686 mImpl->mAlignmentOffset.x = controlSize.width - layoutSize.width;
1692 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1693 switch( verticalAlignment )
1695 case LayoutEngine::VERTICAL_ALIGN_TOP:
1697 mImpl->mAlignmentOffset.y = 0.f;
1700 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1702 mImpl->mAlignmentOffset.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
1705 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1707 mImpl->mAlignmentOffset.y = controlSize.height - layoutSize.height;
1713 LayoutEngine& Controller::GetLayoutEngine()
1715 return mImpl->mLayoutEngine;
1718 View& Controller::GetView()
1720 return mImpl->mView;
1723 void Controller::KeyboardFocusGainEvent()
1725 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1727 if( NULL != mImpl->mEventData )
1729 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1730 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1732 mImpl->ChangeState( EventData::EDITING );
1733 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1736 if( mImpl->IsShowingPlaceholderText() )
1738 // Show alternative placeholder-text when editing
1739 ShowPlaceholderText();
1742 mImpl->RequestRelayout();
1746 void Controller::KeyboardFocusLostEvent()
1748 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1750 if( NULL != mImpl->mEventData )
1752 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1754 mImpl->ChangeState( EventData::INACTIVE );
1756 if( !mImpl->IsShowingRealText() )
1758 // Revert to regular placeholder-text when not editing
1759 ShowPlaceholderText();
1763 mImpl->RequestRelayout();
1766 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1768 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1770 bool textChanged( false );
1772 if( ( NULL != mImpl->mEventData ) &&
1773 ( keyEvent.state == KeyEvent::Down ) )
1775 int keyCode = keyEvent.keyCode;
1776 const std::string& keyString = keyEvent.keyPressed;
1778 // Pre-process to separate modifying events from non-modifying input events.
1779 if( Dali::DALI_KEY_ESCAPE == keyCode )
1781 // Escape key is a special case which causes focus loss
1782 KeyboardFocusLostEvent();
1784 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
1785 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1786 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
1787 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
1789 Event event( Event::CURSOR_KEY_EVENT );
1790 event.p1.mInt = keyCode;
1791 mImpl->mEventData->mEventQueue.push_back( event );
1793 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1795 textChanged = BackspaceKeyEvent();
1797 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1799 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1800 // Avoids calling the InsertText() method which can delete selected text
1802 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1803 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1805 mImpl->ChangeState( EventData::INACTIVE );
1806 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1807 // Avoids calling the InsertText() method which can delete selected text
1809 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1811 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1812 // and a character is typed after the type of a upper case latin character.
1818 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1820 // IMF manager is no longer handling key-events
1821 mImpl->ClearPreEditFlag();
1823 InsertText( keyString, COMMIT );
1827 if( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1828 ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1830 mImpl->ChangeState( EventData::EDITING );
1833 mImpl->RequestRelayout();
1838 // Do this last since it provides callbacks into application code
1839 mImpl->mControlInterface.TextChanged();
1845 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1847 bool removedPrevious( false );
1848 bool maxLengthReached( false );
1850 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1852 if( NULL == mImpl->mEventData )
1857 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1858 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1859 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1861 // TODO: At the moment the underline runs are only for pre-edit.
1862 mImpl->mVisualModel->mUnderlineRuns.Clear();
1864 Vector<Character> utf32Characters;
1865 Length characterCount( 0u );
1867 // Remove the previous IMF pre-edit (predicitive text)
1868 if( mImpl->mEventData->mPreEditFlag &&
1869 ( 0u != mImpl->mEventData->mPreEditLength ) )
1871 const CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1873 removedPrevious = RemoveText( -static_cast<int>( offset ),
1874 mImpl->mEventData->mPreEditLength,
1875 DONT_UPDATE_INPUT_STYLE );
1877 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1878 mImpl->mEventData->mPreEditLength = 0u;
1882 // Remove the previous Selection
1883 removedPrevious = RemoveSelectedText();
1888 // Convert text into UTF-32
1889 utf32Characters.Resize( text.size() );
1891 // This is a bit horrible but std::string returns a (signed) char*
1892 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1894 // Transform a text array encoded in utf8 into an array encoded in utf32.
1895 // It returns the actual number of characters.
1896 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1897 utf32Characters.Resize( characterCount );
1899 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1900 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1903 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1905 // The placeholder text is no longer needed
1906 if( mImpl->IsShowingPlaceholderText() )
1911 mImpl->ChangeState( EventData::EDITING );
1913 // Handle the IMF (predicitive text) state changes
1914 if( COMMIT == type )
1916 // IMF manager is no longer handling key-events
1917 mImpl->ClearPreEditFlag();
1921 if( !mImpl->mEventData->mPreEditFlag )
1923 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1925 // Record the start of the pre-edit text
1926 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1929 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1930 mImpl->mEventData->mPreEditFlag = true;
1932 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1935 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1937 // Restrict new text to fit within Maximum characters setting.
1938 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1939 maxLengthReached = ( characterCount > maxSizeOfNewText );
1941 // The cursor position.
1942 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1944 // Update the text's style.
1946 // Updates the text style runs by adding characters.
1947 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
1949 // Get the character index from the cursor index.
1950 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
1952 // Retrieve the text's style for the given index.
1954 mImpl->RetrieveDefaultInputStyle( style );
1955 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
1957 // Whether to add a new text color run.
1958 const bool addColorRun = ( style.textColor != mImpl->mEventData->mInputStyle.textColor );
1960 // Whether to add a new font run.
1961 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
1962 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
1963 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
1964 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
1965 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
1970 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
1971 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
1973 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
1974 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
1975 colorRun.characterRun.characterIndex = cursorIndex;
1976 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1979 if( addFontNameRun ||
1985 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
1986 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
1988 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
1990 if( addFontNameRun )
1992 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
1993 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
1994 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
1995 fontDescriptionRun.familyDefined = true;
1997 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
2000 if( addFontWeightRun )
2002 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
2003 fontDescriptionRun.weightDefined = true;
2006 if( addFontWidthRun )
2008 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
2009 fontDescriptionRun.widthDefined = true;
2012 if( addFontSlantRun )
2014 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
2015 fontDescriptionRun.slantDefined = true;
2018 if( addFontSizeRun )
2020 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
2021 fontDescriptionRun.sizeDefined = true;
2024 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
2025 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2028 // Insert at current cursor position.
2029 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
2031 if( cursorIndex < numberOfCharactersInModel )
2033 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2037 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2040 // Mark the first paragraph to be updated.
2041 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
2042 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
2044 // Update the cursor index.
2045 cursorIndex += maxSizeOfNewText;
2047 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
2050 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
2051 mImpl->IsPlaceholderAvailable() )
2053 // Show place-holder if empty after removing the pre-edit text
2054 ShowPlaceholderText();
2055 mImpl->mEventData->mUpdateCursorPosition = true;
2056 mImpl->ClearPreEditFlag();
2058 else if( removedPrevious ||
2059 ( 0 != utf32Characters.Count() ) )
2061 // Queue an inserted event
2062 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
2065 if( maxLengthReached )
2067 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
2069 mImpl->ResetImfManager();
2071 // Do this last since it provides callbacks into application code
2072 mImpl->mControlInterface.MaxLengthReached();
2076 bool Controller::RemoveSelectedText()
2078 bool textRemoved( false );
2080 if( EventData::SELECTING == mImpl->mEventData->mState )
2082 std::string removedString;
2083 mImpl->RetrieveSelection( removedString, true );
2085 if( !removedString.empty() )
2088 mImpl->ChangeState( EventData::EDITING );
2095 void Controller::TapEvent( unsigned int tapCount, float x, float y )
2097 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
2099 if( NULL != mImpl->mEventData )
2101 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
2103 if( 1u == tapCount )
2105 // This is to avoid unnecessary relayouts when tapping an empty text-field
2106 bool relayoutNeeded( false );
2108 if( ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
2109 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
2111 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE ); // If Popup shown hide it here so can be shown again if required.
2114 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
2116 // Already in an active state so show a popup
2117 if( !mImpl->IsClipboardEmpty() )
2119 // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
2120 mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
2124 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2126 relayoutNeeded = true;
2130 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2132 // Hide placeholder text
2136 if( EventData::INACTIVE == mImpl->mEventData->mState )
2138 mImpl->ChangeState( EventData::EDITING );
2140 else if( !mImpl->IsClipboardEmpty() )
2142 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2144 relayoutNeeded = true;
2147 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2148 if( relayoutNeeded )
2150 Event event( Event::TAP_EVENT );
2151 event.p1.mUint = tapCount;
2152 event.p2.mFloat = x;
2153 event.p3.mFloat = y;
2154 mImpl->mEventData->mEventQueue.push_back( event );
2156 mImpl->RequestRelayout();
2159 else if( 2u == tapCount )
2161 if( mImpl->mEventData->mSelectionEnabled &&
2162 mImpl->IsShowingRealText() )
2164 SelectEvent( x, y, false );
2169 // Reset keyboard as tap event has occurred.
2170 mImpl->ResetImfManager();
2173 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2174 // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2176 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2178 if( NULL != mImpl->mEventData )
2180 Event event( Event::PAN_EVENT );
2181 event.p1.mInt = state;
2182 event.p2.mFloat = displacement.x;
2183 event.p3.mFloat = displacement.y;
2184 mImpl->mEventData->mEventQueue.push_back( event );
2186 mImpl->RequestRelayout();
2190 void Controller::LongPressEvent( Gesture::State state, float x, float y )
2192 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2194 if( ( state == Gesture::Started ) &&
2195 ( NULL != mImpl->mEventData ) )
2197 if( !mImpl->IsShowingRealText() )
2199 Event event( Event::LONG_PRESS_EVENT );
2200 event.p1.mInt = state;
2201 mImpl->mEventData->mEventQueue.push_back( event );
2202 mImpl->RequestRelayout();
2206 // The 1st long-press on inactive text-field is treated as tap
2207 if( EventData::INACTIVE == mImpl->mEventData->mState )
2209 mImpl->ChangeState( EventData::EDITING );
2211 Event event( Event::TAP_EVENT );
2213 event.p2.mFloat = x;
2214 event.p3.mFloat = y;
2215 mImpl->mEventData->mEventQueue.push_back( event );
2217 mImpl->RequestRelayout();
2221 // Reset the imf manger to commit the pre-edit before selecting the text.
2222 mImpl->ResetImfManager();
2224 SelectEvent( x, y, false );
2230 void Controller::SelectEvent( float x, float y, bool selectAll )
2232 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2234 if( NULL != mImpl->mEventData )
2236 mImpl->ChangeState( EventData::SELECTING );
2240 Event event( Event::SELECT_ALL );
2241 mImpl->mEventData->mEventQueue.push_back( event );
2245 Event event( Event::SELECT );
2246 event.p2.mFloat = x;
2247 event.p3.mFloat = y;
2248 mImpl->mEventData->mEventQueue.push_back( event );
2251 mImpl->RequestRelayout();
2255 void Controller::GetTargetSize( Vector2& targetSize )
2257 targetSize = mImpl->mVisualModel->mControlSize;
2260 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2262 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2265 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2267 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2269 if( NULL != mImpl->mEventData )
2271 switch( handleType )
2275 Event event( Event::GRAB_HANDLE_EVENT );
2276 event.p1.mUint = state;
2277 event.p2.mFloat = x;
2278 event.p3.mFloat = y;
2280 mImpl->mEventData->mEventQueue.push_back( event );
2283 case LEFT_SELECTION_HANDLE:
2285 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2286 event.p1.mUint = state;
2287 event.p2.mFloat = x;
2288 event.p3.mFloat = y;
2290 mImpl->mEventData->mEventQueue.push_back( event );
2293 case RIGHT_SELECTION_HANDLE:
2295 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2296 event.p1.mUint = state;
2297 event.p2.mFloat = x;
2298 event.p3.mFloat = y;
2300 mImpl->mEventData->mEventQueue.push_back( event );
2303 case LEFT_SELECTION_HANDLE_MARKER:
2304 case RIGHT_SELECTION_HANDLE_MARKER:
2306 // Markers do not move the handles.
2309 case HANDLE_TYPE_COUNT:
2311 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2315 mImpl->RequestRelayout();
2319 void Controller::PasteText( const std::string& stringToPaste )
2321 InsertText( stringToPaste, Text::Controller::COMMIT );
2322 mImpl->ChangeState( EventData::EDITING );
2323 mImpl->RequestRelayout();
2325 // Do this last since it provides callbacks into application code
2326 mImpl->mControlInterface.TextChanged();
2329 void Controller::PasteClipboardItemEvent()
2331 // Retrieve the clipboard contents first
2332 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2333 std::string stringToPaste( notifier.GetContent() );
2335 // Commit the current pre-edit text; the contents of the clipboard should be appended
2336 mImpl->ResetImfManager();
2339 PasteText( stringToPaste );
2342 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2344 if( NULL == mImpl->mEventData )
2351 case Toolkit::TextSelectionPopup::CUT:
2353 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2354 mImpl->mOperationsPending = ALL_OPERATIONS;
2356 // This is to reset the virtual keyboard to Upper-case
2357 if( 0u == mImpl->mLogicalModel->mText.Count() )
2362 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2363 !mImpl->IsPlaceholderAvailable() )
2365 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2369 ShowPlaceholderText();
2370 mImpl->mEventData->mUpdateCursorPosition = true;
2372 mImpl->RequestRelayout();
2373 mImpl->mControlInterface.TextChanged();
2376 case Toolkit::TextSelectionPopup::COPY:
2378 mImpl->SendSelectionToClipboard( false ); // Text not modified
2379 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
2382 case Toolkit::TextSelectionPopup::PASTE:
2384 std::string stringToPaste("");
2385 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2386 PasteText( stringToPaste );
2389 case Toolkit::TextSelectionPopup::SELECT:
2391 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2393 if( mImpl->mEventData->mSelectionEnabled )
2395 // Creates a SELECT event.
2396 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2400 case Toolkit::TextSelectionPopup::SELECT_ALL:
2402 // Creates a SELECT_ALL event
2403 SelectEvent( 0.f, 0.f, true );
2406 case Toolkit::TextSelectionPopup::CLIPBOARD:
2408 mImpl->ShowClipboard();
2411 case Toolkit::TextSelectionPopup::NONE:
2419 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2421 bool update = false;
2422 bool requestRelayout = false;
2425 unsigned int cursorPosition = 0u;
2427 switch( imfEvent.eventName )
2429 case ImfManager::COMMIT:
2431 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2433 requestRelayout = true;
2436 case ImfManager::PREEDIT:
2438 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2440 requestRelayout = true;
2443 case ImfManager::DELETESURROUNDING:
2445 update = RemoveText( imfEvent.cursorOffset,
2446 imfEvent.numberOfChars,
2447 DONT_UPDATE_INPUT_STYLE );
2451 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2452 !mImpl->IsPlaceholderAvailable() )
2454 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2458 ShowPlaceholderText();
2459 mImpl->mEventData->mUpdateCursorPosition = true;
2462 requestRelayout = true;
2465 case ImfManager::GETSURROUNDING:
2468 cursorPosition = GetLogicalCursorPosition();
2470 imfManager.SetSurroundingText( text );
2471 imfManager.SetCursorPosition( cursorPosition );
2474 case ImfManager::VOID:
2481 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
2484 cursorPosition = GetLogicalCursorPosition();
2487 if( requestRelayout )
2489 mImpl->mOperationsPending = ALL_OPERATIONS;
2490 mImpl->RequestRelayout();
2492 // Do this last since it provides callbacks into application code
2493 mImpl->mControlInterface.TextChanged();
2496 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
2498 return callbackData;
2501 Controller::~Controller()
2506 bool Controller::BackspaceKeyEvent()
2508 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2510 bool removed = false;
2512 if( NULL == mImpl->mEventData )
2517 // IMF manager is no longer handling key-events
2518 mImpl->ClearPreEditFlag();
2520 if( EventData::SELECTING == mImpl->mEventData->mState )
2522 removed = RemoveSelectedText();
2524 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2526 // Remove the character before the current cursor position
2527 removed = RemoveText( -1,
2529 UPDATE_INPUT_STYLE );
2534 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE RemovedText\n", this );
2535 // Notifiy the IMF manager after text changed
2536 // Automatic Upper-case and restarting prediction on an existing word require this.
2539 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2540 !mImpl->IsPlaceholderAvailable() )
2542 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2546 ShowPlaceholderText();
2547 mImpl->mEventData->mUpdateCursorPosition = true;
2554 void Controller::NotifyImfManager()
2556 if( NULL != mImpl->mEventData )
2558 if( mImpl->mEventData->mImfManager )
2560 // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
2563 mImpl->mEventData->mImfManager.SetSurroundingText( text );
2565 mImpl->mEventData->mImfManager.SetCursorPosition( GetLogicalCursorPosition() );
2566 mImpl->mEventData->mImfManager.NotifyCursorPosition();
2571 void Controller::ShowPlaceholderText()
2573 if( mImpl->IsPlaceholderAvailable() )
2575 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2577 if( NULL == mImpl->mEventData )
2582 mImpl->mEventData->mIsShowingPlaceholderText = true;
2584 // Disable handles when showing place-holder text
2585 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2586 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2587 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2589 const char* text( NULL );
2592 // TODO - Switch placeholder text styles when changing state
2593 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2594 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2596 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2597 size = mImpl->mEventData->mPlaceholderTextActive.size();
2601 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2602 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2605 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2606 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2608 // Reset model for showing placeholder.
2609 mImpl->mLogicalModel->mText.Clear();
2610 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2612 // Convert text into UTF-32
2613 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2614 utf32Characters.Resize( size );
2616 // This is a bit horrible but std::string returns a (signed) char*
2617 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2619 // Transform a text array encoded in utf8 into an array encoded in utf32.
2620 // It returns the actual number of characters.
2621 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2622 utf32Characters.Resize( characterCount );
2624 // The characters to be added.
2625 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = characterCount;
2627 // Reset the cursor position
2628 mImpl->mEventData->mPrimaryCursorPosition = 0;
2630 // The natural size needs to be re-calculated.
2631 mImpl->mRecalculateNaturalSize = true;
2633 // Apply modifications to the model
2634 mImpl->mOperationsPending = ALL_OPERATIONS;
2636 // Update the rest of the model during size negotiation
2637 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2641 void Controller::ClearFontData()
2643 if( mImpl->mFontDefaults )
2645 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2648 // Set flags to update the model.
2649 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2650 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2651 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
2653 mImpl->mTextUpdateInfo.mClearAll = true;
2654 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
2655 mImpl->mRecalculateNaturalSize = true;
2657 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2662 UPDATE_ACTUAL_SIZE |
2667 void Controller::ClearStyleData()
2669 mImpl->mLogicalModel->mColorRuns.Clear();
2670 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2673 Controller::Controller( ControlInterface& controlInterface )
2676 mImpl = new Controller::Impl( controlInterface );
2681 } // namespace Toolkit