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->mRecalculateNaturalSize = true;
120 mImpl->RequestRelayout();
123 void Controller::SetMarkupProcessorEnabled( bool enable )
125 mImpl->mMarkupProcessorEnabled = enable;
128 bool Controller::IsMarkupProcessorEnabled() const
130 return mImpl->mMarkupProcessorEnabled;
133 void Controller::SetText( const std::string& text )
135 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
137 // Reset keyboard as text changed
138 mImpl->ResetImfManager();
140 // Remove the previously set text and style.
146 CharacterIndex lastCursorIndex = 0u;
148 if( NULL != mImpl->mEventData )
150 // If popup shown then hide it by switching to Editing state
151 if( ( EventData::SELECTING == mImpl->mEventData->mState ) ||
152 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
153 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
154 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
156 mImpl->ChangeState( EventData::EDITING );
162 MarkupProcessData markupProcessData( mImpl->mLogicalModel->mColorRuns,
163 mImpl->mLogicalModel->mFontDescriptionRuns );
165 Length textSize = 0u;
166 const uint8_t* utf8 = NULL;
167 if( mImpl->mMarkupProcessorEnabled )
169 ProcessMarkupString( text, markupProcessData );
170 textSize = markupProcessData.markupProcessedText.size();
172 // This is a bit horrible but std::string returns a (signed) char*
173 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
177 textSize = text.size();
179 // This is a bit horrible but std::string returns a (signed) char*
180 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
183 // Convert text into UTF-32
184 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
185 utf32Characters.Resize( textSize );
187 // Transform a text array encoded in utf8 into an array encoded in utf32.
188 // It returns the actual number of characters.
189 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
190 utf32Characters.Resize( characterCount );
192 DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
193 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mLogicalModel->mText.Count() );
195 // The characters to be added.
196 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
198 // To reset the cursor position
199 lastCursorIndex = characterCount;
201 // Update the rest of the model during size negotiation
202 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
204 // The natural size needs to be re-calculated.
205 mImpl->mRecalculateNaturalSize = true;
207 // Apply modifications to the model
208 mImpl->mOperationsPending = ALL_OPERATIONS;
212 ShowPlaceholderText();
215 // Resets the cursor position.
216 ResetCursorPosition( lastCursorIndex );
218 // Scrolls the text to make the cursor visible.
219 ResetScrollPosition();
221 mImpl->RequestRelayout();
223 if( NULL != mImpl->mEventData )
225 // Cancel previously queued events
226 mImpl->mEventData->mEventQueue.clear();
229 // Notify IMF as text changed
232 // Do this last since it provides callbacks into application code
233 mImpl->mControlInterface.TextChanged();
236 void Controller::GetText( std::string& text ) const
238 if( !mImpl->IsShowingPlaceholderText() )
240 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
242 if( 0u != utf32Characters.Count() )
244 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
249 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
253 unsigned int Controller::GetLogicalCursorPosition() const
255 if( NULL != mImpl->mEventData )
257 return mImpl->mEventData->mPrimaryCursorPosition;
263 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
265 if( NULL != mImpl->mEventData )
267 if( PLACEHOLDER_TYPE_INACTIVE == type )
269 mImpl->mEventData->mPlaceholderTextInactive = text;
273 mImpl->mEventData->mPlaceholderTextActive = text;
276 // Update placeholder if there is no text
277 if( mImpl->IsShowingPlaceholderText() ||
278 ( 0u == mImpl->mLogicalModel->mText.Count() ) )
280 ShowPlaceholderText();
285 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
287 if( NULL != mImpl->mEventData )
289 if( PLACEHOLDER_TYPE_INACTIVE == type )
291 text = mImpl->mEventData->mPlaceholderTextInactive;
295 text = mImpl->mEventData->mPlaceholderTextActive;
300 void Controller::SetMaximumNumberOfCharacters( Length maxCharacters )
302 mImpl->mMaximumNumberOfCharacters = maxCharacters;
305 int Controller::GetMaximumNumberOfCharacters()
307 return mImpl->mMaximumNumberOfCharacters;
310 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
312 if( NULL == mImpl->mFontDefaults )
314 mImpl->mFontDefaults = new FontDefaults();
317 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
318 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
319 mImpl->mFontDefaults->familyDefined = true;
321 // Clear the font-specific data
324 mImpl->mRecalculateNaturalSize = true;
326 mImpl->RequestRelayout();
329 const std::string& Controller::GetDefaultFontFamily() const
331 if( NULL != mImpl->mFontDefaults )
333 return mImpl->mFontDefaults->mFontDescription.family;
339 void Controller::SetDefaultFontStyle( const std::string& style )
341 if( NULL == mImpl->mFontDefaults )
343 mImpl->mFontDefaults = new FontDefaults();
346 mImpl->mFontDefaults->mFontStyle = style;
349 const std::string& Controller::GetDefaultFontStyle() const
351 if( NULL != mImpl->mFontDefaults )
353 return mImpl->mFontDefaults->mFontStyle;
359 void Controller::SetDefaultFontWeight( FontWeight weight )
361 if( NULL == mImpl->mFontDefaults )
363 mImpl->mFontDefaults = new FontDefaults();
366 mImpl->mFontDefaults->mFontDescription.weight = weight;
367 mImpl->mFontDefaults->weightDefined = true;
369 // Clear the font-specific data
372 mImpl->mRecalculateNaturalSize = true;
374 mImpl->RequestRelayout();
377 FontWeight Controller::GetDefaultFontWeight() const
379 if( NULL != mImpl->mFontDefaults )
381 return mImpl->mFontDefaults->mFontDescription.weight;
384 return TextAbstraction::FontWeight::NORMAL;
387 void Controller::SetDefaultFontWidth( FontWidth width )
389 if( NULL == mImpl->mFontDefaults )
391 mImpl->mFontDefaults = new FontDefaults();
394 mImpl->mFontDefaults->mFontDescription.width = width;
395 mImpl->mFontDefaults->widthDefined = true;
397 // Clear the font-specific data
400 mImpl->mRecalculateNaturalSize = true;
402 mImpl->RequestRelayout();
405 FontWidth Controller::GetDefaultFontWidth() const
407 if( NULL != mImpl->mFontDefaults )
409 return mImpl->mFontDefaults->mFontDescription.width;
412 return TextAbstraction::FontWidth::NORMAL;
415 void Controller::SetDefaultFontSlant( FontSlant slant )
417 if( NULL == mImpl->mFontDefaults )
419 mImpl->mFontDefaults = new FontDefaults();
422 mImpl->mFontDefaults->mFontDescription.slant = slant;
423 mImpl->mFontDefaults->slantDefined = true;
425 // Clear the font-specific data
428 mImpl->mRecalculateNaturalSize = true;
430 mImpl->RequestRelayout();
433 FontSlant Controller::GetDefaultFontSlant() const
435 if( NULL != mImpl->mFontDefaults )
437 return mImpl->mFontDefaults->mFontDescription.slant;
440 return TextAbstraction::FontSlant::NORMAL;
443 void Controller::SetDefaultPointSize( float pointSize )
445 if( NULL == mImpl->mFontDefaults )
447 mImpl->mFontDefaults = new FontDefaults();
450 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
451 mImpl->mFontDefaults->sizeDefined = true;
453 unsigned int horizontalDpi( 0u );
454 unsigned int verticalDpi( 0u );
455 mImpl->mFontClient.GetDpi( horizontalDpi, verticalDpi );
457 // Adjust the metrics if the fixed-size font should be down-scaled
458 int maxEmojiSize( pointSize/POINTS_PER_INCH * verticalDpi );
459 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultPointSize %p setting MaxEmojiSize %d\n", this, maxEmojiSize );
460 mImpl->mMetrics->SetMaxEmojiSize( maxEmojiSize );
462 // Clear the font-specific data
465 mImpl->mRecalculateNaturalSize = true;
467 mImpl->RequestRelayout();
470 float Controller::GetDefaultPointSize() const
472 if( NULL != mImpl->mFontDefaults )
474 return mImpl->mFontDefaults->mDefaultPointSize;
480 void Controller::UpdateAfterFontChange( const std::string& newDefaultFont )
482 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange");
484 if( !mImpl->mFontDefaults->familyDefined ) // If user defined font then should not update when system font changes
486 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
488 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
489 mImpl->mRecalculateNaturalSize = true;
490 mImpl->RequestRelayout();
494 void Controller::SetTextColor( const Vector4& textColor )
496 mImpl->mTextColor = textColor;
498 if( !mImpl->IsShowingPlaceholderText() )
500 mImpl->mVisualModel->SetTextColor( textColor );
502 mImpl->RequestRelayout();
506 const Vector4& Controller::GetTextColor() const
508 return mImpl->mTextColor;
511 bool Controller::RemoveText( int cursorOffset, int numberOfCharacters )
513 bool removed = false;
515 if( NULL == mImpl->mEventData )
520 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfCharacters %d\n",
521 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters );
523 if( !mImpl->IsShowingPlaceholderText() )
525 // Delete at current cursor position
526 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
527 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
529 CharacterIndex cursorIndex = oldCursorIndex;
531 // Validate the cursor position & number of characters
532 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
534 cursorIndex = oldCursorIndex + cursorOffset;
537 if( ( cursorIndex + numberOfCharacters ) > currentText.Count() )
539 numberOfCharacters = currentText.Count() - cursorIndex;
542 if( ( cursorIndex + numberOfCharacters ) <= mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters )
544 // Mark the paragraphs to be updated.
545 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
546 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove += numberOfCharacters;
548 // Update the input style and remove the text's style before removing the text.
550 // Set first the default input style.
551 mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
553 // Update the input style.
554 mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
556 // Remove the text's style before removing the text.
557 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfCharacters );
559 // Remove the characters.
560 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
561 Vector<Character>::Iterator last = first + numberOfCharacters;
563 currentText.Erase( first, last );
565 // Cursor position retreat
566 oldCursorIndex = cursorIndex;
568 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfCharacters );
576 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
578 if( NULL != mImpl->mEventData )
580 mImpl->mEventData->mPlaceholderTextColor = textColor;
583 if( mImpl->IsShowingPlaceholderText() )
585 mImpl->mVisualModel->SetTextColor( textColor );
586 mImpl->RequestRelayout();
590 const Vector4& Controller::GetPlaceholderTextColor() const
592 if( NULL != mImpl->mEventData )
594 return mImpl->mEventData->mPlaceholderTextColor;
600 void Controller::SetShadowOffset( const Vector2& shadowOffset )
602 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
604 mImpl->RequestRelayout();
607 const Vector2& Controller::GetShadowOffset() const
609 return mImpl->mVisualModel->GetShadowOffset();
612 void Controller::SetShadowColor( const Vector4& shadowColor )
614 mImpl->mVisualModel->SetShadowColor( shadowColor );
616 mImpl->RequestRelayout();
619 const Vector4& Controller::GetShadowColor() const
621 return mImpl->mVisualModel->GetShadowColor();
624 void Controller::SetUnderlineColor( const Vector4& color )
626 mImpl->mVisualModel->SetUnderlineColor( color );
628 mImpl->RequestRelayout();
631 const Vector4& Controller::GetUnderlineColor() const
633 return mImpl->mVisualModel->GetUnderlineColor();
636 void Controller::SetUnderlineEnabled( bool enabled )
638 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
640 mImpl->RequestRelayout();
643 bool Controller::IsUnderlineEnabled() const
645 return mImpl->mVisualModel->IsUnderlineEnabled();
648 void Controller::SetUnderlineHeight( float height )
650 mImpl->mVisualModel->SetUnderlineHeight( height );
652 mImpl->RequestRelayout();
655 float Controller::GetUnderlineHeight() const
657 return mImpl->mVisualModel->GetUnderlineHeight();
660 void Controller::SetInputColor( const Vector4& color )
662 if( NULL != mImpl->mEventData )
664 mImpl->mEventData->mInputStyle.textColor = color;
666 if( EventData::SELECTING == mImpl->mEventData->mState )
668 const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
670 // Get start and end position of selection
671 const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
672 const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
674 // Add the color run.
675 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
676 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
678 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
679 colorRun.color = color;
680 colorRun.characterRun.characterIndex = startOfSelectedText;
681 colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
683 // Request to relayout.
684 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
685 mImpl->RequestRelayout();
690 const Vector4& Controller::GetInputColor() const
692 if( NULL != mImpl->mEventData )
694 return mImpl->mEventData->mInputStyle.textColor;
697 // Return the default text's color if there is no EventData.
698 return mImpl->mTextColor;
702 void Controller::SetInputFontFamily( const std::string& fontFamily )
704 if( NULL != mImpl->mEventData )
706 mImpl->mEventData->mInputStyle.familyName = fontFamily;
707 mImpl->mEventData->mInputStyle.familyDefined = true;
709 if( EventData::SELECTING == mImpl->mEventData->mState )
711 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
712 mImpl->mLogicalModel );
714 fontDescriptionRun.familyLength = fontFamily.size();
715 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
716 memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
717 fontDescriptionRun.familyDefined = true;
719 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
721 // Request to relayout.
722 mImpl->mOperationsPending = ALL_OPERATIONS;
723 mImpl->mRecalculateNaturalSize = true;
724 mImpl->RequestRelayout();
726 // As the font changes, recalculate the handle positions is needed.
727 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
728 mImpl->mEventData->mUpdateRightSelectionPosition = true;
729 mImpl->mEventData->mScrollAfterUpdatePosition = true;
734 const std::string& Controller::GetInputFontFamily() const
736 if( NULL != mImpl->mEventData )
738 return mImpl->mEventData->mInputStyle.familyName;
741 // Return the default font's family if there is no EventData.
742 return GetDefaultFontFamily();
745 void Controller::SetInputFontStyle( const std::string& fontStyle )
747 if( NULL != mImpl->mEventData )
749 mImpl->mEventData->mInputStyle.fontStyle = fontStyle;
753 const std::string& Controller::GetInputFontStyle() const
755 if( NULL != mImpl->mEventData )
757 return mImpl->mEventData->mInputStyle.fontStyle;
760 // Return the default font's style if there is no EventData.
761 return GetDefaultFontStyle();
764 void Controller::SetInputFontWeight( FontWeight weight )
766 if( NULL != mImpl->mEventData )
768 mImpl->mEventData->mInputStyle.weight = weight;
769 mImpl->mEventData->mInputStyle.weightDefined = true;
771 if( EventData::SELECTING == mImpl->mEventData->mState )
773 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
774 mImpl->mLogicalModel );
776 fontDescriptionRun.weight = weight;
777 fontDescriptionRun.weightDefined = true;
779 // Request to relayout.
780 mImpl->mOperationsPending = ALL_OPERATIONS;
781 mImpl->mRecalculateNaturalSize = true;
782 mImpl->RequestRelayout();
784 // As the font might change, recalculate the handle positions is needed.
785 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
786 mImpl->mEventData->mUpdateRightSelectionPosition = true;
787 mImpl->mEventData->mScrollAfterUpdatePosition = true;
792 FontWeight Controller::GetInputFontWeight() const
794 if( NULL != mImpl->mEventData )
796 return mImpl->mEventData->mInputStyle.weight;
799 return GetDefaultFontWeight();
802 void Controller::SetInputFontWidth( FontWidth width )
804 if( NULL != mImpl->mEventData )
806 mImpl->mEventData->mInputStyle.width = width;
807 mImpl->mEventData->mInputStyle.widthDefined = true;
809 if( EventData::SELECTING == mImpl->mEventData->mState )
811 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
812 mImpl->mLogicalModel );
814 fontDescriptionRun.width = width;
815 fontDescriptionRun.widthDefined = true;
817 // Request to relayout.
818 mImpl->mOperationsPending = ALL_OPERATIONS;
819 mImpl->mRecalculateNaturalSize = true;
820 mImpl->RequestRelayout();
822 // As the font might change, recalculate the handle positions is needed.
823 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
824 mImpl->mEventData->mUpdateRightSelectionPosition = true;
825 mImpl->mEventData->mScrollAfterUpdatePosition = true;
830 FontWidth Controller::GetInputFontWidth() const
832 if( NULL != mImpl->mEventData )
834 return mImpl->mEventData->mInputStyle.width;
837 return GetDefaultFontWidth();
840 void Controller::SetInputFontSlant( FontSlant slant )
842 if( NULL != mImpl->mEventData )
844 mImpl->mEventData->mInputStyle.slant = slant;
845 mImpl->mEventData->mInputStyle.slantDefined = true;
847 if( EventData::SELECTING == mImpl->mEventData->mState )
849 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
850 mImpl->mLogicalModel );
852 fontDescriptionRun.slant = slant;
853 fontDescriptionRun.slantDefined = true;
855 // Request to relayout.
856 mImpl->mOperationsPending = ALL_OPERATIONS;
857 mImpl->mRecalculateNaturalSize = true;
858 mImpl->RequestRelayout();
860 // As the font might change, recalculate the handle positions is needed.
861 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
862 mImpl->mEventData->mUpdateRightSelectionPosition = true;
863 mImpl->mEventData->mScrollAfterUpdatePosition = true;
868 FontSlant Controller::GetInputFontSlant() const
870 if( NULL != mImpl->mEventData )
872 return mImpl->mEventData->mInputStyle.slant;
875 return GetDefaultFontSlant();
878 void Controller::SetInputFontPointSize( float size )
880 if( NULL != mImpl->mEventData )
882 mImpl->mEventData->mInputStyle.size = size;
884 if( EventData::SELECTING == mImpl->mEventData->mState )
886 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
887 mImpl->mLogicalModel );
889 fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
890 fontDescriptionRun.sizeDefined = true;
892 // Request to relayout.
893 mImpl->mOperationsPending = ALL_OPERATIONS;
894 mImpl->mRecalculateNaturalSize = true;
895 mImpl->RequestRelayout();
897 // As the font might change, recalculate the handle positions is needed.
898 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
899 mImpl->mEventData->mUpdateRightSelectionPosition = true;
900 mImpl->mEventData->mScrollAfterUpdatePosition = true;
905 float Controller::GetInputFontPointSize() const
907 if( NULL != mImpl->mEventData )
909 return mImpl->mEventData->mInputStyle.size;
912 // Return the default font's point size if there is no EventData.
913 return GetDefaultPointSize();
916 void Controller::SetEnableCursorBlink( bool enable )
918 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
920 if( NULL != mImpl->mEventData )
922 mImpl->mEventData->mCursorBlinkEnabled = enable;
925 mImpl->mEventData->mDecorator )
927 mImpl->mEventData->mDecorator->StopCursorBlink();
932 bool Controller::GetEnableCursorBlink() const
934 if( NULL != mImpl->mEventData )
936 return mImpl->mEventData->mCursorBlinkEnabled;
942 const Vector2& Controller::GetScrollPosition() const
944 if( NULL != mImpl->mEventData )
946 return mImpl->mEventData->mScrollPosition;
949 return Vector2::ZERO;
952 const Vector2& Controller::GetAlignmentOffset() const
954 return mImpl->mAlignmentOffset;
957 Vector3 Controller::GetNaturalSize()
959 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
962 // Make sure the model is up-to-date before layouting
963 ProcessModifyEvents();
965 if( mImpl->mRecalculateNaturalSize )
967 // Operations that can be done only once until the text changes.
968 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
976 // Make sure the model is up-to-date before layouting
977 mImpl->UpdateModel( onlyOnceOperations );
979 // Layout the text for the new width.
980 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
982 // Set the update info to relayout the whole text.
983 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
984 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
986 // Store the actual control's width.
987 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
989 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
990 static_cast<OperationsMask>( onlyOnceOperations |
992 naturalSize.GetVectorXY() );
994 // Do not do again the only once operations.
995 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
997 // Do the size related operations again.
998 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1001 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1003 // Stores the natural size to avoid recalculate it again
1004 // unless the text/style changes.
1005 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
1007 mImpl->mRecalculateNaturalSize = false;
1009 // Clear the update info. This info will be set the next time the text is updated.
1010 mImpl->mTextUpdateInfo.Clear();
1012 // Restore the actual control's width.
1013 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1015 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1019 naturalSize = mImpl->mVisualModel->GetNaturalSize();
1021 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1024 naturalSize.x = ConvertToEven( naturalSize.x );
1025 naturalSize.y = ConvertToEven( naturalSize.y );
1030 float Controller::GetHeightForWidth( float width )
1032 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1033 // Make sure the model is up-to-date before layouting
1034 ProcessModifyEvents();
1037 if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
1039 // Operations that can be done only once until the text changes.
1040 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1047 GET_GLYPH_METRICS );
1048 // Make sure the model is up-to-date before layouting
1049 mImpl->UpdateModel( onlyOnceOperations );
1052 // Layout the text for the new width.
1053 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1055 // Set the update info to relayout the whole text.
1056 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1057 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1059 // Store the actual control's width.
1060 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
1062 DoRelayout( Size( width, MAX_FLOAT ),
1063 static_cast<OperationsMask>( onlyOnceOperations |
1067 // Do not do again the only once operations.
1068 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1070 // Do the size related operations again.
1071 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1075 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1077 // Clear the update info. This info will be set the next time the text is updated.
1078 mImpl->mTextUpdateInfo.Clear();
1080 // Restore the actual control's width.
1081 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1083 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1087 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1088 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1091 return layoutSize.height;
1094 bool Controller::Relayout( const Size& size )
1096 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
1098 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1100 bool glyphsRemoved( false );
1101 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1103 mImpl->mVisualModel->mGlyphPositions.Clear();
1104 glyphsRemoved = true;
1107 // Clear the update info. This info will be set the next time the text is updated.
1108 mImpl->mTextUpdateInfo.Clear();
1110 // Not worth to relayout if width or height is equal to zero.
1111 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1112 return glyphsRemoved;
1115 // Whether a new size has been set.
1116 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1120 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1122 // Layout operations that need to be done if the size changes.
1123 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1126 UPDATE_ACTUAL_SIZE |
1128 // Set the update info to relayout the whole text.
1129 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
1130 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1133 // Whether there are modify events.
1134 const bool isModifyEventsEmpty = 0u == mImpl->mModifyEvents.Count();
1136 // Make sure the model is up-to-date before layouting.
1137 ProcessModifyEvents();
1138 mImpl->UpdateModel( mImpl->mOperationsPending );
1140 // Style operations that need to be done if the text is modified.
1141 if( !isModifyEventsEmpty )
1143 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1147 // Apply the style runs if text is modified.
1148 bool updated = mImpl->UpdateModelStyle( mImpl->mOperationsPending );
1152 updated = DoRelayout( size,
1153 mImpl->mOperationsPending,
1154 layoutSize ) || updated;
1156 // Do not re-do any operation until something changes.
1157 mImpl->mOperationsPending = NO_OPERATION;
1159 // Whether the text control is editable
1160 const bool isEditable = NULL != mImpl->mEventData;
1162 // Keep the current offset and alignment as it will be used to update the decorator's positions (if the size changes).
1164 if( newSize && isEditable )
1166 offset = mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition;
1169 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
1170 CalculateTextAlignment( size );
1176 // If there is a new size, the scroll position needs to be clamped.
1177 mImpl->ClampHorizontalScroll( layoutSize );
1179 // Update the decorator's positions is needed if there is a new size.
1180 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
1183 // Move the cursor, grab handle etc.
1184 updated = mImpl->ProcessInputEvents() || updated;
1187 // Clear the update info. This info will be set the next time the text is updated.
1188 mImpl->mTextUpdateInfo.Clear();
1190 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1194 void Controller::ProcessModifyEvents()
1196 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
1198 if( 0u == events.Count() )
1204 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
1205 endIt = events.End();
1209 const ModifyEvent& event = *it;
1211 if( ModifyEvent::TEXT_REPLACED == event.type )
1213 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
1214 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
1216 TextReplacedEvent();
1218 else if( ModifyEvent::TEXT_INSERTED == event.type )
1220 TextInsertedEvent();
1222 else if( ModifyEvent::TEXT_DELETED == event.type )
1224 // Placeholder-text cannot be deleted
1225 if( !mImpl->IsShowingPlaceholderText() )
1232 if( NULL != mImpl->mEventData )
1234 // When the text is being modified, delay cursor blinking
1235 mImpl->mEventData->mDecorator->DelayCursorBlink();
1238 // Discard temporary text
1242 void Controller::ResetText()
1245 mImpl->mLogicalModel->mText.Clear();
1247 // We have cleared everything including the placeholder-text
1248 mImpl->PlaceholderCleared();
1250 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1251 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1252 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
1254 // Clear any previous text.
1255 mImpl->mTextUpdateInfo.mClearAll = true;
1257 // The natural size needs to be re-calculated.
1258 mImpl->mRecalculateNaturalSize = true;
1260 // Apply modifications to the model
1261 mImpl->mOperationsPending = ALL_OPERATIONS;
1264 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
1266 // Reset the cursor position
1267 if( NULL != mImpl->mEventData )
1269 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
1271 // Update the cursor if it's in editing mode.
1272 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1274 mImpl->mEventData->mUpdateCursorPosition = true;
1279 void Controller::ResetScrollPosition()
1281 if( NULL != mImpl->mEventData )
1283 // Reset the scroll position.
1284 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
1285 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1289 void Controller::TextReplacedEvent()
1291 // The natural size needs to be re-calculated.
1292 mImpl->mRecalculateNaturalSize = true;
1294 // Apply modifications to the model
1295 mImpl->mOperationsPending = ALL_OPERATIONS;
1298 void Controller::TextInsertedEvent()
1300 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
1302 if( NULL == mImpl->mEventData )
1307 // The natural size needs to be re-calculated.
1308 mImpl->mRecalculateNaturalSize = true;
1310 // Apply modifications to the model; TODO - Optimize this
1311 mImpl->mOperationsPending = ALL_OPERATIONS;
1313 // Queue a cursor reposition event; this must wait until after DoRelayout()
1314 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1316 mImpl->mEventData->mUpdateCursorPosition = true;
1317 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1321 void Controller::TextDeletedEvent()
1323 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
1325 if( NULL == mImpl->mEventData )
1330 // The natural size needs to be re-calculated.
1331 mImpl->mRecalculateNaturalSize = true;
1333 // Apply modifications to the model; TODO - Optimize this
1334 mImpl->mOperationsPending = ALL_OPERATIONS;
1336 // Queue a cursor reposition event; this must wait until after DoRelayout()
1337 mImpl->mEventData->mUpdateCursorPosition = true;
1338 if( 0u != mImpl->mLogicalModel->mText.Count() )
1340 mImpl->mEventData->mScrollAfterDelete = true;
1344 bool Controller::DoRelayout( const Size& size,
1345 OperationsMask operationsRequired,
1348 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
1349 bool viewUpdated( false );
1351 // Calculate the operations to be done.
1352 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
1354 const CharacterIndex startIndex = mImpl->mTextUpdateInfo.mParagraphCharacterIndex;
1355 const Length requestedNumberOfCharacters = mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters;
1357 if( LAYOUT & operations )
1359 // Some vectors with data needed to layout and reorder may be void
1360 // after the first time the text has been laid out.
1361 // Fill the vectors again.
1363 // Calculate the number of glyphs to layout.
1364 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1365 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1366 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
1367 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
1369 const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
1370 const GlyphIndex startGlyphIndex = mImpl->mTextUpdateInfo.mStartGlyphIndex;
1371 const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
1372 const Length totalNumberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
1374 if( 0u == totalNumberOfGlyphs )
1376 if( UPDATE_ACTUAL_SIZE & operations )
1378 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
1381 // Nothing else to do if there is no glyphs.
1382 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1386 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1387 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1388 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1389 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1390 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1391 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1392 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1394 // Set the layout parameters.
1395 LayoutParameters layoutParameters( size,
1397 lineBreakInfo.Begin(),
1398 wordBreakInfo.Begin(),
1399 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1401 glyphsToCharactersMap.Begin(),
1402 charactersPerGlyph.Begin(),
1403 charactersToGlyphBuffer,
1404 glyphsPerCharacterBuffer,
1405 totalNumberOfGlyphs );
1407 // Resize the vector of positions to have the same size than the vector of glyphs.
1408 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1409 glyphPositions.Resize( totalNumberOfGlyphs );
1411 // Whether the last character is a new paragraph character.
1412 mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1413 layoutParameters.isLastNewParagraph = mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph;
1415 // The initial glyph and the number of glyphs to layout.
1416 layoutParameters.startGlyphIndex = startGlyphIndex;
1417 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1418 layoutParameters.startLineIndex = mImpl->mTextUpdateInfo.mStartLineIndex;
1419 layoutParameters.estimatedNumberOfLines = mImpl->mTextUpdateInfo.mEstimatedNumberOfLines;
1421 // Update the visual model.
1422 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1424 mImpl->mVisualModel->mLines,
1429 // Reorder the lines
1430 if( REORDER & operations )
1432 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1433 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1435 // Check first if there are paragraphs with bidirectional info.
1436 if( 0u != bidirectionalInfo.Count() )
1439 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1441 // Reorder the lines.
1442 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1443 ReorderLines( bidirectionalInfo,
1445 requestedNumberOfCharacters,
1446 mImpl->mVisualModel->mLines,
1447 bidirectionalLineInfo );
1449 // Set the bidirectional info per line into the layout parameters.
1450 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1451 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1453 // TODO: update the conversion map instead creating it from scratch.
1454 // Note this tables store indices to characters, so update the table means modify all the indices of the text after the last updated character.
1455 // It's better to refactor this. Store this table per line and don't update the indices.
1456 // For the cursor position probably is better to use the function instead creating a table.
1457 // Set the bidirectional info into the model.
1458 mImpl->mLogicalModel->SetVisualToLogicalMap( layoutParameters.lineBidirectionalInfoRunsBuffer,
1459 layoutParameters.numberOfBidirectionalInfoRuns,
1461 mImpl->mLogicalModel->mText.Count() );
1463 // Re-layout the text. Reorder those lines with right to left characters.
1464 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1466 requestedNumberOfCharacters,
1472 // Sets the actual size.
1473 if( UPDATE_ACTUAL_SIZE & operations )
1475 mImpl->mVisualModel->SetLayoutSize( layoutSize );
1479 // Store the size used to layout the text.
1480 mImpl->mVisualModel->mControlSize = size;
1484 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1487 if( ALIGN & operations )
1489 // The laid-out lines.
1490 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1492 mImpl->mLayoutEngine.Align( size,
1494 requestedNumberOfCharacters,
1500 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1504 void Controller::SetMultiLineEnabled( bool enable )
1506 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1508 if( layout != mImpl->mLayoutEngine.GetLayout() )
1510 // Set the layout type.
1511 mImpl->mLayoutEngine.SetLayout( layout );
1513 // Set the flags to redo the layout operations
1514 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1515 UPDATE_ACTUAL_SIZE |
1519 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1521 mImpl->RequestRelayout();
1525 bool Controller::IsMultiLineEnabled() const
1527 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1530 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1532 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1534 // Set the alignment.
1535 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1537 // Set the flag to redo the alignment operation.
1538 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1540 mImpl->RequestRelayout();
1544 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1546 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1549 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1551 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1553 // Set the alignment.
1554 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1556 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1558 mImpl->RequestRelayout();
1562 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1564 return mImpl->mLayoutEngine.GetVerticalAlignment();
1567 void Controller::CalculateTextAlignment( const Size& controlSize )
1569 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1571 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1573 // Get the line height of the default font.
1574 layoutSize.height = mImpl->GetDefaultFontLineHeight();
1577 if( LayoutEngine::SINGLE_LINE_BOX == mImpl->mLayoutEngine.GetLayout() )
1579 // Get the direction of the first character.
1580 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1582 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1583 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1584 if( firstParagraphDirection )
1586 switch( horizontalAlignment )
1588 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1590 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1593 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1598 case LayoutEngine::HORIZONTAL_ALIGN_END:
1600 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1606 switch( horizontalAlignment )
1608 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1610 mImpl->mAlignmentOffset.x = 0.f;
1613 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1615 mImpl->mAlignmentOffset.x = floorf( 0.5f * ( controlSize.width - layoutSize.width ) ); // try to avoid pixel alignment.
1618 case LayoutEngine::HORIZONTAL_ALIGN_END:
1620 mImpl->mAlignmentOffset.x = controlSize.width - layoutSize.width;
1626 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1627 switch( verticalAlignment )
1629 case LayoutEngine::VERTICAL_ALIGN_TOP:
1631 mImpl->mAlignmentOffset.y = 0.f;
1634 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1636 mImpl->mAlignmentOffset.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
1639 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1641 mImpl->mAlignmentOffset.y = controlSize.height - layoutSize.height;
1647 LayoutEngine& Controller::GetLayoutEngine()
1649 return mImpl->mLayoutEngine;
1652 View& Controller::GetView()
1654 return mImpl->mView;
1657 void Controller::KeyboardFocusGainEvent()
1659 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1661 if( NULL != mImpl->mEventData )
1663 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1664 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1666 mImpl->ChangeState( EventData::EDITING );
1667 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1670 if( mImpl->IsShowingPlaceholderText() )
1672 // Show alternative placeholder-text when editing
1673 ShowPlaceholderText();
1676 mImpl->RequestRelayout();
1680 void Controller::KeyboardFocusLostEvent()
1682 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1684 if( NULL != mImpl->mEventData )
1686 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1688 mImpl->ChangeState( EventData::INACTIVE );
1690 if( !mImpl->IsShowingRealText() )
1692 // Revert to regular placeholder-text when not editing
1693 ShowPlaceholderText();
1697 mImpl->RequestRelayout();
1700 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1702 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1704 bool textChanged( false );
1706 if( ( NULL != mImpl->mEventData ) &&
1707 ( keyEvent.state == KeyEvent::Down ) )
1709 int keyCode = keyEvent.keyCode;
1710 const std::string& keyString = keyEvent.keyPressed;
1712 // Pre-process to separate modifying events from non-modifying input events.
1713 if( Dali::DALI_KEY_ESCAPE == keyCode )
1715 // Escape key is a special case which causes focus loss
1716 KeyboardFocusLostEvent();
1718 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
1719 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1720 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
1721 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
1723 Event event( Event::CURSOR_KEY_EVENT );
1724 event.p1.mInt = keyCode;
1725 mImpl->mEventData->mEventQueue.push_back( event );
1727 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1729 textChanged = BackspaceKeyEvent();
1731 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1733 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1734 // Avoids calling the InsertText() method which can delete selected text
1736 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1737 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1739 mImpl->ChangeState( EventData::INACTIVE );
1740 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1741 // Avoids calling the InsertText() method which can delete selected text
1743 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1745 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1746 // and a character is typed after the type of a upper case latin character.
1752 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1754 // IMF manager is no longer handling key-events
1755 mImpl->ClearPreEditFlag();
1757 InsertText( keyString, COMMIT );
1761 if( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1762 ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1764 mImpl->ChangeState( EventData::EDITING );
1767 mImpl->RequestRelayout();
1772 // Do this last since it provides callbacks into application code
1773 mImpl->mControlInterface.TextChanged();
1779 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1781 bool removedPrevious( false );
1782 bool maxLengthReached( false );
1784 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1786 if( NULL == mImpl->mEventData )
1791 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1792 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1793 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1795 // TODO: At the moment the underline runs are only for pre-edit.
1796 mImpl->mVisualModel->mUnderlineRuns.Clear();
1798 Vector<Character> utf32Characters;
1799 Length characterCount( 0u );
1801 // Remove the previous IMF pre-edit (predicitive text)
1802 if( mImpl->mEventData->mPreEditFlag &&
1803 ( 0u != mImpl->mEventData->mPreEditLength ) )
1805 const CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1807 removedPrevious = RemoveText( -static_cast<int>( offset ), mImpl->mEventData->mPreEditLength );
1809 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1810 mImpl->mEventData->mPreEditLength = 0u;
1814 // Remove the previous Selection
1815 removedPrevious = RemoveSelectedText();
1820 // Convert text into UTF-32
1821 utf32Characters.Resize( text.size() );
1823 // This is a bit horrible but std::string returns a (signed) char*
1824 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1826 // Transform a text array encoded in utf8 into an array encoded in utf32.
1827 // It returns the actual number of characters.
1828 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1829 utf32Characters.Resize( characterCount );
1831 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1832 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1835 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1837 // The placeholder text is no longer needed
1838 if( mImpl->IsShowingPlaceholderText() )
1843 mImpl->ChangeState( EventData::EDITING );
1845 // Handle the IMF (predicitive text) state changes
1846 if( COMMIT == type )
1848 // IMF manager is no longer handling key-events
1849 mImpl->ClearPreEditFlag();
1853 if( !mImpl->mEventData->mPreEditFlag )
1855 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1857 // Record the start of the pre-edit text
1858 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1861 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1862 mImpl->mEventData->mPreEditFlag = true;
1864 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1867 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1869 // Restrict new text to fit within Maximum characters setting.
1870 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1871 maxLengthReached = ( characterCount > maxSizeOfNewText );
1873 // The cursor position.
1874 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1876 // Update the text's style.
1878 // Updates the text style runs by adding characters.
1879 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
1881 // Get the character index from the cursor index.
1882 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
1884 // Retrieve the text's style for the given index.
1886 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
1888 // Whether to add a new text color run.
1889 const bool addColorRun = style.textColor != mImpl->mEventData->mInputStyle.textColor;
1891 // Whether to add a new font run.
1892 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
1893 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
1894 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
1895 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
1896 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
1901 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
1902 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
1904 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
1905 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
1906 colorRun.characterRun.characterIndex = cursorIndex;
1907 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1910 if( addFontNameRun ||
1916 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
1917 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
1919 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
1921 if( addFontNameRun )
1923 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
1924 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
1925 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
1926 fontDescriptionRun.familyDefined = true;
1928 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
1931 if( addFontWeightRun )
1933 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
1934 fontDescriptionRun.weightDefined = true;
1937 if( addFontWidthRun )
1939 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
1940 fontDescriptionRun.widthDefined = true;
1943 if( addFontSlantRun )
1945 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
1946 fontDescriptionRun.slantDefined = true;
1949 if( addFontSizeRun )
1951 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
1952 fontDescriptionRun.sizeDefined = true;
1955 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
1956 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1959 // Insert at current cursor position.
1960 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1962 if( cursorIndex < numberOfCharactersInModel )
1964 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1968 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1971 // Mark the first paragraph to be updated.
1972 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
1973 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
1975 // Update the cursor index.
1976 cursorIndex += maxSizeOfNewText;
1978 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1981 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
1982 mImpl->IsPlaceholderAvailable() )
1984 // Show place-holder if empty after removing the pre-edit text
1985 ShowPlaceholderText();
1986 mImpl->mEventData->mUpdateCursorPosition = true;
1987 mImpl->ClearPreEditFlag();
1989 else if( removedPrevious ||
1990 ( 0 != utf32Characters.Count() ) )
1992 // Queue an inserted event
1993 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1996 if( maxLengthReached )
1998 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
2000 mImpl->ResetImfManager();
2002 // Do this last since it provides callbacks into application code
2003 mImpl->mControlInterface.MaxLengthReached();
2007 bool Controller::RemoveSelectedText()
2009 bool textRemoved( false );
2011 if( EventData::SELECTING == mImpl->mEventData->mState )
2013 std::string removedString;
2014 mImpl->RetrieveSelection( removedString, true );
2016 if( !removedString.empty() )
2019 mImpl->ChangeState( EventData::EDITING );
2026 void Controller::TapEvent( unsigned int tapCount, float x, float y )
2028 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
2030 if( NULL != mImpl->mEventData )
2032 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
2034 if( 1u == tapCount )
2036 // This is to avoid unnecessary relayouts when tapping an empty text-field
2037 bool relayoutNeeded( false );
2039 if( ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) ||
2040 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
2042 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE); // If Popup shown hide it here so can be shown again if required.
2045 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
2047 // Already in an active state so show a popup
2048 if( !mImpl->IsClipboardEmpty() )
2050 // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
2051 mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
2055 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2057 relayoutNeeded = true;
2061 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2063 // Hide placeholder text
2067 if( EventData::INACTIVE == mImpl->mEventData->mState )
2069 mImpl->ChangeState( EventData::EDITING );
2071 else if( !mImpl->IsClipboardEmpty() )
2073 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2075 relayoutNeeded = true;
2078 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2079 if( relayoutNeeded )
2081 Event event( Event::TAP_EVENT );
2082 event.p1.mUint = tapCount;
2083 event.p2.mFloat = x;
2084 event.p3.mFloat = y;
2085 mImpl->mEventData->mEventQueue.push_back( event );
2087 mImpl->RequestRelayout();
2090 else if( 2u == tapCount )
2092 if( mImpl->mEventData->mSelectionEnabled &&
2093 mImpl->IsShowingRealText() )
2095 SelectEvent( x, y, false );
2100 // Reset keyboard as tap event has occurred.
2101 mImpl->ResetImfManager();
2104 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2105 // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2107 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2109 if( NULL != mImpl->mEventData )
2111 Event event( Event::PAN_EVENT );
2112 event.p1.mInt = state;
2113 event.p2.mFloat = displacement.x;
2114 event.p3.mFloat = displacement.y;
2115 mImpl->mEventData->mEventQueue.push_back( event );
2117 mImpl->RequestRelayout();
2121 void Controller::LongPressEvent( Gesture::State state, float x, float y )
2123 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2125 if( ( state == Gesture::Started ) &&
2126 ( NULL != mImpl->mEventData ) )
2128 if( !mImpl->IsShowingRealText() )
2130 Event event( Event::LONG_PRESS_EVENT );
2131 event.p1.mInt = state;
2132 mImpl->mEventData->mEventQueue.push_back( event );
2133 mImpl->RequestRelayout();
2137 // The 1st long-press on inactive text-field is treated as tap
2138 if( EventData::INACTIVE == mImpl->mEventData->mState )
2140 mImpl->ChangeState( EventData::EDITING );
2142 Event event( Event::TAP_EVENT );
2144 event.p2.mFloat = x;
2145 event.p3.mFloat = y;
2146 mImpl->mEventData->mEventQueue.push_back( event );
2148 mImpl->RequestRelayout();
2152 // Reset the imf manger to commit the pre-edit before selecting the text.
2153 mImpl->ResetImfManager();
2155 SelectEvent( x, y, false );
2161 void Controller::SelectEvent( float x, float y, bool selectAll )
2163 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2165 if( NULL != mImpl->mEventData )
2167 mImpl->ChangeState( EventData::SELECTING );
2171 Event event( Event::SELECT_ALL );
2172 mImpl->mEventData->mEventQueue.push_back( event );
2176 Event event( Event::SELECT );
2177 event.p2.mFloat = x;
2178 event.p3.mFloat = y;
2179 mImpl->mEventData->mEventQueue.push_back( event );
2182 mImpl->RequestRelayout();
2186 void Controller::GetTargetSize( Vector2& targetSize )
2188 targetSize = mImpl->mVisualModel->mControlSize;
2191 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2193 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2196 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2198 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2200 if( NULL != mImpl->mEventData )
2202 switch( handleType )
2206 Event event( Event::GRAB_HANDLE_EVENT );
2207 event.p1.mUint = state;
2208 event.p2.mFloat = x;
2209 event.p3.mFloat = y;
2211 mImpl->mEventData->mEventQueue.push_back( event );
2214 case LEFT_SELECTION_HANDLE:
2216 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2217 event.p1.mUint = state;
2218 event.p2.mFloat = x;
2219 event.p3.mFloat = y;
2221 mImpl->mEventData->mEventQueue.push_back( event );
2224 case RIGHT_SELECTION_HANDLE:
2226 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2227 event.p1.mUint = state;
2228 event.p2.mFloat = x;
2229 event.p3.mFloat = y;
2231 mImpl->mEventData->mEventQueue.push_back( event );
2234 case LEFT_SELECTION_HANDLE_MARKER:
2235 case RIGHT_SELECTION_HANDLE_MARKER:
2237 // Markers do not move the handles.
2240 case HANDLE_TYPE_COUNT:
2242 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2246 mImpl->RequestRelayout();
2250 void Controller::PasteText( const std::string& stringToPaste )
2252 InsertText( stringToPaste, Text::Controller::COMMIT );
2253 mImpl->ChangeState( EventData::EDITING );
2254 mImpl->RequestRelayout();
2256 // Do this last since it provides callbacks into application code
2257 mImpl->mControlInterface.TextChanged();
2260 void Controller::PasteClipboardItemEvent()
2262 // Retrieve the clipboard contents first
2263 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2264 std::string stringToPaste( notifier.GetContent() );
2266 // Commit the current pre-edit text; the contents of the clipboard should be appended
2267 mImpl->ResetImfManager();
2270 PasteText( stringToPaste );
2273 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2275 if( NULL == mImpl->mEventData )
2282 case Toolkit::TextSelectionPopup::CUT:
2284 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2285 mImpl->mOperationsPending = ALL_OPERATIONS;
2287 // This is to reset the virtual keyboard to Upper-case
2288 if( 0u == mImpl->mLogicalModel->mText.Count() )
2293 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2294 !mImpl->IsPlaceholderAvailable() )
2296 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2300 ShowPlaceholderText();
2301 mImpl->mEventData->mUpdateCursorPosition = true;
2303 mImpl->RequestRelayout();
2304 mImpl->mControlInterface.TextChanged();
2307 case Toolkit::TextSelectionPopup::COPY:
2309 mImpl->SendSelectionToClipboard( false ); // Text not modified
2310 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
2313 case Toolkit::TextSelectionPopup::PASTE:
2315 std::string stringToPaste("");
2316 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2317 PasteText( stringToPaste );
2320 case Toolkit::TextSelectionPopup::SELECT:
2322 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2324 if( mImpl->mEventData->mSelectionEnabled )
2326 // Creates a SELECT event.
2327 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2331 case Toolkit::TextSelectionPopup::SELECT_ALL:
2333 // Creates a SELECT_ALL event
2334 SelectEvent( 0.f, 0.f, true );
2337 case Toolkit::TextSelectionPopup::CLIPBOARD:
2339 mImpl->ShowClipboard();
2342 case Toolkit::TextSelectionPopup::NONE:
2350 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2352 bool update = false;
2353 bool requestRelayout = false;
2356 unsigned int cursorPosition = 0u;
2358 switch( imfEvent.eventName )
2360 case ImfManager::COMMIT:
2362 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2364 requestRelayout = true;
2367 case ImfManager::PREEDIT:
2369 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2371 requestRelayout = true;
2374 case ImfManager::DELETESURROUNDING:
2376 update = RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
2380 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2381 !mImpl->IsPlaceholderAvailable() )
2383 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2387 ShowPlaceholderText();
2388 mImpl->mEventData->mUpdateCursorPosition = true;
2391 requestRelayout = true;
2394 case ImfManager::GETSURROUNDING:
2397 cursorPosition = GetLogicalCursorPosition();
2399 imfManager.SetSurroundingText( text );
2400 imfManager.SetCursorPosition( cursorPosition );
2403 case ImfManager::VOID:
2410 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
2413 cursorPosition = GetLogicalCursorPosition();
2416 if( requestRelayout )
2418 mImpl->mOperationsPending = ALL_OPERATIONS;
2419 mImpl->RequestRelayout();
2421 // Do this last since it provides callbacks into application code
2422 mImpl->mControlInterface.TextChanged();
2425 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
2427 return callbackData;
2430 Controller::~Controller()
2435 bool Controller::BackspaceKeyEvent()
2437 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2439 bool removed = false;
2441 if( NULL == mImpl->mEventData )
2446 // IMF manager is no longer handling key-events
2447 mImpl->ClearPreEditFlag();
2449 if( EventData::SELECTING == mImpl->mEventData->mState )
2451 removed = RemoveSelectedText();
2453 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2455 // Remove the character before the current cursor position
2456 removed = RemoveText( -1, 1 );
2461 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE RemovedText\n", this );
2462 // Notifiy the IMF manager after text changed
2463 // Automatic Upper-case and restarting prediction on an existing word require this.
2466 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2467 !mImpl->IsPlaceholderAvailable() )
2469 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2473 ShowPlaceholderText();
2474 mImpl->mEventData->mUpdateCursorPosition = true;
2481 void Controller::NotifyImfManager()
2483 if( NULL != mImpl->mEventData )
2485 if( mImpl->mEventData->mImfManager )
2487 // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
2490 mImpl->mEventData->mImfManager.SetSurroundingText( text );
2492 mImpl->mEventData->mImfManager.SetCursorPosition( GetLogicalCursorPosition() );
2493 mImpl->mEventData->mImfManager.NotifyCursorPosition();
2498 void Controller::ShowPlaceholderText()
2500 if( mImpl->IsPlaceholderAvailable() )
2502 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2504 if( NULL == mImpl->mEventData )
2509 mImpl->mEventData->mIsShowingPlaceholderText = true;
2511 // Disable handles when showing place-holder text
2512 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2513 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2514 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2516 const char* text( NULL );
2519 // TODO - Switch placeholder text styles when changing state
2520 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2521 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2523 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2524 size = mImpl->mEventData->mPlaceholderTextActive.size();
2528 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2529 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2532 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2533 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2535 // Reset model for showing placeholder.
2536 mImpl->mLogicalModel->mText.Clear();
2537 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2539 // Convert text into UTF-32
2540 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2541 utf32Characters.Resize( size );
2543 // This is a bit horrible but std::string returns a (signed) char*
2544 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2546 // Transform a text array encoded in utf8 into an array encoded in utf32.
2547 // It returns the actual number of characters.
2548 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2549 utf32Characters.Resize( characterCount );
2551 // The characters to be added.
2552 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = characterCount;
2554 // Reset the cursor position
2555 mImpl->mEventData->mPrimaryCursorPosition = 0;
2557 // The natural size needs to be re-calculated.
2558 mImpl->mRecalculateNaturalSize = true;
2560 // Apply modifications to the model
2561 mImpl->mOperationsPending = ALL_OPERATIONS;
2563 // Update the rest of the model during size negotiation
2564 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2568 void Controller::ClearFontData()
2570 if( mImpl->mFontDefaults )
2572 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2574 mImpl->mVisualModel->ClearCaches();
2576 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2577 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2578 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
2580 mImpl->mTextUpdateInfo.mClearAll = true;
2581 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2586 UPDATE_ACTUAL_SIZE |
2591 void Controller::ClearStyleData()
2593 mImpl->mLogicalModel->mColorRuns.Clear();
2594 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2597 Controller::Controller( ControlInterface& controlInterface )
2600 mImpl = new Controller::Impl( controlInterface );
2605 } // namespace Toolkit