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.
72 FontDescriptionRun& UpdateSelectionFontStyleRun( EventData* eventData,
73 LogicalModelPtr logicalModel )
75 const bool handlesCrossed = eventData->mLeftSelectionPosition > eventData->mRightSelectionPosition;
77 // Get start and end position of selection
78 const CharacterIndex startOfSelectedText = handlesCrossed ? eventData->mRightSelectionPosition : eventData->mLeftSelectionPosition;
79 const Length lengthOfSelectedText = ( handlesCrossed ? eventData->mLeftSelectionPosition : eventData->mRightSelectionPosition ) - startOfSelectedText;
82 const VectorBase::SizeType numberOfRuns = logicalModel->mFontDescriptionRuns.Count();
83 logicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
85 FontDescriptionRun& fontDescriptionRun = *( logicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
87 fontDescriptionRun.characterRun.characterIndex = startOfSelectedText;
88 fontDescriptionRun.characterRun.numberOfCharacters = lengthOfSelectedText;
90 // Recalculate the selection highlight as the metrics may have changed.
91 eventData->mUpdateLeftSelectionPosition = true;
92 eventData->mUpdateRightSelectionPosition = true;
94 return fontDescriptionRun;
97 ControllerPtr Controller::New( ControlInterface& controlInterface )
99 return ControllerPtr( new Controller( controlInterface ) );
102 void Controller::EnableTextInput( DecoratorPtr decorator )
104 if( NULL == mImpl->mEventData )
106 mImpl->mEventData = new EventData( decorator );
110 void Controller::SetGlyphType( TextAbstraction::GlyphType glyphType )
112 // Metrics for bitmap & vector based glyphs are different
113 mImpl->mMetrics->SetGlyphType( glyphType );
115 // Clear the font-specific data
118 mImpl->mOperationsPending = ALL_OPERATIONS;
119 mImpl->mRecalculateNaturalSize = true;
121 mImpl->RequestRelayout();
124 void Controller::SetMarkupProcessorEnabled( bool enable )
126 mImpl->mMarkupProcessorEnabled = enable;
129 bool Controller::IsMarkupProcessorEnabled() const
131 return mImpl->mMarkupProcessorEnabled;
134 void Controller::SetText( const std::string& text )
136 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
138 // Reset keyboard as text changed
139 mImpl->ResetImfManager();
141 // Remove the previously set text and style.
147 CharacterIndex lastCursorIndex = 0u;
149 if( NULL != mImpl->mEventData )
151 // If popup shown then hide it by switching to Editing state
152 if( ( EventData::SELECTING == mImpl->mEventData->mState ) ||
153 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
154 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
155 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
157 mImpl->ChangeState( EventData::EDITING );
163 MarkupProcessData markupProcessData( mImpl->mLogicalModel->mColorRuns,
164 mImpl->mLogicalModel->mFontDescriptionRuns );
166 Length textSize = 0u;
167 const uint8_t* utf8 = NULL;
168 if( mImpl->mMarkupProcessorEnabled )
170 ProcessMarkupString( text, markupProcessData );
171 textSize = markupProcessData.markupProcessedText.size();
173 // This is a bit horrible but std::string returns a (signed) char*
174 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
178 textSize = text.size();
180 // This is a bit horrible but std::string returns a (signed) char*
181 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
184 // Convert text into UTF-32
185 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
186 utf32Characters.Resize( textSize );
188 // Transform a text array encoded in utf8 into an array encoded in utf32.
189 // It returns the actual number of characters.
190 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
191 utf32Characters.Resize( characterCount );
193 DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
194 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mLogicalModel->mText.Count() );
196 // The characters to be added.
197 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
199 // To reset the cursor position
200 lastCursorIndex = characterCount;
202 // Update the rest of the model during size negotiation
203 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
205 // The natural size needs to be re-calculated.
206 mImpl->mRecalculateNaturalSize = true;
208 // Apply modifications to the model
209 mImpl->mOperationsPending = ALL_OPERATIONS;
213 ShowPlaceholderText();
216 // Resets the cursor position.
217 ResetCursorPosition( lastCursorIndex );
219 // Scrolls the text to make the cursor visible.
220 ResetScrollPosition();
222 mImpl->RequestRelayout();
224 if( NULL != mImpl->mEventData )
226 // Cancel previously queued events
227 mImpl->mEventData->mEventQueue.clear();
230 // Notify IMF as text changed
233 // Do this last since it provides callbacks into application code
234 mImpl->mControlInterface.TextChanged();
237 void Controller::GetText( std::string& text ) const
239 if( !mImpl->IsShowingPlaceholderText() )
241 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
243 if( 0u != utf32Characters.Count() )
245 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
250 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
254 unsigned int Controller::GetLogicalCursorPosition() const
256 if( NULL != mImpl->mEventData )
258 return mImpl->mEventData->mPrimaryCursorPosition;
264 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
266 if( NULL != mImpl->mEventData )
268 if( PLACEHOLDER_TYPE_INACTIVE == type )
270 mImpl->mEventData->mPlaceholderTextInactive = text;
274 mImpl->mEventData->mPlaceholderTextActive = text;
277 // Update placeholder if there is no text
278 if( mImpl->IsShowingPlaceholderText() ||
279 ( 0u == mImpl->mLogicalModel->mText.Count() ) )
281 ShowPlaceholderText();
286 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
288 if( NULL != mImpl->mEventData )
290 if( PLACEHOLDER_TYPE_INACTIVE == type )
292 text = mImpl->mEventData->mPlaceholderTextInactive;
296 text = mImpl->mEventData->mPlaceholderTextActive;
301 void Controller::SetMaximumNumberOfCharacters( Length maxCharacters )
303 mImpl->mMaximumNumberOfCharacters = maxCharacters;
306 int Controller::GetMaximumNumberOfCharacters()
308 return mImpl->mMaximumNumberOfCharacters;
311 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
313 if( NULL == mImpl->mFontDefaults )
315 mImpl->mFontDefaults = new FontDefaults();
318 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
319 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
320 mImpl->mFontDefaults->familyDefined = true;
322 // Clear the font-specific data
325 mImpl->mOperationsPending = ALL_OPERATIONS;
326 mImpl->mRecalculateNaturalSize = true;
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->mOperationsPending = ALL_OPERATIONS;
375 mImpl->mRecalculateNaturalSize = true;
377 mImpl->RequestRelayout();
380 FontWeight Controller::GetDefaultFontWeight() const
382 if( NULL != mImpl->mFontDefaults )
384 return mImpl->mFontDefaults->mFontDescription.weight;
387 return TextAbstraction::FontWeight::NORMAL;
390 void Controller::SetDefaultFontWidth( FontWidth width )
392 if( NULL == mImpl->mFontDefaults )
394 mImpl->mFontDefaults = new FontDefaults();
397 mImpl->mFontDefaults->mFontDescription.width = width;
398 mImpl->mFontDefaults->widthDefined = true;
400 // Clear the font-specific data
403 mImpl->mOperationsPending = ALL_OPERATIONS;
404 mImpl->mRecalculateNaturalSize = true;
406 mImpl->RequestRelayout();
409 FontWidth Controller::GetDefaultFontWidth() const
411 if( NULL != mImpl->mFontDefaults )
413 return mImpl->mFontDefaults->mFontDescription.width;
416 return TextAbstraction::FontWidth::NORMAL;
419 void Controller::SetDefaultFontSlant( FontSlant slant )
421 if( NULL == mImpl->mFontDefaults )
423 mImpl->mFontDefaults = new FontDefaults();
426 mImpl->mFontDefaults->mFontDescription.slant = slant;
427 mImpl->mFontDefaults->slantDefined = true;
429 // Clear the font-specific data
432 mImpl->mOperationsPending = ALL_OPERATIONS;
433 mImpl->mRecalculateNaturalSize = true;
435 mImpl->RequestRelayout();
438 FontSlant Controller::GetDefaultFontSlant() const
440 if( NULL != mImpl->mFontDefaults )
442 return mImpl->mFontDefaults->mFontDescription.slant;
445 return TextAbstraction::FontSlant::NORMAL;
448 void Controller::SetDefaultPointSize( float pointSize )
450 if( NULL == mImpl->mFontDefaults )
452 mImpl->mFontDefaults = new FontDefaults();
455 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
456 mImpl->mFontDefaults->sizeDefined = true;
458 unsigned int horizontalDpi( 0u );
459 unsigned int verticalDpi( 0u );
460 mImpl->mFontClient.GetDpi( horizontalDpi, verticalDpi );
462 // Adjust the metrics if the fixed-size font should be down-scaled
463 int maxEmojiSize( pointSize/POINTS_PER_INCH * verticalDpi );
464 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultPointSize %p setting MaxEmojiSize %d\n", this, maxEmojiSize );
465 mImpl->mMetrics->SetMaxEmojiSize( maxEmojiSize );
467 // Clear the font-specific data
470 mImpl->mOperationsPending = ALL_OPERATIONS;
471 mImpl->mRecalculateNaturalSize = true;
473 mImpl->RequestRelayout();
476 float Controller::GetDefaultPointSize() const
478 if( NULL != mImpl->mFontDefaults )
480 return mImpl->mFontDefaults->mDefaultPointSize;
486 void Controller::UpdateAfterFontChange( const std::string& newDefaultFont )
488 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange");
490 if( !mImpl->mFontDefaults->familyDefined ) // If user defined font then should not update when system font changes
492 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
494 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
495 mImpl->UpdateModel( ALL_OPERATIONS );
496 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
497 mImpl->mRecalculateNaturalSize = true;
498 mImpl->RequestRelayout();
502 void Controller::SetTextColor( const Vector4& textColor )
504 mImpl->mTextColor = textColor;
506 if( !mImpl->IsShowingPlaceholderText() )
508 mImpl->mVisualModel->SetTextColor( textColor );
510 mImpl->RequestRelayout();
514 const Vector4& Controller::GetTextColor() const
516 return mImpl->mTextColor;
519 bool Controller::RemoveText( int cursorOffset, int numberOfCharacters )
521 bool removed = false;
523 if( NULL == mImpl->mEventData )
528 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfCharacters %d\n",
529 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters );
531 if( !mImpl->IsShowingPlaceholderText() )
533 // Delete at current cursor position
534 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
535 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
537 CharacterIndex cursorIndex = oldCursorIndex;
539 // Validate the cursor position & number of characters
540 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
542 cursorIndex = oldCursorIndex + cursorOffset;
545 if( ( cursorIndex + numberOfCharacters ) > currentText.Count() )
547 numberOfCharacters = currentText.Count() - cursorIndex;
550 if( ( cursorIndex + numberOfCharacters ) <= mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters )
552 // Mark the paragraphs to be updated.
553 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
554 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove += numberOfCharacters;
556 // Update the input style and remove the text's style before removing the text.
558 // Set first the default input style.
559 mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
561 // Update the input style.
562 mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
564 // Remove the text's style before removing the text.
565 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfCharacters );
567 // Remove the characters.
568 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
569 Vector<Character>::Iterator last = first + numberOfCharacters;
571 currentText.Erase( first, last );
573 // Cursor position retreat
574 oldCursorIndex = cursorIndex;
576 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfCharacters );
584 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
586 if( NULL != mImpl->mEventData )
588 mImpl->mEventData->mPlaceholderTextColor = textColor;
591 if( mImpl->IsShowingPlaceholderText() )
593 mImpl->mVisualModel->SetTextColor( textColor );
594 mImpl->RequestRelayout();
598 const Vector4& Controller::GetPlaceholderTextColor() const
600 if( NULL != mImpl->mEventData )
602 return mImpl->mEventData->mPlaceholderTextColor;
608 void Controller::SetShadowOffset( const Vector2& shadowOffset )
610 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
612 mImpl->RequestRelayout();
615 const Vector2& Controller::GetShadowOffset() const
617 return mImpl->mVisualModel->GetShadowOffset();
620 void Controller::SetShadowColor( const Vector4& shadowColor )
622 mImpl->mVisualModel->SetShadowColor( shadowColor );
624 mImpl->RequestRelayout();
627 const Vector4& Controller::GetShadowColor() const
629 return mImpl->mVisualModel->GetShadowColor();
632 void Controller::SetUnderlineColor( const Vector4& color )
634 mImpl->mVisualModel->SetUnderlineColor( color );
636 mImpl->RequestRelayout();
639 const Vector4& Controller::GetUnderlineColor() const
641 return mImpl->mVisualModel->GetUnderlineColor();
644 void Controller::SetUnderlineEnabled( bool enabled )
646 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
648 mImpl->RequestRelayout();
651 bool Controller::IsUnderlineEnabled() const
653 return mImpl->mVisualModel->IsUnderlineEnabled();
656 void Controller::SetUnderlineHeight( float height )
658 mImpl->mVisualModel->SetUnderlineHeight( height );
660 mImpl->RequestRelayout();
663 float Controller::GetUnderlineHeight() const
665 return mImpl->mVisualModel->GetUnderlineHeight();
668 void Controller::SetInputColor( const Vector4& color )
670 if( NULL != mImpl->mEventData )
672 mImpl->mEventData->mInputStyle.textColor = color;
674 if( EventData::SELECTING == mImpl->mEventData->mState )
676 const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
678 // Get start and end position of selection
679 const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
680 const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
682 // Add the color run.
683 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
684 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
686 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
687 colorRun.color = color;
688 colorRun.characterRun.characterIndex = startOfSelectedText;
689 colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
691 // Request to relayout.
692 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
693 mImpl->RequestRelayout();
698 const Vector4& Controller::GetInputColor() const
700 if( NULL != mImpl->mEventData )
702 return mImpl->mEventData->mInputStyle.textColor;
705 // Return the default text's color if there is no EventData.
706 return mImpl->mTextColor;
710 void Controller::SetInputFontFamily( const std::string& fontFamily )
712 if( NULL != mImpl->mEventData )
714 mImpl->mEventData->mInputStyle.familyName = fontFamily;
715 mImpl->mEventData->mInputStyle.familyDefined = true;
717 if( EventData::SELECTING == mImpl->mEventData->mState )
719 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
720 mImpl->mLogicalModel );
722 fontDescriptionRun.familyLength = fontFamily.size();
723 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
724 memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
725 fontDescriptionRun.familyDefined = true;
727 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
729 // Request to relayout.
730 mImpl->mOperationsPending = ALL_OPERATIONS;
731 mImpl->mRecalculateNaturalSize = true;
732 mImpl->RequestRelayout();
734 // As the font changes, recalculate the handle positions is needed.
735 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
736 mImpl->mEventData->mUpdateRightSelectionPosition = true;
737 mImpl->mEventData->mScrollAfterUpdatePosition = true;
742 const std::string& Controller::GetInputFontFamily() const
744 if( NULL != mImpl->mEventData )
746 return mImpl->mEventData->mInputStyle.familyName;
749 // Return the default font's family if there is no EventData.
750 return GetDefaultFontFamily();
753 void Controller::SetInputFontStyle( const std::string& fontStyle )
755 if( NULL != mImpl->mEventData )
757 mImpl->mEventData->mInputStyle.fontStyle = fontStyle;
761 const std::string& Controller::GetInputFontStyle() const
763 if( NULL != mImpl->mEventData )
765 return mImpl->mEventData->mInputStyle.fontStyle;
768 // Return the default font's style if there is no EventData.
769 return GetDefaultFontStyle();
772 void Controller::SetInputFontWeight( FontWeight weight )
774 if( NULL != mImpl->mEventData )
776 mImpl->mEventData->mInputStyle.weight = weight;
777 mImpl->mEventData->mInputStyle.weightDefined = true;
779 if( EventData::SELECTING == mImpl->mEventData->mState )
781 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
782 mImpl->mLogicalModel );
784 fontDescriptionRun.weight = weight;
785 fontDescriptionRun.weightDefined = true;
787 // Request to relayout.
788 mImpl->mOperationsPending = ALL_OPERATIONS;
789 mImpl->mRecalculateNaturalSize = true;
790 mImpl->RequestRelayout();
792 // As the font might change, recalculate the handle positions is needed.
793 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
794 mImpl->mEventData->mUpdateRightSelectionPosition = true;
795 mImpl->mEventData->mScrollAfterUpdatePosition = true;
800 FontWeight Controller::GetInputFontWeight() const
802 if( NULL != mImpl->mEventData )
804 return mImpl->mEventData->mInputStyle.weight;
807 return GetDefaultFontWeight();
810 void Controller::SetInputFontWidth( FontWidth width )
812 if( NULL != mImpl->mEventData )
814 mImpl->mEventData->mInputStyle.width = width;
815 mImpl->mEventData->mInputStyle.widthDefined = true;
817 if( EventData::SELECTING == mImpl->mEventData->mState )
819 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
820 mImpl->mLogicalModel );
822 fontDescriptionRun.width = width;
823 fontDescriptionRun.widthDefined = true;
825 // Request to relayout.
826 mImpl->mOperationsPending = ALL_OPERATIONS;
827 mImpl->mRecalculateNaturalSize = true;
828 mImpl->RequestRelayout();
830 // As the font might change, recalculate the handle positions is needed.
831 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
832 mImpl->mEventData->mUpdateRightSelectionPosition = true;
833 mImpl->mEventData->mScrollAfterUpdatePosition = true;
838 FontWidth Controller::GetInputFontWidth() const
840 if( NULL != mImpl->mEventData )
842 return mImpl->mEventData->mInputStyle.width;
845 return GetDefaultFontWidth();
848 void Controller::SetInputFontSlant( FontSlant slant )
850 if( NULL != mImpl->mEventData )
852 mImpl->mEventData->mInputStyle.slant = slant;
853 mImpl->mEventData->mInputStyle.slantDefined = true;
855 if( EventData::SELECTING == mImpl->mEventData->mState )
857 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
858 mImpl->mLogicalModel );
860 fontDescriptionRun.slant = slant;
861 fontDescriptionRun.slantDefined = true;
863 // Request to relayout.
864 mImpl->mOperationsPending = ALL_OPERATIONS;
865 mImpl->mRecalculateNaturalSize = true;
866 mImpl->RequestRelayout();
868 // As the font might change, recalculate the handle positions is needed.
869 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
870 mImpl->mEventData->mUpdateRightSelectionPosition = true;
871 mImpl->mEventData->mScrollAfterUpdatePosition = true;
876 FontSlant Controller::GetInputFontSlant() const
878 if( NULL != mImpl->mEventData )
880 return mImpl->mEventData->mInputStyle.slant;
883 return GetDefaultFontSlant();
886 void Controller::SetInputFontPointSize( float size )
888 if( NULL != mImpl->mEventData )
890 mImpl->mEventData->mInputStyle.size = size;
892 if( EventData::SELECTING == mImpl->mEventData->mState )
894 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
895 mImpl->mLogicalModel );
897 fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
898 fontDescriptionRun.sizeDefined = true;
900 // Request to relayout.
901 mImpl->mOperationsPending = ALL_OPERATIONS;
902 mImpl->mRecalculateNaturalSize = true;
903 mImpl->RequestRelayout();
905 // As the font might change, recalculate the handle positions is needed.
906 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
907 mImpl->mEventData->mUpdateRightSelectionPosition = true;
908 mImpl->mEventData->mScrollAfterUpdatePosition = true;
913 float Controller::GetInputFontPointSize() const
915 if( NULL != mImpl->mEventData )
917 return mImpl->mEventData->mInputStyle.size;
920 // Return the default font's point size if there is no EventData.
921 return GetDefaultPointSize();
924 void Controller::SetEnableCursorBlink( bool enable )
926 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
928 if( NULL != mImpl->mEventData )
930 mImpl->mEventData->mCursorBlinkEnabled = enable;
933 mImpl->mEventData->mDecorator )
935 mImpl->mEventData->mDecorator->StopCursorBlink();
940 bool Controller::GetEnableCursorBlink() const
942 if( NULL != mImpl->mEventData )
944 return mImpl->mEventData->mCursorBlinkEnabled;
950 const Vector2& Controller::GetScrollPosition() const
952 if( NULL != mImpl->mEventData )
954 return mImpl->mEventData->mScrollPosition;
957 return Vector2::ZERO;
960 const Vector2& Controller::GetAlignmentOffset() const
962 return mImpl->mAlignmentOffset;
965 Vector3 Controller::GetNaturalSize()
967 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
970 // Make sure the model is up-to-date before layouting
971 ProcessModifyEvents();
973 if( mImpl->mRecalculateNaturalSize )
975 // Operations that can be done only once until the text changes.
976 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
984 // Make sure the model is up-to-date before layouting
985 mImpl->UpdateModel( onlyOnceOperations );
987 // Layout the text for the new width.
988 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
990 // Set the update info to relayout the whole text.
991 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
992 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
994 // Store the actual control's width.
995 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
997 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
998 static_cast<OperationsMask>( onlyOnceOperations |
1000 naturalSize.GetVectorXY() );
1002 // Do not do again the only once operations.
1003 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1005 // Do the size related operations again.
1006 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1009 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1011 // Stores the natural size to avoid recalculate it again
1012 // unless the text/style changes.
1013 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
1015 mImpl->mRecalculateNaturalSize = false;
1017 // Clear the update info. This info will be set the next time the text is updated.
1018 mImpl->mTextUpdateInfo.Clear();
1020 // Restore the actual control's width.
1021 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1023 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1027 naturalSize = mImpl->mVisualModel->GetNaturalSize();
1029 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1032 naturalSize.x = ConvertToEven( naturalSize.x );
1033 naturalSize.y = ConvertToEven( naturalSize.y );
1038 float Controller::GetHeightForWidth( float width )
1040 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1041 // Make sure the model is up-to-date before layouting
1042 ProcessModifyEvents();
1045 if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
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 );
1060 // Layout the text for the new width.
1061 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1063 // Set the update info to relayout the whole text.
1064 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1065 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1067 // Store the actual control's width.
1068 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
1070 DoRelayout( Size( width, MAX_FLOAT ),
1071 static_cast<OperationsMask>( onlyOnceOperations |
1075 // Do not do again the only once operations.
1076 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1078 // Do the size related operations again.
1079 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1083 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1085 // Clear the update info. This info will be set the next time the text is updated.
1086 mImpl->mTextUpdateInfo.Clear();
1088 // Restore the actual control's width.
1089 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1091 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1095 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1096 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1099 return layoutSize.height;
1102 bool Controller::Relayout( const Size& size )
1104 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
1106 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1108 bool glyphsRemoved( false );
1109 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1111 mImpl->mVisualModel->mGlyphPositions.Clear();
1112 glyphsRemoved = true;
1115 // Clear the update info. This info will be set the next time the text is updated.
1116 mImpl->mTextUpdateInfo.Clear();
1118 // Not worth to relayout if width or height is equal to zero.
1119 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1120 return glyphsRemoved;
1123 // Whether a new size has been set.
1124 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1128 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1130 // Layout operations that need to be done if the size changes.
1131 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1134 UPDATE_ACTUAL_SIZE |
1136 // Set the update info to relayout the whole text.
1137 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
1138 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1141 // Whether there are modify events.
1142 const bool isModifyEventsEmpty = 0u == mImpl->mModifyEvents.Count();
1144 // Make sure the model is up-to-date before layouting.
1145 ProcessModifyEvents();
1146 mImpl->UpdateModel( mImpl->mOperationsPending );
1148 // Style operations that need to be done if the text is modified.
1149 if( !isModifyEventsEmpty )
1151 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1155 // Apply the style runs if text is modified.
1156 bool updated = mImpl->UpdateModelStyle( mImpl->mOperationsPending );
1160 updated = DoRelayout( size,
1161 mImpl->mOperationsPending,
1162 layoutSize ) || updated;
1164 // Do not re-do any operation until something changes.
1165 mImpl->mOperationsPending = NO_OPERATION;
1167 // Whether the text control is editable
1168 const bool isEditable = NULL != mImpl->mEventData;
1170 // Keep the current offset and alignment as it will be used to update the decorator's positions (if the size changes).
1172 if( newSize && isEditable )
1174 offset = mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition;
1177 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
1178 CalculateTextAlignment( size );
1184 // If there is a new size, the scroll position needs to be clamped.
1185 mImpl->ClampHorizontalScroll( layoutSize );
1187 // Update the decorator's positions is needed if there is a new size.
1188 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
1191 // Move the cursor, grab handle etc.
1192 updated = mImpl->ProcessInputEvents() || updated;
1195 // Clear the update info. This info will be set the next time the text is updated.
1196 mImpl->mTextUpdateInfo.Clear();
1198 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1202 void Controller::ProcessModifyEvents()
1204 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
1206 if( 0u == events.Count() )
1212 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
1213 endIt = events.End();
1217 const ModifyEvent& event = *it;
1219 if( ModifyEvent::TEXT_REPLACED == event.type )
1221 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
1222 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
1224 TextReplacedEvent();
1226 else if( ModifyEvent::TEXT_INSERTED == event.type )
1228 TextInsertedEvent();
1230 else if( ModifyEvent::TEXT_DELETED == event.type )
1232 // Placeholder-text cannot be deleted
1233 if( !mImpl->IsShowingPlaceholderText() )
1240 if( NULL != mImpl->mEventData )
1242 // When the text is being modified, delay cursor blinking
1243 mImpl->mEventData->mDecorator->DelayCursorBlink();
1246 // Discard temporary text
1250 void Controller::ResetText()
1253 mImpl->mLogicalModel->mText.Clear();
1255 // We have cleared everything including the placeholder-text
1256 mImpl->PlaceholderCleared();
1258 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1259 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1260 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
1262 // Clear any previous text.
1263 mImpl->mTextUpdateInfo.mClearAll = true;
1265 // The natural size needs to be re-calculated.
1266 mImpl->mRecalculateNaturalSize = true;
1268 // Apply modifications to the model
1269 mImpl->mOperationsPending = ALL_OPERATIONS;
1272 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
1274 // Reset the cursor position
1275 if( NULL != mImpl->mEventData )
1277 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
1279 // Update the cursor if it's in editing mode.
1280 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1282 mImpl->mEventData->mUpdateCursorPosition = true;
1287 void Controller::ResetScrollPosition()
1289 if( NULL != mImpl->mEventData )
1291 // Reset the scroll position.
1292 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
1293 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1297 void Controller::TextReplacedEvent()
1299 // The natural size needs to be re-calculated.
1300 mImpl->mRecalculateNaturalSize = true;
1302 // Apply modifications to the model
1303 mImpl->mOperationsPending = ALL_OPERATIONS;
1306 void Controller::TextInsertedEvent()
1308 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
1310 if( NULL == mImpl->mEventData )
1315 // The natural size needs to be re-calculated.
1316 mImpl->mRecalculateNaturalSize = true;
1318 // Apply modifications to the model; TODO - Optimize this
1319 mImpl->mOperationsPending = ALL_OPERATIONS;
1321 // Queue a cursor reposition event; this must wait until after DoRelayout()
1322 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1324 mImpl->mEventData->mUpdateCursorPosition = true;
1325 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1329 void Controller::TextDeletedEvent()
1331 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
1333 if( NULL == mImpl->mEventData )
1338 // The natural size needs to be re-calculated.
1339 mImpl->mRecalculateNaturalSize = true;
1341 // Apply modifications to the model; TODO - Optimize this
1342 mImpl->mOperationsPending = ALL_OPERATIONS;
1344 // Queue a cursor reposition event; this must wait until after DoRelayout()
1345 mImpl->mEventData->mUpdateCursorPosition = true;
1346 if( 0u != mImpl->mLogicalModel->mText.Count() )
1348 mImpl->mEventData->mScrollAfterDelete = true;
1352 bool Controller::DoRelayout( const Size& size,
1353 OperationsMask operationsRequired,
1356 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
1357 bool viewUpdated( false );
1359 // Calculate the operations to be done.
1360 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
1362 const CharacterIndex startIndex = mImpl->mTextUpdateInfo.mParagraphCharacterIndex;
1363 const Length requestedNumberOfCharacters = mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters;
1365 if( LAYOUT & operations )
1367 // Some vectors with data needed to layout and reorder may be void
1368 // after the first time the text has been laid out.
1369 // Fill the vectors again.
1371 // Calculate the number of glyphs to layout.
1372 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1373 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1374 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
1375 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
1377 const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
1378 const GlyphIndex startGlyphIndex = mImpl->mTextUpdateInfo.mStartGlyphIndex;
1379 const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
1380 const Length totalNumberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
1382 if( 0u == totalNumberOfGlyphs )
1384 if( UPDATE_ACTUAL_SIZE & operations )
1386 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
1389 // Nothing else to do if there is no glyphs.
1390 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1394 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1395 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1396 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1397 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1398 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1399 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1400 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1402 // Set the layout parameters.
1403 LayoutParameters layoutParameters( size,
1405 lineBreakInfo.Begin(),
1406 wordBreakInfo.Begin(),
1407 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1409 glyphsToCharactersMap.Begin(),
1410 charactersPerGlyph.Begin(),
1411 charactersToGlyphBuffer,
1412 glyphsPerCharacterBuffer,
1413 totalNumberOfGlyphs );
1415 // Resize the vector of positions to have the same size than the vector of glyphs.
1416 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1417 glyphPositions.Resize( totalNumberOfGlyphs );
1419 // Whether the last character is a new paragraph character.
1420 mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1421 layoutParameters.isLastNewParagraph = mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph;
1423 // The initial glyph and the number of glyphs to layout.
1424 layoutParameters.startGlyphIndex = startGlyphIndex;
1425 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1426 layoutParameters.startLineIndex = mImpl->mTextUpdateInfo.mStartLineIndex;
1427 layoutParameters.estimatedNumberOfLines = mImpl->mTextUpdateInfo.mEstimatedNumberOfLines;
1429 // Update the visual model.
1430 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1432 mImpl->mVisualModel->mLines,
1437 // Reorder the lines
1438 if( REORDER & operations )
1440 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1441 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1443 // Check first if there are paragraphs with bidirectional info.
1444 if( 0u != bidirectionalInfo.Count() )
1447 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1449 // Reorder the lines.
1450 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1451 ReorderLines( bidirectionalInfo,
1453 requestedNumberOfCharacters,
1454 mImpl->mVisualModel->mLines,
1455 bidirectionalLineInfo );
1457 // Set the bidirectional info per line into the layout parameters.
1458 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1459 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1461 // TODO: update the conversion map instead creating it from scratch.
1462 // Note this tables store indices to characters, so update the table means modify all the indices of the text after the last updated character.
1463 // It's better to refactor this. Store this table per line and don't update the indices.
1464 // For the cursor position probably is better to use the function instead creating a table.
1465 // Set the bidirectional info into the model.
1466 mImpl->mLogicalModel->SetVisualToLogicalMap( layoutParameters.lineBidirectionalInfoRunsBuffer,
1467 layoutParameters.numberOfBidirectionalInfoRuns,
1469 mImpl->mLogicalModel->mText.Count() );
1471 // Re-layout the text. Reorder those lines with right to left characters.
1472 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1474 requestedNumberOfCharacters,
1480 // Sets the actual size.
1481 if( UPDATE_ACTUAL_SIZE & operations )
1483 mImpl->mVisualModel->SetLayoutSize( layoutSize );
1487 // Store the size used to layout the text.
1488 mImpl->mVisualModel->mControlSize = size;
1492 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1495 if( ALIGN & operations )
1497 // The laid-out lines.
1498 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1500 mImpl->mLayoutEngine.Align( size,
1502 requestedNumberOfCharacters,
1508 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1512 void Controller::SetMultiLineEnabled( bool enable )
1514 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1516 if( layout != mImpl->mLayoutEngine.GetLayout() )
1518 // Set the layout type.
1519 mImpl->mLayoutEngine.SetLayout( layout );
1521 // Set the flags to redo the layout operations
1522 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1523 UPDATE_ACTUAL_SIZE |
1527 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1529 mImpl->RequestRelayout();
1533 bool Controller::IsMultiLineEnabled() const
1535 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1538 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1540 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1542 // Set the alignment.
1543 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1545 // Set the flag to redo the alignment operation.
1546 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1548 mImpl->RequestRelayout();
1552 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1554 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1557 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1559 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1561 // Set the alignment.
1562 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1564 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1566 mImpl->RequestRelayout();
1570 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1572 return mImpl->mLayoutEngine.GetVerticalAlignment();
1575 void Controller::CalculateTextAlignment( const Size& controlSize )
1577 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1579 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1581 // Get the line height of the default font.
1582 layoutSize.height = mImpl->GetDefaultFontLineHeight();
1585 if( LayoutEngine::SINGLE_LINE_BOX == mImpl->mLayoutEngine.GetLayout() )
1587 // Get the direction of the first character.
1588 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1590 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1591 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1592 if( firstParagraphDirection )
1594 switch( horizontalAlignment )
1596 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1598 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1601 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1606 case LayoutEngine::HORIZONTAL_ALIGN_END:
1608 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1614 switch( horizontalAlignment )
1616 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1618 mImpl->mAlignmentOffset.x = 0.f;
1621 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1623 mImpl->mAlignmentOffset.x = floorf( 0.5f * ( controlSize.width - layoutSize.width ) ); // try to avoid pixel alignment.
1626 case LayoutEngine::HORIZONTAL_ALIGN_END:
1628 mImpl->mAlignmentOffset.x = controlSize.width - layoutSize.width;
1634 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1635 switch( verticalAlignment )
1637 case LayoutEngine::VERTICAL_ALIGN_TOP:
1639 mImpl->mAlignmentOffset.y = 0.f;
1642 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1644 mImpl->mAlignmentOffset.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
1647 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1649 mImpl->mAlignmentOffset.y = controlSize.height - layoutSize.height;
1655 LayoutEngine& Controller::GetLayoutEngine()
1657 return mImpl->mLayoutEngine;
1660 View& Controller::GetView()
1662 return mImpl->mView;
1665 void Controller::KeyboardFocusGainEvent()
1667 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1669 if( NULL != mImpl->mEventData )
1671 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1672 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1674 mImpl->ChangeState( EventData::EDITING );
1675 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1678 if( mImpl->IsShowingPlaceholderText() )
1680 // Show alternative placeholder-text when editing
1681 ShowPlaceholderText();
1684 mImpl->RequestRelayout();
1688 void Controller::KeyboardFocusLostEvent()
1690 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1692 if( NULL != mImpl->mEventData )
1694 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1696 mImpl->ChangeState( EventData::INACTIVE );
1698 if( !mImpl->IsShowingRealText() )
1700 // Revert to regular placeholder-text when not editing
1701 ShowPlaceholderText();
1705 mImpl->RequestRelayout();
1708 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1710 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1712 bool textChanged( false );
1714 if( ( NULL != mImpl->mEventData ) &&
1715 ( keyEvent.state == KeyEvent::Down ) )
1717 int keyCode = keyEvent.keyCode;
1718 const std::string& keyString = keyEvent.keyPressed;
1720 // Pre-process to separate modifying events from non-modifying input events.
1721 if( Dali::DALI_KEY_ESCAPE == keyCode )
1723 // Escape key is a special case which causes focus loss
1724 KeyboardFocusLostEvent();
1726 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
1727 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1728 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
1729 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
1731 Event event( Event::CURSOR_KEY_EVENT );
1732 event.p1.mInt = keyCode;
1733 mImpl->mEventData->mEventQueue.push_back( event );
1735 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1737 textChanged = BackspaceKeyEvent();
1739 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1741 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1742 // Avoids calling the InsertText() method which can delete selected text
1744 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1745 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1747 mImpl->ChangeState( EventData::INACTIVE );
1748 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1749 // Avoids calling the InsertText() method which can delete selected text
1751 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1753 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1754 // and a character is typed after the type of a upper case latin character.
1760 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1762 // IMF manager is no longer handling key-events
1763 mImpl->ClearPreEditFlag();
1765 InsertText( keyString, COMMIT );
1769 if( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1770 ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1772 mImpl->ChangeState( EventData::EDITING );
1775 mImpl->RequestRelayout();
1780 // Do this last since it provides callbacks into application code
1781 mImpl->mControlInterface.TextChanged();
1787 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1789 bool removedPrevious( false );
1790 bool maxLengthReached( false );
1792 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1794 if( NULL == mImpl->mEventData )
1799 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1800 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1801 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1803 // TODO: At the moment the underline runs are only for pre-edit.
1804 mImpl->mVisualModel->mUnderlineRuns.Clear();
1806 Vector<Character> utf32Characters;
1807 Length characterCount( 0u );
1809 // Remove the previous IMF pre-edit (predicitive text)
1810 if( mImpl->mEventData->mPreEditFlag &&
1811 ( 0u != mImpl->mEventData->mPreEditLength ) )
1813 const CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1815 removedPrevious = RemoveText( -static_cast<int>( offset ), mImpl->mEventData->mPreEditLength );
1817 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1818 mImpl->mEventData->mPreEditLength = 0u;
1822 // Remove the previous Selection
1823 removedPrevious = RemoveSelectedText();
1828 // Convert text into UTF-32
1829 utf32Characters.Resize( text.size() );
1831 // This is a bit horrible but std::string returns a (signed) char*
1832 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1834 // Transform a text array encoded in utf8 into an array encoded in utf32.
1835 // It returns the actual number of characters.
1836 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1837 utf32Characters.Resize( characterCount );
1839 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1840 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1843 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1845 // The placeholder text is no longer needed
1846 if( mImpl->IsShowingPlaceholderText() )
1851 mImpl->ChangeState( EventData::EDITING );
1853 // Handle the IMF (predicitive text) state changes
1854 if( COMMIT == type )
1856 // IMF manager is no longer handling key-events
1857 mImpl->ClearPreEditFlag();
1861 if( !mImpl->mEventData->mPreEditFlag )
1863 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1865 // Record the start of the pre-edit text
1866 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1869 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1870 mImpl->mEventData->mPreEditFlag = true;
1872 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1875 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1877 // Restrict new text to fit within Maximum characters setting.
1878 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1879 maxLengthReached = ( characterCount > maxSizeOfNewText );
1881 // The cursor position.
1882 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1884 // Update the text's style.
1886 // Updates the text style runs by adding characters.
1887 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
1889 // Get the character index from the cursor index.
1890 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
1892 // Retrieve the text's style for the given index.
1894 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
1896 // Whether to add a new text color run.
1897 const bool addColorRun = style.textColor != mImpl->mEventData->mInputStyle.textColor;
1899 // Whether to add a new font run.
1900 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
1901 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
1902 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
1903 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
1904 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
1909 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
1910 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
1912 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
1913 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
1914 colorRun.characterRun.characterIndex = cursorIndex;
1915 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1918 if( addFontNameRun ||
1924 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
1925 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
1927 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
1929 if( addFontNameRun )
1931 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
1932 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
1933 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
1934 fontDescriptionRun.familyDefined = true;
1936 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
1939 if( addFontWeightRun )
1941 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
1942 fontDescriptionRun.weightDefined = true;
1945 if( addFontWidthRun )
1947 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
1948 fontDescriptionRun.widthDefined = true;
1951 if( addFontSlantRun )
1953 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
1954 fontDescriptionRun.slantDefined = true;
1957 if( addFontSizeRun )
1959 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
1960 fontDescriptionRun.sizeDefined = true;
1963 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
1964 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1967 // Insert at current cursor position.
1968 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1970 if( cursorIndex < numberOfCharactersInModel )
1972 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1976 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1979 // Mark the first paragraph to be updated.
1980 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
1981 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
1983 // Update the cursor index.
1984 cursorIndex += maxSizeOfNewText;
1986 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1989 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
1990 mImpl->IsPlaceholderAvailable() )
1992 // Show place-holder if empty after removing the pre-edit text
1993 ShowPlaceholderText();
1994 mImpl->mEventData->mUpdateCursorPosition = true;
1995 mImpl->ClearPreEditFlag();
1997 else if( removedPrevious ||
1998 ( 0 != utf32Characters.Count() ) )
2000 // Queue an inserted event
2001 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
2004 if( maxLengthReached )
2006 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
2008 mImpl->ResetImfManager();
2010 // Do this last since it provides callbacks into application code
2011 mImpl->mControlInterface.MaxLengthReached();
2015 bool Controller::RemoveSelectedText()
2017 bool textRemoved( false );
2019 if( EventData::SELECTING == mImpl->mEventData->mState )
2021 std::string removedString;
2022 mImpl->RetrieveSelection( removedString, true );
2024 if( !removedString.empty() )
2027 mImpl->ChangeState( EventData::EDITING );
2034 void Controller::TapEvent( unsigned int tapCount, float x, float y )
2036 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
2038 if( NULL != mImpl->mEventData )
2040 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
2042 if( 1u == tapCount )
2044 // This is to avoid unnecessary relayouts when tapping an empty text-field
2045 bool relayoutNeeded( false );
2047 if( ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) ||
2048 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
2050 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE); // If Popup shown hide it here so can be shown again if required.
2053 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
2055 // Already in an active state so show a popup
2056 if( !mImpl->IsClipboardEmpty() )
2058 // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
2059 mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
2063 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2065 relayoutNeeded = true;
2069 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2071 // Hide placeholder text
2075 if( EventData::INACTIVE == mImpl->mEventData->mState )
2077 mImpl->ChangeState( EventData::EDITING );
2079 else if( !mImpl->IsClipboardEmpty() )
2081 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2083 relayoutNeeded = true;
2086 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2087 if( relayoutNeeded )
2089 Event event( Event::TAP_EVENT );
2090 event.p1.mUint = tapCount;
2091 event.p2.mFloat = x;
2092 event.p3.mFloat = y;
2093 mImpl->mEventData->mEventQueue.push_back( event );
2095 mImpl->RequestRelayout();
2098 else if( 2u == tapCount )
2100 if( mImpl->mEventData->mSelectionEnabled &&
2101 mImpl->IsShowingRealText() )
2103 SelectEvent( x, y, false );
2108 // Reset keyboard as tap event has occurred.
2109 mImpl->ResetImfManager();
2112 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2113 // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2115 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2117 if( NULL != mImpl->mEventData )
2119 Event event( Event::PAN_EVENT );
2120 event.p1.mInt = state;
2121 event.p2.mFloat = displacement.x;
2122 event.p3.mFloat = displacement.y;
2123 mImpl->mEventData->mEventQueue.push_back( event );
2125 mImpl->RequestRelayout();
2129 void Controller::LongPressEvent( Gesture::State state, float x, float y )
2131 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2133 if( ( state == Gesture::Started ) &&
2134 ( NULL != mImpl->mEventData ) )
2136 if( !mImpl->IsShowingRealText() )
2138 Event event( Event::LONG_PRESS_EVENT );
2139 event.p1.mInt = state;
2140 mImpl->mEventData->mEventQueue.push_back( event );
2141 mImpl->RequestRelayout();
2145 // The 1st long-press on inactive text-field is treated as tap
2146 if( EventData::INACTIVE == mImpl->mEventData->mState )
2148 mImpl->ChangeState( EventData::EDITING );
2150 Event event( Event::TAP_EVENT );
2152 event.p2.mFloat = x;
2153 event.p3.mFloat = y;
2154 mImpl->mEventData->mEventQueue.push_back( event );
2156 mImpl->RequestRelayout();
2160 // Reset the imf manger to commit the pre-edit before selecting the text.
2161 mImpl->ResetImfManager();
2163 SelectEvent( x, y, false );
2169 void Controller::SelectEvent( float x, float y, bool selectAll )
2171 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2173 if( NULL != mImpl->mEventData )
2175 mImpl->ChangeState( EventData::SELECTING );
2179 Event event( Event::SELECT_ALL );
2180 mImpl->mEventData->mEventQueue.push_back( event );
2184 Event event( Event::SELECT );
2185 event.p2.mFloat = x;
2186 event.p3.mFloat = y;
2187 mImpl->mEventData->mEventQueue.push_back( event );
2190 mImpl->RequestRelayout();
2194 void Controller::GetTargetSize( Vector2& targetSize )
2196 targetSize = mImpl->mVisualModel->mControlSize;
2199 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2201 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2204 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2206 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2208 if( NULL != mImpl->mEventData )
2210 switch( handleType )
2214 Event event( Event::GRAB_HANDLE_EVENT );
2215 event.p1.mUint = state;
2216 event.p2.mFloat = x;
2217 event.p3.mFloat = y;
2219 mImpl->mEventData->mEventQueue.push_back( event );
2222 case LEFT_SELECTION_HANDLE:
2224 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2225 event.p1.mUint = state;
2226 event.p2.mFloat = x;
2227 event.p3.mFloat = y;
2229 mImpl->mEventData->mEventQueue.push_back( event );
2232 case RIGHT_SELECTION_HANDLE:
2234 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2235 event.p1.mUint = state;
2236 event.p2.mFloat = x;
2237 event.p3.mFloat = y;
2239 mImpl->mEventData->mEventQueue.push_back( event );
2242 case LEFT_SELECTION_HANDLE_MARKER:
2243 case RIGHT_SELECTION_HANDLE_MARKER:
2245 // Markers do not move the handles.
2248 case HANDLE_TYPE_COUNT:
2250 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2254 mImpl->RequestRelayout();
2258 void Controller::PasteText( const std::string& stringToPaste )
2260 InsertText( stringToPaste, Text::Controller::COMMIT );
2261 mImpl->ChangeState( EventData::EDITING );
2262 mImpl->RequestRelayout();
2264 // Do this last since it provides callbacks into application code
2265 mImpl->mControlInterface.TextChanged();
2268 void Controller::PasteClipboardItemEvent()
2270 // Retrieve the clipboard contents first
2271 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2272 std::string stringToPaste( notifier.GetContent() );
2274 // Commit the current pre-edit text; the contents of the clipboard should be appended
2275 mImpl->ResetImfManager();
2278 PasteText( stringToPaste );
2281 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2283 if( NULL == mImpl->mEventData )
2290 case Toolkit::TextSelectionPopup::CUT:
2292 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2293 mImpl->mOperationsPending = ALL_OPERATIONS;
2295 // This is to reset the virtual keyboard to Upper-case
2296 if( 0u == mImpl->mLogicalModel->mText.Count() )
2301 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2302 !mImpl->IsPlaceholderAvailable() )
2304 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2308 ShowPlaceholderText();
2309 mImpl->mEventData->mUpdateCursorPosition = true;
2311 mImpl->RequestRelayout();
2312 mImpl->mControlInterface.TextChanged();
2315 case Toolkit::TextSelectionPopup::COPY:
2317 mImpl->SendSelectionToClipboard( false ); // Text not modified
2318 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
2321 case Toolkit::TextSelectionPopup::PASTE:
2323 std::string stringToPaste("");
2324 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2325 PasteText( stringToPaste );
2328 case Toolkit::TextSelectionPopup::SELECT:
2330 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2332 if( mImpl->mEventData->mSelectionEnabled )
2334 // Creates a SELECT event.
2335 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2339 case Toolkit::TextSelectionPopup::SELECT_ALL:
2341 // Creates a SELECT_ALL event
2342 SelectEvent( 0.f, 0.f, true );
2345 case Toolkit::TextSelectionPopup::CLIPBOARD:
2347 mImpl->ShowClipboard();
2350 case Toolkit::TextSelectionPopup::NONE:
2358 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2360 bool update = false;
2361 bool requestRelayout = false;
2364 unsigned int cursorPosition = 0u;
2366 switch( imfEvent.eventName )
2368 case ImfManager::COMMIT:
2370 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2372 requestRelayout = true;
2375 case ImfManager::PREEDIT:
2377 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2379 requestRelayout = true;
2382 case ImfManager::DELETESURROUNDING:
2384 update = RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
2388 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2389 !mImpl->IsPlaceholderAvailable() )
2391 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2395 ShowPlaceholderText();
2396 mImpl->mEventData->mUpdateCursorPosition = true;
2399 requestRelayout = true;
2402 case ImfManager::GETSURROUNDING:
2405 cursorPosition = GetLogicalCursorPosition();
2407 imfManager.SetSurroundingText( text );
2408 imfManager.SetCursorPosition( cursorPosition );
2411 case ImfManager::VOID:
2418 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
2421 cursorPosition = GetLogicalCursorPosition();
2424 if( requestRelayout )
2426 mImpl->mOperationsPending = ALL_OPERATIONS;
2427 mImpl->RequestRelayout();
2429 // Do this last since it provides callbacks into application code
2430 mImpl->mControlInterface.TextChanged();
2433 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
2435 return callbackData;
2438 Controller::~Controller()
2443 bool Controller::BackspaceKeyEvent()
2445 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2447 bool removed = false;
2449 if( NULL == mImpl->mEventData )
2454 // IMF manager is no longer handling key-events
2455 mImpl->ClearPreEditFlag();
2457 if( EventData::SELECTING == mImpl->mEventData->mState )
2459 removed = RemoveSelectedText();
2461 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2463 // Remove the character before the current cursor position
2464 removed = RemoveText( -1, 1 );
2469 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE RemovedText\n", this );
2470 // Notifiy the IMF manager after text changed
2471 // Automatic Upper-case and restarting prediction on an existing word require this.
2474 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2475 !mImpl->IsPlaceholderAvailable() )
2477 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2481 ShowPlaceholderText();
2482 mImpl->mEventData->mUpdateCursorPosition = true;
2489 void Controller::NotifyImfManager()
2491 if( NULL != mImpl->mEventData )
2493 if( mImpl->mEventData->mImfManager )
2495 // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
2498 mImpl->mEventData->mImfManager.SetSurroundingText( text );
2500 mImpl->mEventData->mImfManager.SetCursorPosition( GetLogicalCursorPosition() );
2501 mImpl->mEventData->mImfManager.NotifyCursorPosition();
2506 void Controller::ShowPlaceholderText()
2508 if( mImpl->IsPlaceholderAvailable() )
2510 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2512 if( NULL == mImpl->mEventData )
2517 mImpl->mEventData->mIsShowingPlaceholderText = true;
2519 // Disable handles when showing place-holder text
2520 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2521 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2522 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2524 const char* text( NULL );
2527 // TODO - Switch placeholder text styles when changing state
2528 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2529 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2531 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2532 size = mImpl->mEventData->mPlaceholderTextActive.size();
2536 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2537 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2540 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2541 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2543 // Reset model for showing placeholder.
2544 mImpl->mLogicalModel->mText.Clear();
2545 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2547 // Convert text into UTF-32
2548 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2549 utf32Characters.Resize( size );
2551 // This is a bit horrible but std::string returns a (signed) char*
2552 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2554 // Transform a text array encoded in utf8 into an array encoded in utf32.
2555 // It returns the actual number of characters.
2556 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2557 utf32Characters.Resize( characterCount );
2559 // The characters to be added.
2560 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = characterCount;
2562 // Reset the cursor position
2563 mImpl->mEventData->mPrimaryCursorPosition = 0;
2565 // The natural size needs to be re-calculated.
2566 mImpl->mRecalculateNaturalSize = true;
2568 // Apply modifications to the model
2569 mImpl->mOperationsPending = ALL_OPERATIONS;
2571 // Update the rest of the model during size negotiation
2572 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2576 void Controller::ClearFontData()
2578 if( mImpl->mFontDefaults )
2580 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2582 mImpl->mLogicalModel->mFontRuns.Clear();
2583 mImpl->mVisualModel->mGlyphs.Clear();
2584 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2585 mImpl->mVisualModel->mCharactersToGlyph.Clear();
2586 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2587 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2588 mImpl->mVisualModel->mGlyphPositions.Clear();
2589 mImpl->mVisualModel->mLines.Clear();
2590 mImpl->mVisualModel->ClearCaches();
2593 void Controller::ClearStyleData()
2595 mImpl->mLogicalModel->mColorRuns.Clear();
2596 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2599 Controller::Controller( ControlInterface& controlInterface )
2602 mImpl = new Controller::Impl( controlInterface );
2607 } // namespace Toolkit