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::SetAutoScrollEnabled( bool enable )
137 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled[%s] SingleBox[%s]-> [%p]\n", (enable)?"true":"false", ( mImpl->mLayoutEngine.GetLayout() == LayoutEngine::SINGLE_LINE_BOX)?"true":"false", this );
139 if ( mImpl->mLayoutEngine.GetLayout() == LayoutEngine::SINGLE_LINE_BOX )
143 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled for SINGLE_LINE_BOX\n" );
144 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
154 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled Disabling autoscroll\n");
155 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
162 mImpl->mAutoScrollEnabled = enable;
163 mImpl->RequestRelayout();
167 DALI_LOG_WARNING( "Attempted AutoScrolling on a non SINGLE_LINE_BOX, request ignored" );
168 mImpl->mAutoScrollEnabled = false;
172 bool Controller::IsAutoScrollEnabled() const
174 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::IsAutoScrollEnabled[%s]\n", (mImpl->mAutoScrollEnabled)?"true":"false" );
176 return mImpl->mAutoScrollEnabled;
179 CharacterDirection Controller::GetAutoScrollDirection() const
181 return mImpl->mAutoScrollDirectionRTL;
184 float Controller::GetAutoScrollLineAlignment() const
188 if( mImpl->mVisualModel &&
189 ( 0u != mImpl->mVisualModel->mLines.Count() ) )
191 offset = ( *mImpl->mVisualModel->mLines.Begin() ).alignmentOffset;
197 void Controller::SetText( const std::string& text )
199 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
201 // Reset keyboard as text changed
202 mImpl->ResetImfManager();
204 // Remove the previously set text and style.
210 CharacterIndex lastCursorIndex = 0u;
212 if( NULL != mImpl->mEventData )
214 // If popup shown then hide it by switching to Editing state
215 if( ( EventData::SELECTING == mImpl->mEventData->mState ) ||
216 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
217 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
218 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
220 mImpl->ChangeState( EventData::EDITING );
226 mImpl->mVisualModel->SetTextColor( mImpl->mTextColor );
228 MarkupProcessData markupProcessData( mImpl->mLogicalModel->mColorRuns,
229 mImpl->mLogicalModel->mFontDescriptionRuns );
231 Length textSize = 0u;
232 const uint8_t* utf8 = NULL;
233 if( mImpl->mMarkupProcessorEnabled )
235 ProcessMarkupString( text, markupProcessData );
236 textSize = markupProcessData.markupProcessedText.size();
238 // This is a bit horrible but std::string returns a (signed) char*
239 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
243 textSize = text.size();
245 // This is a bit horrible but std::string returns a (signed) char*
246 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
249 // Convert text into UTF-32
250 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
251 utf32Characters.Resize( textSize );
253 // Transform a text array encoded in utf8 into an array encoded in utf32.
254 // It returns the actual number of characters.
255 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
256 utf32Characters.Resize( characterCount );
258 DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
259 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mLogicalModel->mText.Count() );
261 // The characters to be added.
262 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
264 // To reset the cursor position
265 lastCursorIndex = characterCount;
267 // Update the rest of the model during size negotiation
268 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
270 // The natural size needs to be re-calculated.
271 mImpl->mRecalculateNaturalSize = true;
273 // Apply modifications to the model
274 mImpl->mOperationsPending = ALL_OPERATIONS;
278 ShowPlaceholderText();
281 // Resets the cursor position.
282 ResetCursorPosition( lastCursorIndex );
284 // Scrolls the text to make the cursor visible.
285 ResetScrollPosition();
287 mImpl->RequestRelayout();
289 if( NULL != mImpl->mEventData )
291 // Cancel previously queued events
292 mImpl->mEventData->mEventQueue.clear();
295 // Notify IMF as text changed
298 // Do this last since it provides callbacks into application code
299 mImpl->mControlInterface.TextChanged();
302 void Controller::GetText( std::string& text ) const
304 if( !mImpl->IsShowingPlaceholderText() )
306 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
308 if( 0u != utf32Characters.Count() )
310 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
315 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
319 unsigned int Controller::GetLogicalCursorPosition() const
321 if( NULL != mImpl->mEventData )
323 return mImpl->mEventData->mPrimaryCursorPosition;
329 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
331 if( NULL != mImpl->mEventData )
333 if( PLACEHOLDER_TYPE_INACTIVE == type )
335 mImpl->mEventData->mPlaceholderTextInactive = text;
339 mImpl->mEventData->mPlaceholderTextActive = text;
342 // Update placeholder if there is no text
343 if( mImpl->IsShowingPlaceholderText() ||
344 ( 0u == mImpl->mLogicalModel->mText.Count() ) )
346 ShowPlaceholderText();
351 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
353 if( NULL != mImpl->mEventData )
355 if( PLACEHOLDER_TYPE_INACTIVE == type )
357 text = mImpl->mEventData->mPlaceholderTextInactive;
361 text = mImpl->mEventData->mPlaceholderTextActive;
366 void Controller::SetMaximumNumberOfCharacters( Length maxCharacters )
368 mImpl->mMaximumNumberOfCharacters = maxCharacters;
371 int Controller::GetMaximumNumberOfCharacters()
373 return mImpl->mMaximumNumberOfCharacters;
376 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
378 if( NULL == mImpl->mFontDefaults )
380 mImpl->mFontDefaults = new FontDefaults();
383 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
384 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
385 mImpl->mFontDefaults->familyDefined = true;
387 // Clear the font-specific data
390 mImpl->RequestRelayout();
393 const std::string& Controller::GetDefaultFontFamily() const
395 if( NULL != mImpl->mFontDefaults )
397 return mImpl->mFontDefaults->mFontDescription.family;
403 void Controller::SetDefaultFontStyle( const std::string& style )
405 if( NULL == mImpl->mFontDefaults )
407 mImpl->mFontDefaults = new FontDefaults();
410 mImpl->mFontDefaults->mFontStyle = style;
413 const std::string& Controller::GetDefaultFontStyle() const
415 if( NULL != mImpl->mFontDefaults )
417 return mImpl->mFontDefaults->mFontStyle;
423 void Controller::SetDefaultFontWeight( FontWeight weight )
425 if( NULL == mImpl->mFontDefaults )
427 mImpl->mFontDefaults = new FontDefaults();
430 mImpl->mFontDefaults->mFontDescription.weight = weight;
431 mImpl->mFontDefaults->weightDefined = true;
433 // Clear the font-specific data
436 mImpl->RequestRelayout();
439 FontWeight Controller::GetDefaultFontWeight() const
441 if( NULL != mImpl->mFontDefaults )
443 return mImpl->mFontDefaults->mFontDescription.weight;
446 return TextAbstraction::FontWeight::NORMAL;
449 void Controller::SetDefaultFontWidth( FontWidth width )
451 if( NULL == mImpl->mFontDefaults )
453 mImpl->mFontDefaults = new FontDefaults();
456 mImpl->mFontDefaults->mFontDescription.width = width;
457 mImpl->mFontDefaults->widthDefined = true;
459 // Clear the font-specific data
462 mImpl->RequestRelayout();
465 FontWidth Controller::GetDefaultFontWidth() const
467 if( NULL != mImpl->mFontDefaults )
469 return mImpl->mFontDefaults->mFontDescription.width;
472 return TextAbstraction::FontWidth::NORMAL;
475 void Controller::SetDefaultFontSlant( FontSlant slant )
477 if( NULL == mImpl->mFontDefaults )
479 mImpl->mFontDefaults = new FontDefaults();
482 mImpl->mFontDefaults->mFontDescription.slant = slant;
483 mImpl->mFontDefaults->slantDefined = true;
485 // Clear the font-specific data
488 mImpl->RequestRelayout();
491 FontSlant Controller::GetDefaultFontSlant() const
493 if( NULL != mImpl->mFontDefaults )
495 return mImpl->mFontDefaults->mFontDescription.slant;
498 return TextAbstraction::FontSlant::NORMAL;
501 void Controller::SetDefaultPointSize( float pointSize )
503 if( NULL == mImpl->mFontDefaults )
505 mImpl->mFontDefaults = new FontDefaults();
508 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
509 mImpl->mFontDefaults->sizeDefined = true;
511 unsigned int horizontalDpi( 0u );
512 unsigned int verticalDpi( 0u );
513 mImpl->mFontClient.GetDpi( horizontalDpi, verticalDpi );
515 // Adjust the metrics if the fixed-size font should be down-scaled
516 int maxEmojiSize( pointSize/POINTS_PER_INCH * verticalDpi );
517 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultPointSize %p setting MaxEmojiSize %d\n", this, maxEmojiSize );
518 mImpl->mMetrics->SetMaxEmojiSize( maxEmojiSize );
520 // Clear the font-specific data
523 mImpl->RequestRelayout();
526 float Controller::GetDefaultPointSize() const
528 if( NULL != mImpl->mFontDefaults )
530 return mImpl->mFontDefaults->mDefaultPointSize;
536 void Controller::UpdateAfterFontChange( const std::string& newDefaultFont )
538 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::UpdateAfterFontChange");
540 if( !mImpl->mFontDefaults->familyDefined ) // If user defined font then should not update when system font changes
542 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
543 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
547 mImpl->RequestRelayout();
551 void Controller::SetTextColor( const Vector4& textColor )
553 mImpl->mTextColor = textColor;
555 if( !mImpl->IsShowingPlaceholderText() )
557 mImpl->mVisualModel->SetTextColor( textColor );
559 mImpl->RequestRelayout();
563 const Vector4& Controller::GetTextColor() const
565 return mImpl->mTextColor;
568 bool Controller::RemoveText( int cursorOffset,
569 int numberOfCharacters,
570 UpdateInputStyleType type )
572 bool removed = false;
574 if( NULL == mImpl->mEventData )
579 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfCharacters %d\n",
580 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters );
582 if( !mImpl->IsShowingPlaceholderText() )
584 // Delete at current cursor position
585 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
586 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
588 CharacterIndex cursorIndex = oldCursorIndex;
590 // Validate the cursor position & number of characters
591 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
593 cursorIndex = oldCursorIndex + cursorOffset;
596 if( ( cursorIndex + numberOfCharacters ) > currentText.Count() )
598 numberOfCharacters = currentText.Count() - cursorIndex;
601 if( ( cursorIndex + numberOfCharacters ) <= mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters )
603 // Mark the paragraphs to be updated.
604 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
605 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove += numberOfCharacters;
607 // Update the input style and remove the text's style before removing the text.
609 if( UPDATE_INPUT_STYLE == type )
611 // Set first the default input style.
612 mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
614 // Update the input style.
615 mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
618 // Updates the text style runs by removing characters. Runs with no characters are removed.
619 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfCharacters );
621 // Remove the characters.
622 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
623 Vector<Character>::Iterator last = first + numberOfCharacters;
625 currentText.Erase( first, last );
627 // Cursor position retreat
628 oldCursorIndex = cursorIndex;
630 mImpl->mEventData->mScrollAfterDelete = true;
632 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfCharacters );
640 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
642 if( NULL != mImpl->mEventData )
644 mImpl->mEventData->mPlaceholderTextColor = textColor;
647 if( mImpl->IsShowingPlaceholderText() )
649 mImpl->mVisualModel->SetTextColor( textColor );
650 mImpl->RequestRelayout();
654 const Vector4& Controller::GetPlaceholderTextColor() const
656 if( NULL != mImpl->mEventData )
658 return mImpl->mEventData->mPlaceholderTextColor;
664 void Controller::SetShadowOffset( const Vector2& shadowOffset )
666 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
668 mImpl->RequestRelayout();
671 const Vector2& Controller::GetShadowOffset() const
673 return mImpl->mVisualModel->GetShadowOffset();
676 void Controller::SetShadowColor( const Vector4& shadowColor )
678 mImpl->mVisualModel->SetShadowColor( shadowColor );
680 mImpl->RequestRelayout();
683 const Vector4& Controller::GetShadowColor() const
685 return mImpl->mVisualModel->GetShadowColor();
688 void Controller::SetUnderlineColor( const Vector4& color )
690 mImpl->mVisualModel->SetUnderlineColor( color );
692 mImpl->RequestRelayout();
695 const Vector4& Controller::GetUnderlineColor() const
697 return mImpl->mVisualModel->GetUnderlineColor();
700 void Controller::SetUnderlineEnabled( bool enabled )
702 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
704 mImpl->RequestRelayout();
707 bool Controller::IsUnderlineEnabled() const
709 return mImpl->mVisualModel->IsUnderlineEnabled();
712 void Controller::SetUnderlineHeight( float height )
714 mImpl->mVisualModel->SetUnderlineHeight( height );
716 mImpl->RequestRelayout();
719 float Controller::GetUnderlineHeight() const
721 return mImpl->mVisualModel->GetUnderlineHeight();
724 void Controller::SetInputColor( const Vector4& color )
726 if( NULL != mImpl->mEventData )
728 mImpl->mEventData->mInputStyle.textColor = color;
729 mImpl->mEventData->mInputStyle.isDefaultColor = false;
731 if( EventData::SELECTING == mImpl->mEventData->mState )
733 const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
735 // Get start and end position of selection
736 const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
737 const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
739 // Add the color run.
740 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
741 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
743 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
744 colorRun.color = color;
745 colorRun.characterRun.characterIndex = startOfSelectedText;
746 colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
748 // Request to relayout.
749 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
750 mImpl->RequestRelayout();
752 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
753 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
754 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
759 const Vector4& Controller::GetInputColor() const
761 if( NULL != mImpl->mEventData )
763 return mImpl->mEventData->mInputStyle.textColor;
766 // Return the default text's color if there is no EventData.
767 return mImpl->mTextColor;
771 void Controller::SetInputFontFamily( const std::string& fontFamily )
773 if( NULL != mImpl->mEventData )
775 mImpl->mEventData->mInputStyle.familyName = fontFamily;
776 mImpl->mEventData->mInputStyle.familyDefined = true;
778 if( EventData::SELECTING == mImpl->mEventData->mState )
780 CharacterIndex startOfSelectedText = 0u;
781 Length lengthOfSelectedText = 0u;
782 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
783 mImpl->mLogicalModel,
785 lengthOfSelectedText );
787 fontDescriptionRun.familyLength = fontFamily.size();
788 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
789 memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
790 fontDescriptionRun.familyDefined = true;
792 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
794 // Request to relayout.
795 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
803 mImpl->mRecalculateNaturalSize = true;
804 mImpl->RequestRelayout();
806 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
807 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
808 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
810 // As the font changes, recalculate the handle positions is needed.
811 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
812 mImpl->mEventData->mUpdateRightSelectionPosition = true;
813 mImpl->mEventData->mScrollAfterUpdatePosition = true;
818 const std::string& Controller::GetInputFontFamily() const
820 if( NULL != mImpl->mEventData )
822 return mImpl->mEventData->mInputStyle.familyName;
825 // Return the default font's family if there is no EventData.
826 return GetDefaultFontFamily();
829 void Controller::SetInputFontStyle( const std::string& fontStyle )
831 if( NULL != mImpl->mEventData )
833 mImpl->mEventData->mInputStyle.fontStyle = fontStyle;
837 const std::string& Controller::GetInputFontStyle() const
839 if( NULL != mImpl->mEventData )
841 return mImpl->mEventData->mInputStyle.fontStyle;
844 // Return the default font's style if there is no EventData.
845 return GetDefaultFontStyle();
848 void Controller::SetInputFontWeight( FontWeight weight )
850 if( NULL != mImpl->mEventData )
852 mImpl->mEventData->mInputStyle.weight = weight;
853 mImpl->mEventData->mInputStyle.weightDefined = true;
855 if( EventData::SELECTING == mImpl->mEventData->mState )
857 CharacterIndex startOfSelectedText = 0u;
858 Length lengthOfSelectedText = 0u;
859 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
860 mImpl->mLogicalModel,
862 lengthOfSelectedText );
864 fontDescriptionRun.weight = weight;
865 fontDescriptionRun.weightDefined = true;
867 // Request to relayout.
868 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
876 mImpl->mRecalculateNaturalSize = true;
877 mImpl->RequestRelayout();
879 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
880 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
881 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
883 // As the font might change, recalculate the handle positions is needed.
884 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
885 mImpl->mEventData->mUpdateRightSelectionPosition = true;
886 mImpl->mEventData->mScrollAfterUpdatePosition = true;
891 FontWeight Controller::GetInputFontWeight() const
893 if( NULL != mImpl->mEventData )
895 return mImpl->mEventData->mInputStyle.weight;
898 return GetDefaultFontWeight();
901 void Controller::SetInputFontWidth( FontWidth width )
903 if( NULL != mImpl->mEventData )
905 mImpl->mEventData->mInputStyle.width = width;
906 mImpl->mEventData->mInputStyle.widthDefined = true;
908 if( EventData::SELECTING == mImpl->mEventData->mState )
910 CharacterIndex startOfSelectedText = 0u;
911 Length lengthOfSelectedText = 0u;
912 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
913 mImpl->mLogicalModel,
915 lengthOfSelectedText );
917 fontDescriptionRun.width = width;
918 fontDescriptionRun.widthDefined = true;
920 // Request to relayout.
921 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
929 mImpl->mRecalculateNaturalSize = true;
930 mImpl->RequestRelayout();
932 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
933 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
934 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
936 // As the font might change, recalculate the handle positions is needed.
937 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
938 mImpl->mEventData->mUpdateRightSelectionPosition = true;
939 mImpl->mEventData->mScrollAfterUpdatePosition = true;
944 FontWidth Controller::GetInputFontWidth() const
946 if( NULL != mImpl->mEventData )
948 return mImpl->mEventData->mInputStyle.width;
951 return GetDefaultFontWidth();
954 void Controller::SetInputFontSlant( FontSlant slant )
956 if( NULL != mImpl->mEventData )
958 mImpl->mEventData->mInputStyle.slant = slant;
959 mImpl->mEventData->mInputStyle.slantDefined = 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.slant = slant;
971 fontDescriptionRun.slantDefined = 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->mScrollAfterUpdatePosition = true;
997 FontSlant Controller::GetInputFontSlant() const
999 if( NULL != mImpl->mEventData )
1001 return mImpl->mEventData->mInputStyle.slant;
1004 return GetDefaultFontSlant();
1007 void Controller::SetInputFontPointSize( float size )
1009 if( NULL != mImpl->mEventData )
1011 mImpl->mEventData->mInputStyle.size = size;
1013 if( EventData::SELECTING == mImpl->mEventData->mState )
1015 CharacterIndex startOfSelectedText = 0u;
1016 Length lengthOfSelectedText = 0u;
1017 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1018 mImpl->mLogicalModel,
1019 startOfSelectedText,
1020 lengthOfSelectedText );
1022 fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
1023 fontDescriptionRun.sizeDefined = true;
1025 // Request to relayout.
1026 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1031 UPDATE_ACTUAL_SIZE |
1034 mImpl->mRecalculateNaturalSize = true;
1035 mImpl->RequestRelayout();
1037 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1038 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1039 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1041 // As the font might change, recalculate the handle positions is needed.
1042 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1043 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1044 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1049 float Controller::GetInputFontPointSize() const
1051 if( NULL != mImpl->mEventData )
1053 return mImpl->mEventData->mInputStyle.size;
1056 // Return the default font's point size if there is no EventData.
1057 return GetDefaultPointSize();
1060 void Controller::SetEnableCursorBlink( bool enable )
1062 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
1064 if( NULL != mImpl->mEventData )
1066 mImpl->mEventData->mCursorBlinkEnabled = enable;
1069 mImpl->mEventData->mDecorator )
1071 mImpl->mEventData->mDecorator->StopCursorBlink();
1076 bool Controller::GetEnableCursorBlink() const
1078 if( NULL != mImpl->mEventData )
1080 return mImpl->mEventData->mCursorBlinkEnabled;
1086 const Vector2& Controller::GetScrollPosition() const
1088 return mImpl->mScrollPosition;
1091 Vector3 Controller::GetNaturalSize()
1093 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
1094 Vector3 naturalSize;
1096 // Make sure the model is up-to-date before layouting
1097 ProcessModifyEvents();
1099 if( mImpl->mRecalculateNaturalSize )
1101 // Operations that can be done only once until the text changes.
1102 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1109 GET_GLYPH_METRICS );
1110 // Make sure the model is up-to-date before layouting
1111 mImpl->UpdateModel( onlyOnceOperations );
1113 // Layout the text for the new width.
1114 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1116 // Set the update info to relayout the whole text.
1117 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1118 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1120 // Store the actual control's size to restore later.
1121 const Size actualControlSize = mImpl->mVisualModel->mControlSize;
1123 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
1124 static_cast<OperationsMask>( onlyOnceOperations |
1126 naturalSize.GetVectorXY() );
1128 // Do not do again the only once operations.
1129 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1131 // Do the size related operations again.
1132 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1135 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1137 // Stores the natural size to avoid recalculate it again
1138 // unless the text/style changes.
1139 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
1141 mImpl->mRecalculateNaturalSize = false;
1143 // Clear the update info. This info will be set the next time the text is updated.
1144 mImpl->mTextUpdateInfo.Clear();
1146 // Restore the actual control's size.
1147 mImpl->mVisualModel->mControlSize = actualControlSize;
1149 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1153 naturalSize = mImpl->mVisualModel->GetNaturalSize();
1155 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1158 naturalSize.x = ConvertToEven( naturalSize.x );
1159 naturalSize.y = ConvertToEven( naturalSize.y );
1164 float Controller::GetHeightForWidth( float width )
1166 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1167 // Make sure the model is up-to-date before layouting
1168 ProcessModifyEvents();
1171 if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
1173 // Operations that can be done only once until the text changes.
1174 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1181 GET_GLYPH_METRICS );
1182 // Make sure the model is up-to-date before layouting
1183 mImpl->UpdateModel( onlyOnceOperations );
1186 // Layout the text for the new width.
1187 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1189 // Set the update info to relayout the whole text.
1190 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1191 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1193 // Store the actual control's width.
1194 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
1196 DoRelayout( Size( width, MAX_FLOAT ),
1197 static_cast<OperationsMask>( onlyOnceOperations |
1201 // Do not do again the only once operations.
1202 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1204 // Do the size related operations again.
1205 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1209 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1211 // Clear the update info. This info will be set the next time the text is updated.
1212 mImpl->mTextUpdateInfo.Clear();
1214 // Restore the actual control's width.
1215 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1217 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1221 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1222 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1225 return layoutSize.height;
1228 bool Controller::Relayout( const Size& size )
1230 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", this, size.width, size.height, (mImpl->mAutoScrollEnabled)?"true":"false" );
1232 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1234 bool glyphsRemoved( false );
1235 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1237 mImpl->mVisualModel->mGlyphPositions.Clear();
1238 glyphsRemoved = true;
1241 // Clear the update info. This info will be set the next time the text is updated.
1242 mImpl->mTextUpdateInfo.Clear();
1244 // Not worth to relayout if width or height is equal to zero.
1245 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1247 return glyphsRemoved;
1250 // Whether a new size has been set.
1251 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1255 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1257 // Layout operations that need to be done if the size changes.
1258 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1261 UPDATE_ACTUAL_SIZE |
1263 // Set the update info to relayout the whole text.
1264 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
1265 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1268 // Whether there are modify events.
1269 if( 0u != mImpl->mModifyEvents.Count() )
1271 // Style operations that need to be done if the text is modified.
1272 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1276 // Make sure the model is up-to-date before layouting.
1277 ProcessModifyEvents();
1278 bool updated = mImpl->UpdateModel( mImpl->mOperationsPending );
1282 updated = DoRelayout( size,
1283 mImpl->mOperationsPending,
1284 layoutSize ) || updated;
1286 // Do not re-do any operation until something changes.
1287 mImpl->mOperationsPending = NO_OPERATION;
1289 // Whether the text control is editable
1290 const bool isEditable = NULL != mImpl->mEventData;
1292 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
1294 if( newSize && isEditable )
1296 offset = mImpl->mScrollPosition;
1299 if( !isEditable || !IsMultiLineEnabled() )
1301 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
1302 CalculateVerticalOffset( size );
1309 // If there is a new size, the scroll position needs to be clamped.
1310 mImpl->ClampHorizontalScroll( layoutSize );
1312 // Update the decorator's positions is needed if there is a new size.
1313 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mScrollPosition - offset );
1316 // Move the cursor, grab handle etc.
1317 updated = mImpl->ProcessInputEvents() || updated;
1320 // Clear the update info. This info will be set the next time the text is updated.
1321 mImpl->mTextUpdateInfo.Clear();
1322 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1327 void Controller::ProcessModifyEvents()
1329 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
1331 if( 0u == events.Count() )
1337 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
1338 endIt = events.End();
1342 const ModifyEvent& event = *it;
1344 if( ModifyEvent::TEXT_REPLACED == event.type )
1346 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
1347 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
1349 TextReplacedEvent();
1351 else if( ModifyEvent::TEXT_INSERTED == event.type )
1353 TextInsertedEvent();
1355 else if( ModifyEvent::TEXT_DELETED == event.type )
1357 // Placeholder-text cannot be deleted
1358 if( !mImpl->IsShowingPlaceholderText() )
1365 if( NULL != mImpl->mEventData )
1367 // When the text is being modified, delay cursor blinking
1368 mImpl->mEventData->mDecorator->DelayCursorBlink();
1371 // Discard temporary text
1375 void Controller::ResetText()
1378 mImpl->mLogicalModel->mText.Clear();
1380 // We have cleared everything including the placeholder-text
1381 mImpl->PlaceholderCleared();
1383 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1384 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1385 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
1387 // Clear any previous text.
1388 mImpl->mTextUpdateInfo.mClearAll = true;
1390 // The natural size needs to be re-calculated.
1391 mImpl->mRecalculateNaturalSize = true;
1393 // Apply modifications to the model
1394 mImpl->mOperationsPending = ALL_OPERATIONS;
1397 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
1399 // Reset the cursor position
1400 if( NULL != mImpl->mEventData )
1402 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
1404 // Update the cursor if it's in editing mode.
1405 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1407 mImpl->mEventData->mUpdateCursorPosition = true;
1412 void Controller::ResetScrollPosition()
1414 if( NULL != mImpl->mEventData )
1416 // Reset the scroll position.
1417 mImpl->mScrollPosition = Vector2::ZERO;
1418 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1422 void Controller::TextReplacedEvent()
1424 // The natural size needs to be re-calculated.
1425 mImpl->mRecalculateNaturalSize = true;
1427 // Apply modifications to the model
1428 mImpl->mOperationsPending = ALL_OPERATIONS;
1431 void Controller::TextInsertedEvent()
1433 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
1435 if( NULL == mImpl->mEventData )
1440 // The natural size needs to be re-calculated.
1441 mImpl->mRecalculateNaturalSize = true;
1443 // Apply modifications to the model; TODO - Optimize this
1444 mImpl->mOperationsPending = ALL_OPERATIONS;
1447 void Controller::TextDeletedEvent()
1449 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
1451 if( NULL == mImpl->mEventData )
1456 // The natural size needs to be re-calculated.
1457 mImpl->mRecalculateNaturalSize = true;
1459 // Apply modifications to the model; TODO - Optimize this
1460 mImpl->mOperationsPending = ALL_OPERATIONS;
1463 bool Controller::DoRelayout( const Size& size,
1464 OperationsMask operationsRequired,
1467 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
1468 bool viewUpdated( false );
1470 // Calculate the operations to be done.
1471 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
1473 const CharacterIndex startIndex = mImpl->mTextUpdateInfo.mParagraphCharacterIndex;
1474 const Length requestedNumberOfCharacters = mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters;
1476 if( NO_OPERATION != ( LAYOUT & operations ) )
1478 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
1480 // Some vectors with data needed to layout and reorder may be void
1481 // after the first time the text has been laid out.
1482 // Fill the vectors again.
1484 // Calculate the number of glyphs to layout.
1485 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1486 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1487 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
1488 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
1490 const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
1491 const GlyphIndex startGlyphIndex = mImpl->mTextUpdateInfo.mStartGlyphIndex;
1492 const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
1493 const Length totalNumberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
1495 if( 0u == totalNumberOfGlyphs )
1497 if( NO_OPERATION != ( UPDATE_ACTUAL_SIZE & operations ) )
1499 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
1502 // Nothing else to do if there is no glyphs.
1503 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1507 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1508 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1509 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1510 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1511 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1512 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1513 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1515 // Set the layout parameters.
1516 LayoutParameters layoutParameters( size,
1518 lineBreakInfo.Begin(),
1519 wordBreakInfo.Begin(),
1520 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1522 glyphsToCharactersMap.Begin(),
1523 charactersPerGlyph.Begin(),
1524 charactersToGlyphBuffer,
1525 glyphsPerCharacterBuffer,
1526 totalNumberOfGlyphs );
1528 // Resize the vector of positions to have the same size than the vector of glyphs.
1529 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1530 glyphPositions.Resize( totalNumberOfGlyphs );
1532 // Whether the last character is a new paragraph character.
1533 mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1534 layoutParameters.isLastNewParagraph = mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph;
1536 // The initial glyph and the number of glyphs to layout.
1537 layoutParameters.startGlyphIndex = startGlyphIndex;
1538 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1539 layoutParameters.startLineIndex = mImpl->mTextUpdateInfo.mStartLineIndex;
1540 layoutParameters.estimatedNumberOfLines = mImpl->mTextUpdateInfo.mEstimatedNumberOfLines;
1542 // Update the visual model.
1543 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1545 mImpl->mVisualModel->mLines,
1551 if ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) )
1553 mImpl->mAutoScrollDirectionRTL = false;
1556 // Reorder the lines
1557 if( NO_OPERATION != ( REORDER & operations ) )
1559 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1560 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1562 // Check first if there are paragraphs with bidirectional info.
1563 if( 0u != bidirectionalInfo.Count() )
1566 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1568 // Reorder the lines.
1569 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1570 ReorderLines( bidirectionalInfo,
1572 requestedNumberOfCharacters,
1573 mImpl->mVisualModel->mLines,
1574 bidirectionalLineInfo );
1576 // Set the bidirectional info per line into the layout parameters.
1577 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1578 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1580 // Re-layout the text. Reorder those lines with right to left characters.
1581 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1583 requestedNumberOfCharacters,
1586 if ( ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) ) && ( numberOfLines > 0 ) )
1588 const LineRun* const firstline = mImpl->mVisualModel->mLines.Begin();
1591 mImpl->mAutoScrollDirectionRTL = firstline->direction;
1597 // Sets the actual size.
1598 if( NO_OPERATION != ( UPDATE_ACTUAL_SIZE & operations ) )
1600 mImpl->mVisualModel->SetLayoutSize( layoutSize );
1604 // Store the size used to layout the text.
1605 mImpl->mVisualModel->mControlSize = size;
1609 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1612 if( NO_OPERATION != ( ALIGN & operations ) )
1614 // The laid-out lines.
1615 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1617 mImpl->mLayoutEngine.Align( size,
1619 requestedNumberOfCharacters,
1624 #if defined(DEBUG_ENABLED)
1625 std::string currentText;
1626 GetText( currentText );
1627 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mAutoScrollDirectionRTL[%s] [%s]\n", this, (mImpl->mAutoScrollDirectionRTL)?"true":"false", currentText.c_str() );
1629 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1633 void Controller::SetMultiLineEnabled( bool enable )
1635 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1637 if( layout != mImpl->mLayoutEngine.GetLayout() )
1639 // Set the layout type.
1640 mImpl->mLayoutEngine.SetLayout( layout );
1642 // Set the flags to redo the layout operations
1643 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1644 UPDATE_ACTUAL_SIZE |
1648 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1650 mImpl->RequestRelayout();
1654 bool Controller::IsMultiLineEnabled() const
1656 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1659 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1661 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1663 // Set the alignment.
1664 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1666 // Set the flag to redo the alignment operation.
1667 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1669 mImpl->RequestRelayout();
1673 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1675 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1678 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1680 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1682 // Set the alignment.
1683 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1685 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1687 mImpl->RequestRelayout();
1691 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1693 return mImpl->mLayoutEngine.GetVerticalAlignment();
1696 void Controller::CalculateVerticalOffset( const Size& controlSize )
1698 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1700 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1702 // Get the line height of the default font.
1703 layoutSize.height = mImpl->GetDefaultFontLineHeight();
1706 switch( mImpl->mLayoutEngine.GetVerticalAlignment() )
1708 case LayoutEngine::VERTICAL_ALIGN_TOP:
1710 mImpl->mScrollPosition.y = 0.f;
1713 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1715 mImpl->mScrollPosition.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
1718 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1720 mImpl->mScrollPosition.y = controlSize.height - layoutSize.height;
1726 LayoutEngine& Controller::GetLayoutEngine()
1728 return mImpl->mLayoutEngine;
1731 View& Controller::GetView()
1733 return mImpl->mView;
1736 void Controller::KeyboardFocusGainEvent()
1738 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1740 if( NULL != mImpl->mEventData )
1742 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1743 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1745 mImpl->ChangeState( EventData::EDITING );
1746 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1749 if( mImpl->IsShowingPlaceholderText() )
1751 // Show alternative placeholder-text when editing
1752 ShowPlaceholderText();
1755 mImpl->RequestRelayout();
1759 void Controller::KeyboardFocusLostEvent()
1761 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1763 if( NULL != mImpl->mEventData )
1765 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1767 mImpl->ChangeState( EventData::INACTIVE );
1769 if( !mImpl->IsShowingRealText() )
1771 // Revert to regular placeholder-text when not editing
1772 ShowPlaceholderText();
1776 mImpl->RequestRelayout();
1779 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1781 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1783 bool textChanged( false );
1785 if( ( NULL != mImpl->mEventData ) &&
1786 ( keyEvent.state == KeyEvent::Down ) )
1788 int keyCode = keyEvent.keyCode;
1789 const std::string& keyString = keyEvent.keyPressed;
1791 // Pre-process to separate modifying events from non-modifying input events.
1792 if( Dali::DALI_KEY_ESCAPE == keyCode )
1794 // Escape key is a special case which causes focus loss
1795 KeyboardFocusLostEvent();
1797 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
1798 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1799 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
1800 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
1802 Event event( Event::CURSOR_KEY_EVENT );
1803 event.p1.mInt = keyCode;
1804 mImpl->mEventData->mEventQueue.push_back( event );
1806 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1808 textChanged = BackspaceKeyEvent();
1810 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1812 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1813 // Avoids calling the InsertText() method which can delete selected text
1815 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1816 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1818 mImpl->ChangeState( EventData::INACTIVE );
1819 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1820 // Avoids calling the InsertText() method which can delete selected text
1822 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1824 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1825 // and a character is typed after the type of a upper case latin character.
1831 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1833 // IMF manager is no longer handling key-events
1834 mImpl->ClearPreEditFlag();
1836 InsertText( keyString, COMMIT );
1840 if( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1841 ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1843 mImpl->ChangeState( EventData::EDITING );
1846 mImpl->RequestRelayout();
1851 // Do this last since it provides callbacks into application code
1852 mImpl->mControlInterface.TextChanged();
1858 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1860 bool removedPrevious( false );
1861 bool maxLengthReached( false );
1863 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1865 if( NULL == mImpl->mEventData )
1870 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1871 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1872 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1874 // TODO: At the moment the underline runs are only for pre-edit.
1875 mImpl->mVisualModel->mUnderlineRuns.Clear();
1877 // Keep the current number of characters.
1878 const Length currentNumberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
1880 // Remove the previous IMF pre-edit.
1881 if( mImpl->mEventData->mPreEditFlag && ( 0u != mImpl->mEventData->mPreEditLength ) )
1883 removedPrevious = RemoveText( -static_cast<int>( mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition ),
1884 mImpl->mEventData->mPreEditLength,
1885 DONT_UPDATE_INPUT_STYLE );
1887 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1888 mImpl->mEventData->mPreEditLength = 0u;
1892 // Remove the previous Selection.
1893 removedPrevious = RemoveSelectedText();
1896 Vector<Character> utf32Characters;
1897 Length characterCount = 0u;
1901 // Convert text into UTF-32
1902 utf32Characters.Resize( text.size() );
1904 // This is a bit horrible but std::string returns a (signed) char*
1905 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1907 // Transform a text array encoded in utf8 into an array encoded in utf32.
1908 // It returns the actual number of characters.
1909 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1910 utf32Characters.Resize( characterCount );
1912 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1913 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1916 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1918 // The placeholder text is no longer needed
1919 if( mImpl->IsShowingPlaceholderText() )
1924 mImpl->ChangeState( EventData::EDITING );
1926 // Handle the IMF (predicitive text) state changes
1927 if( COMMIT == type )
1929 // IMF manager is no longer handling key-events
1930 mImpl->ClearPreEditFlag();
1934 if( !mImpl->mEventData->mPreEditFlag )
1936 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1938 // Record the start of the pre-edit text
1939 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1942 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1943 mImpl->mEventData->mPreEditFlag = true;
1945 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1948 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1950 // Restrict new text to fit within Maximum characters setting.
1951 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1952 maxLengthReached = ( characterCount > maxSizeOfNewText );
1954 // The cursor position.
1955 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1957 // Update the text's style.
1959 // Updates the text style runs by adding characters.
1960 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
1962 // Get the character index from the cursor index.
1963 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
1965 // Retrieve the text's style for the given index.
1967 mImpl->RetrieveDefaultInputStyle( style );
1968 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
1970 // Whether to add a new text color run.
1971 const bool addColorRun = ( style.textColor != mImpl->mEventData->mInputStyle.textColor );
1973 // Whether to add a new font run.
1974 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
1975 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
1976 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
1977 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
1978 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
1983 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
1984 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
1986 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
1987 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
1988 colorRun.characterRun.characterIndex = cursorIndex;
1989 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1992 if( addFontNameRun ||
1998 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
1999 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
2001 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
2003 if( addFontNameRun )
2005 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
2006 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
2007 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
2008 fontDescriptionRun.familyDefined = true;
2010 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
2013 if( addFontWeightRun )
2015 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
2016 fontDescriptionRun.weightDefined = true;
2019 if( addFontWidthRun )
2021 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
2022 fontDescriptionRun.widthDefined = true;
2025 if( addFontSlantRun )
2027 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
2028 fontDescriptionRun.slantDefined = true;
2031 if( addFontSizeRun )
2033 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
2034 fontDescriptionRun.sizeDefined = true;
2037 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
2038 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2041 // Insert at current cursor position.
2042 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
2044 if( cursorIndex < numberOfCharactersInModel )
2046 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2050 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2053 // Mark the first paragraph to be updated.
2054 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
2055 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
2057 // Update the cursor index.
2058 cursorIndex += maxSizeOfNewText;
2060 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
2063 const Length numberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2065 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
2066 mImpl->IsPlaceholderAvailable() )
2068 // Show place-holder if empty after removing the pre-edit text
2069 ShowPlaceholderText();
2070 mImpl->mEventData->mUpdateCursorPosition = true;
2071 mImpl->ClearPreEditFlag();
2073 else if( removedPrevious ||
2074 ( 0 != utf32Characters.Count() ) )
2076 // Queue an inserted event
2077 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
2079 mImpl->mEventData->mUpdateCursorPosition = true;
2080 if( numberOfCharacters < currentNumberOfCharacters )
2082 mImpl->mEventData->mScrollAfterDelete = true;
2086 mImpl->mEventData->mScrollAfterUpdatePosition = true;
2090 if( maxLengthReached )
2092 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
2094 mImpl->ResetImfManager();
2096 // Do this last since it provides callbacks into application code
2097 mImpl->mControlInterface.MaxLengthReached();
2101 bool Controller::RemoveSelectedText()
2103 bool textRemoved( false );
2105 if( EventData::SELECTING == mImpl->mEventData->mState )
2107 std::string removedString;
2108 mImpl->RetrieveSelection( removedString, true );
2110 if( !removedString.empty() )
2113 mImpl->ChangeState( EventData::EDITING );
2120 void Controller::TapEvent( unsigned int tapCount, float x, float y )
2122 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
2124 if( NULL != mImpl->mEventData )
2126 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
2128 if( 1u == tapCount )
2130 // This is to avoid unnecessary relayouts when tapping an empty text-field
2131 bool relayoutNeeded( false );
2133 if( ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
2134 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
2136 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE ); // If Popup shown hide it here so can be shown again if required.
2139 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
2141 // Already in an active state so show a popup
2142 if( !mImpl->IsClipboardEmpty() )
2144 // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
2145 mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
2149 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2151 relayoutNeeded = true;
2155 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2157 // Hide placeholder text
2161 if( EventData::INACTIVE == mImpl->mEventData->mState )
2163 mImpl->ChangeState( EventData::EDITING );
2165 else if( !mImpl->IsClipboardEmpty() )
2167 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2169 relayoutNeeded = true;
2172 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2173 if( relayoutNeeded )
2175 Event event( Event::TAP_EVENT );
2176 event.p1.mUint = tapCount;
2177 event.p2.mFloat = x;
2178 event.p3.mFloat = y;
2179 mImpl->mEventData->mEventQueue.push_back( event );
2181 mImpl->RequestRelayout();
2184 else if( 2u == tapCount )
2186 if( mImpl->mEventData->mSelectionEnabled &&
2187 mImpl->IsShowingRealText() )
2189 SelectEvent( x, y, false );
2194 // Reset keyboard as tap event has occurred.
2195 mImpl->ResetImfManager();
2198 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2199 // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2201 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2203 if( NULL != mImpl->mEventData )
2205 Event event( Event::PAN_EVENT );
2206 event.p1.mInt = state;
2207 event.p2.mFloat = displacement.x;
2208 event.p3.mFloat = displacement.y;
2209 mImpl->mEventData->mEventQueue.push_back( event );
2211 mImpl->RequestRelayout();
2215 void Controller::LongPressEvent( Gesture::State state, float x, float y )
2217 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2219 if( ( state == Gesture::Started ) &&
2220 ( NULL != mImpl->mEventData ) )
2222 if( !mImpl->IsShowingRealText() )
2224 Event event( Event::LONG_PRESS_EVENT );
2225 event.p1.mInt = state;
2226 mImpl->mEventData->mEventQueue.push_back( event );
2227 mImpl->RequestRelayout();
2231 // The 1st long-press on inactive text-field is treated as tap
2232 if( EventData::INACTIVE == mImpl->mEventData->mState )
2234 mImpl->ChangeState( EventData::EDITING );
2236 Event event( Event::TAP_EVENT );
2238 event.p2.mFloat = x;
2239 event.p3.mFloat = y;
2240 mImpl->mEventData->mEventQueue.push_back( event );
2242 mImpl->RequestRelayout();
2246 // Reset the imf manger to commit the pre-edit before selecting the text.
2247 mImpl->ResetImfManager();
2249 SelectEvent( x, y, false );
2255 void Controller::SelectEvent( float x, float y, bool selectAll )
2257 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2259 if( NULL != mImpl->mEventData )
2261 mImpl->ChangeState( EventData::SELECTING );
2265 Event event( Event::SELECT_ALL );
2266 mImpl->mEventData->mEventQueue.push_back( event );
2270 Event event( Event::SELECT );
2271 event.p2.mFloat = x;
2272 event.p3.mFloat = y;
2273 mImpl->mEventData->mEventQueue.push_back( event );
2276 mImpl->RequestRelayout();
2280 void Controller::GetTargetSize( Vector2& targetSize )
2282 targetSize = mImpl->mVisualModel->mControlSize;
2285 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2287 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2290 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2292 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2294 if( NULL != mImpl->mEventData )
2296 switch( handleType )
2300 Event event( Event::GRAB_HANDLE_EVENT );
2301 event.p1.mUint = state;
2302 event.p2.mFloat = x;
2303 event.p3.mFloat = y;
2305 mImpl->mEventData->mEventQueue.push_back( event );
2308 case LEFT_SELECTION_HANDLE:
2310 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2311 event.p1.mUint = state;
2312 event.p2.mFloat = x;
2313 event.p3.mFloat = y;
2315 mImpl->mEventData->mEventQueue.push_back( event );
2318 case RIGHT_SELECTION_HANDLE:
2320 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2321 event.p1.mUint = state;
2322 event.p2.mFloat = x;
2323 event.p3.mFloat = y;
2325 mImpl->mEventData->mEventQueue.push_back( event );
2328 case LEFT_SELECTION_HANDLE_MARKER:
2329 case RIGHT_SELECTION_HANDLE_MARKER:
2331 // Markers do not move the handles.
2334 case HANDLE_TYPE_COUNT:
2336 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2340 mImpl->RequestRelayout();
2344 void Controller::PasteText( const std::string& stringToPaste )
2346 InsertText( stringToPaste, Text::Controller::COMMIT );
2347 mImpl->ChangeState( EventData::EDITING );
2348 mImpl->RequestRelayout();
2350 // Do this last since it provides callbacks into application code
2351 mImpl->mControlInterface.TextChanged();
2354 void Controller::PasteClipboardItemEvent()
2356 // Retrieve the clipboard contents first
2357 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2358 std::string stringToPaste( notifier.GetContent() );
2360 // Commit the current pre-edit text; the contents of the clipboard should be appended
2361 mImpl->ResetImfManager();
2363 // Temporary disable hiding clipboard
2364 mImpl->SetClipboardHideEnable( false );
2367 PasteText( stringToPaste );
2369 mImpl->SetClipboardHideEnable( true );
2372 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2374 if( NULL == mImpl->mEventData )
2381 case Toolkit::TextSelectionPopup::CUT:
2383 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2384 mImpl->mOperationsPending = ALL_OPERATIONS;
2386 // This is to reset the virtual keyboard to Upper-case
2387 if( 0u == mImpl->mLogicalModel->mText.Count() )
2392 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2393 !mImpl->IsPlaceholderAvailable() )
2395 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2399 ShowPlaceholderText();
2402 mImpl->mEventData->mUpdateCursorPosition = true;
2403 mImpl->mEventData->mScrollAfterDelete = true;
2405 mImpl->RequestRelayout();
2406 mImpl->mControlInterface.TextChanged();
2409 case Toolkit::TextSelectionPopup::COPY:
2411 mImpl->SendSelectionToClipboard( false ); // Text not modified
2412 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
2415 case Toolkit::TextSelectionPopup::PASTE:
2417 std::string stringToPaste("");
2418 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2419 PasteText( stringToPaste );
2422 case Toolkit::TextSelectionPopup::SELECT:
2424 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2426 if( mImpl->mEventData->mSelectionEnabled )
2428 // Creates a SELECT event.
2429 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2433 case Toolkit::TextSelectionPopup::SELECT_ALL:
2435 // Creates a SELECT_ALL event
2436 SelectEvent( 0.f, 0.f, true );
2439 case Toolkit::TextSelectionPopup::CLIPBOARD:
2441 mImpl->ShowClipboard();
2444 case Toolkit::TextSelectionPopup::NONE:
2452 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2454 bool update = false;
2455 bool requestRelayout = false;
2458 unsigned int cursorPosition = 0u;
2460 switch( imfEvent.eventName )
2462 case ImfManager::COMMIT:
2464 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2466 requestRelayout = true;
2469 case ImfManager::PREEDIT:
2471 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2473 requestRelayout = true;
2476 case ImfManager::DELETESURROUNDING:
2478 update = RemoveText( imfEvent.cursorOffset,
2479 imfEvent.numberOfChars,
2480 DONT_UPDATE_INPUT_STYLE );
2484 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2485 !mImpl->IsPlaceholderAvailable() )
2487 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2491 ShowPlaceholderText();
2493 mImpl->mEventData->mUpdateCursorPosition = true;
2494 mImpl->mEventData->mScrollAfterDelete = true;
2496 requestRelayout = true;
2499 case ImfManager::GETSURROUNDING:
2502 cursorPosition = GetLogicalCursorPosition();
2504 imfManager.SetSurroundingText( text );
2505 imfManager.SetCursorPosition( cursorPosition );
2508 case ImfManager::VOID:
2515 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
2518 cursorPosition = GetLogicalCursorPosition();
2521 if( requestRelayout )
2523 mImpl->mOperationsPending = ALL_OPERATIONS;
2524 mImpl->RequestRelayout();
2526 // Do this last since it provides callbacks into application code
2527 mImpl->mControlInterface.TextChanged();
2530 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
2532 return callbackData;
2535 Controller::~Controller()
2540 bool Controller::BackspaceKeyEvent()
2542 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2544 bool removed = false;
2546 if( NULL == mImpl->mEventData )
2551 // IMF manager is no longer handling key-events
2552 mImpl->ClearPreEditFlag();
2554 if( EventData::SELECTING == mImpl->mEventData->mState )
2556 removed = RemoveSelectedText();
2558 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2560 // Remove the character before the current cursor position
2561 removed = RemoveText( -1,
2563 UPDATE_INPUT_STYLE );
2568 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE RemovedText\n", this );
2569 // Notifiy the IMF manager after text changed
2570 // Automatic Upper-case and restarting prediction on an existing word require this.
2573 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2574 !mImpl->IsPlaceholderAvailable() )
2576 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2580 ShowPlaceholderText();
2582 mImpl->mEventData->mUpdateCursorPosition = true;
2583 mImpl->mEventData->mScrollAfterDelete = true;
2589 void Controller::NotifyImfManager()
2591 if( NULL != mImpl->mEventData )
2593 if( mImpl->mEventData->mImfManager )
2595 // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
2598 mImpl->mEventData->mImfManager.SetSurroundingText( text );
2600 mImpl->mEventData->mImfManager.SetCursorPosition( GetLogicalCursorPosition() );
2601 mImpl->mEventData->mImfManager.NotifyCursorPosition();
2606 void Controller::ShowPlaceholderText()
2608 if( mImpl->IsPlaceholderAvailable() )
2610 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2612 if( NULL == mImpl->mEventData )
2617 mImpl->mEventData->mIsShowingPlaceholderText = true;
2619 // Disable handles when showing place-holder text
2620 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2621 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2622 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2624 const char* text( NULL );
2627 // TODO - Switch placeholder text styles when changing state
2628 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2629 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2631 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2632 size = mImpl->mEventData->mPlaceholderTextActive.size();
2636 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2637 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2640 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2641 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2643 // Reset model for showing placeholder.
2644 mImpl->mLogicalModel->mText.Clear();
2645 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2647 // Convert text into UTF-32
2648 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2649 utf32Characters.Resize( size );
2651 // This is a bit horrible but std::string returns a (signed) char*
2652 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2654 // Transform a text array encoded in utf8 into an array encoded in utf32.
2655 // It returns the actual number of characters.
2656 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2657 utf32Characters.Resize( characterCount );
2659 // The characters to be added.
2660 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = characterCount;
2662 // Reset the cursor position
2663 mImpl->mEventData->mPrimaryCursorPosition = 0;
2665 // The natural size needs to be re-calculated.
2666 mImpl->mRecalculateNaturalSize = true;
2668 // Apply modifications to the model
2669 mImpl->mOperationsPending = ALL_OPERATIONS;
2671 // Update the rest of the model during size negotiation
2672 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2676 void Controller::ClearFontData()
2678 if( mImpl->mFontDefaults )
2680 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2683 // Set flags to update the model.
2684 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2685 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2686 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
2688 mImpl->mTextUpdateInfo.mClearAll = true;
2689 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
2690 mImpl->mRecalculateNaturalSize = true;
2692 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2697 UPDATE_ACTUAL_SIZE |
2702 void Controller::ClearStyleData()
2704 mImpl->mLogicalModel->mColorRuns.Clear();
2705 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2708 Controller::Controller( ControlInterface& controlInterface )
2711 mImpl = new Controller::Impl( controlInterface );
2716 } // namespace Toolkit