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>
23 #include <dali/public-api/adaptor-framework/key.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/devel-api/adaptor-framework/clipboard-event-notifier.h>
28 #include <dali-toolkit/internal/text/bidirectional-support.h>
29 #include <dali-toolkit/internal/text/character-set-conversion.h>
30 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
31 #include <dali-toolkit/internal/text/text-controller-impl.h>
36 #if defined(DEBUG_ENABLED)
37 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_CONTROLS");
40 const float MAX_FLOAT = std::numeric_limits<float>::max();
42 const std::string EMPTY_STRING("");
44 float ConvertToEven( float value )
46 int intValue(static_cast<int>( value ));
47 return static_cast<float>(intValue % 2 == 0) ? intValue : (intValue + 1);
61 ControllerPtr Controller::New( ControlInterface& controlInterface )
63 return ControllerPtr( new Controller( controlInterface ) );
66 void Controller::EnableTextInput( DecoratorPtr decorator )
68 if( !mImpl->mEventData )
70 mImpl->mEventData = new EventData( decorator );
74 void Controller::SetText( const std::string& text )
76 // Remove the previously set text
79 CharacterIndex lastCursorIndex = 0u;
81 if( mImpl->mEventData )
83 // If popup shown then hide it by switching to Editing state
84 if ( EventData::SELECTING == mImpl->mEventData->mState ||
85 EventData::SELECTION_CHANGED == mImpl->mEventData->mState ||
86 EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState )
88 mImpl->ChangeState( EventData::EDITING );
94 // Convert text into UTF-32
95 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
96 utf32Characters.Resize( text.size() );
98 // This is a bit horrible but std::string returns a (signed) char*
99 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
101 // Transform a text array encoded in utf8 into an array encoded in utf32.
102 // It returns the actual number of characters.
103 Length characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
104 utf32Characters.Resize( characterCount );
106 DALI_ASSERT_DEBUG( text.size() >= characterCount && "Invalid UTF32 conversion length" );
107 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, text.size(), mImpl->mLogicalModel->mText.Count() );
109 // To reset the cursor position
110 lastCursorIndex = characterCount;
112 // Update the rest of the model during size negotiation
113 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
115 // The natural size needs to be re-calculated.
116 mImpl->mRecalculateNaturalSize = true;
118 // Apply modifications to the model
119 mImpl->mOperationsPending = ALL_OPERATIONS;
123 ShowPlaceholderText();
126 // Resets the cursor position.
127 ResetCursorPosition( lastCursorIndex );
129 // Scrolls the text to make the cursor visible.
130 ResetScrollPosition();
132 mImpl->RequestRelayout();
134 if( mImpl->mEventData )
136 // Cancel previously queued events
137 mImpl->mEventData->mEventQueue.clear();
140 // Reset keyboard as text changed
141 mImpl->ResetImfManager();
143 // Do this last since it provides callbacks into application code
144 mImpl->mControlInterface.TextChanged();
147 void Controller::GetText( std::string& text ) const
149 if( ! mImpl->IsShowingPlaceholderText() )
151 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
153 if( 0u != utf32Characters.Count() )
155 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
160 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
164 unsigned int Controller::GetLogicalCursorPosition() const
166 if( mImpl->mEventData )
168 return mImpl->mEventData->mPrimaryCursorPosition;
174 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
176 if( mImpl->mEventData )
178 if( PLACEHOLDER_TYPE_INACTIVE == type )
180 mImpl->mEventData->mPlaceholderTextInactive = text;
184 mImpl->mEventData->mPlaceholderTextActive = text;
187 // Update placeholder if there is no text
188 if( mImpl->IsShowingPlaceholderText() ||
189 0u == mImpl->mLogicalModel->mText.Count() )
191 ShowPlaceholderText();
196 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
198 if( mImpl->mEventData )
200 if( PLACEHOLDER_TYPE_INACTIVE == type )
202 text = mImpl->mEventData->mPlaceholderTextInactive;
206 text = mImpl->mEventData->mPlaceholderTextActive;
211 void Controller::SetMaximumNumberOfCharacters( int maxCharacters )
213 if ( maxCharacters >= 0 )
215 mImpl->mMaximumNumberOfCharacters = maxCharacters;
219 int Controller::GetMaximumNumberOfCharacters()
221 return mImpl->mMaximumNumberOfCharacters;
224 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
226 if( !mImpl->mFontDefaults )
228 mImpl->mFontDefaults = new FontDefaults();
231 mImpl->mFontDefaults->mDefaultFontFamily = defaultFontFamily;
233 // Clear the font-specific data
236 mImpl->mOperationsPending = ALL_OPERATIONS;
237 mImpl->mRecalculateNaturalSize = true;
239 mImpl->RequestRelayout();
242 const std::string& Controller::GetDefaultFontFamily() const
244 if( mImpl->mFontDefaults )
246 return mImpl->mFontDefaults->mDefaultFontFamily;
252 void Controller::SetDefaultFontStyle( const std::string& defaultFontStyle )
254 if( !mImpl->mFontDefaults )
256 mImpl->mFontDefaults = new FontDefaults();
259 mImpl->mFontDefaults->mDefaultFontStyle = defaultFontStyle;
261 // Clear the font-specific data
264 mImpl->mOperationsPending = ALL_OPERATIONS;
265 mImpl->mRecalculateNaturalSize = true;
267 mImpl->RequestRelayout();
270 const std::string& Controller::GetDefaultFontStyle() const
272 if( mImpl->mFontDefaults )
274 return mImpl->mFontDefaults->mDefaultFontStyle;
280 void Controller::SetDefaultPointSize( float pointSize )
282 if( !mImpl->mFontDefaults )
284 mImpl->mFontDefaults = new FontDefaults();
287 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
289 // Clear the font-specific data
292 mImpl->mOperationsPending = ALL_OPERATIONS;
293 mImpl->mRecalculateNaturalSize = true;
295 mImpl->RequestRelayout();
298 float Controller::GetDefaultPointSize() const
300 if( mImpl->mFontDefaults )
302 return mImpl->mFontDefaults->mDefaultPointSize;
308 void Controller::SetTextColor( const Vector4& textColor )
310 mImpl->mTextColor = textColor;
312 if( !mImpl->IsShowingPlaceholderText() )
314 mImpl->mVisualModel->SetTextColor( textColor );
316 mImpl->RequestRelayout();
320 const Vector4& Controller::GetTextColor() const
322 return mImpl->mTextColor;
325 bool Controller::RemoveText( int cursorOffset, int numberOfChars )
327 bool removed( false );
329 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfChars %d\n",
330 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfChars );
332 if( !mImpl->IsShowingPlaceholderText() )
334 // Delete at current cursor position
335 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
336 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
338 CharacterIndex cursorIndex = oldCursorIndex;
340 // Validate the cursor position & number of characters
341 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
343 cursorIndex = oldCursorIndex + cursorOffset;
346 if( (cursorIndex + numberOfChars) > currentText.Count() )
348 numberOfChars = currentText.Count() - cursorIndex;
351 if( (cursorIndex + numberOfChars) <= currentText.Count() )
353 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
354 Vector<Character>::Iterator last = first + numberOfChars;
356 currentText.Erase( first, last );
358 // Cursor position retreat
359 oldCursorIndex = cursorIndex;
361 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfChars );
369 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
371 if( mImpl->mEventData )
373 mImpl->mEventData->mPlaceholderTextColor = textColor;
376 if( mImpl->IsShowingPlaceholderText() )
378 mImpl->mVisualModel->SetTextColor( textColor );
379 mImpl->RequestRelayout();
383 const Vector4& Controller::GetPlaceholderTextColor() const
385 if( mImpl->mEventData )
387 return mImpl->mEventData->mPlaceholderTextColor;
393 void Controller::SetShadowOffset( const Vector2& shadowOffset )
395 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
397 mImpl->RequestRelayout();
400 const Vector2& Controller::GetShadowOffset() const
402 return mImpl->mVisualModel->GetShadowOffset();
405 void Controller::SetShadowColor( const Vector4& shadowColor )
407 mImpl->mVisualModel->SetShadowColor( shadowColor );
409 mImpl->RequestRelayout();
412 const Vector4& Controller::GetShadowColor() const
414 return mImpl->mVisualModel->GetShadowColor();
417 void Controller::SetUnderlineColor( const Vector4& color )
419 mImpl->mVisualModel->SetUnderlineColor( color );
421 mImpl->RequestRelayout();
424 const Vector4& Controller::GetUnderlineColor() const
426 return mImpl->mVisualModel->GetUnderlineColor();
429 void Controller::SetUnderlineEnabled( bool enabled )
431 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
433 mImpl->RequestRelayout();
436 bool Controller::IsUnderlineEnabled() const
438 return mImpl->mVisualModel->IsUnderlineEnabled();
441 void Controller::SetUnderlineHeight( float height )
443 mImpl->mVisualModel->SetUnderlineHeight( height );
445 mImpl->RequestRelayout();
448 float Controller::GetUnderlineHeight() const
450 return mImpl->mVisualModel->GetUnderlineHeight();
453 void Controller::SetEnableCursorBlink( bool enable )
455 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
457 if( mImpl->mEventData )
459 mImpl->mEventData->mCursorBlinkEnabled = enable;
462 mImpl->mEventData->mDecorator )
464 mImpl->mEventData->mDecorator->StopCursorBlink();
469 bool Controller::GetEnableCursorBlink() const
471 if( mImpl->mEventData )
473 return mImpl->mEventData->mCursorBlinkEnabled;
479 const Vector2& Controller::GetScrollPosition() const
481 if( mImpl->mEventData )
483 return mImpl->mEventData->mScrollPosition;
486 return Vector2::ZERO;
489 const Vector2& Controller::GetAlignmentOffset() const
491 return mImpl->mAlignmentOffset;
494 Vector3 Controller::GetNaturalSize()
496 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
499 // Make sure the model is up-to-date before layouting
500 ProcessModifyEvents();
502 if( mImpl->mRecalculateNaturalSize )
504 // Operations that can be done only once until the text changes.
505 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
513 // Make sure the model is up-to-date before layouting
514 mImpl->UpdateModel( onlyOnceOperations );
516 // Operations that need to be done if the size changes.
517 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
521 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
522 static_cast<OperationsMask>( onlyOnceOperations |
524 naturalSize.GetVectorXY() );
526 // Do not do again the only once operations.
527 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
529 // Do the size related operations again.
530 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
532 // Stores the natural size to avoid recalculate it again
533 // unless the text/style changes.
534 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
536 mImpl->mRecalculateNaturalSize = false;
538 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
542 naturalSize = mImpl->mVisualModel->GetNaturalSize();
544 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
547 naturalSize.x = ConvertToEven( naturalSize.x );
548 naturalSize.y = ConvertToEven( naturalSize.y );
553 float Controller::GetHeightForWidth( float width )
555 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
556 // Make sure the model is up-to-date before layouting
557 ProcessModifyEvents();
560 if( width != mImpl->mVisualModel->mControlSize.width )
562 // Operations that can be done only once until the text changes.
563 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
571 // Make sure the model is up-to-date before layouting
572 mImpl->UpdateModel( onlyOnceOperations );
574 // Operations that need to be done if the size changes.
575 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
579 DoRelayout( Size( width, MAX_FLOAT ),
580 static_cast<OperationsMask>( onlyOnceOperations |
584 // Do not do again the only once operations.
585 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
587 // Do the size related operations again.
588 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
589 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
593 layoutSize = mImpl->mVisualModel->GetActualSize();
594 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
597 return layoutSize.height;
600 bool Controller::Relayout( const Size& size )
602 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
604 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
606 bool glyphsRemoved( false );
607 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
609 mImpl->mVisualModel->mGlyphPositions.Clear();
610 glyphsRemoved = true;
612 // Not worth to relayout if width or height is equal to zero.
613 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
614 return glyphsRemoved;
617 if( size != mImpl->mVisualModel->mControlSize )
619 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
621 // Operations that need to be done if the size changes.
622 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
628 mImpl->mVisualModel->mControlSize = size;
631 // Make sure the model is up-to-date before layouting
632 ProcessModifyEvents();
633 mImpl->UpdateModel( mImpl->mOperationsPending );
636 bool updated = DoRelayout( mImpl->mVisualModel->mControlSize,
637 mImpl->mOperationsPending,
640 // Do not re-do any operation until something changes.
641 mImpl->mOperationsPending = NO_OPERATION;
643 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
644 CalculateTextAlignment( size );
646 if( mImpl->mEventData )
648 // Move the cursor, grab handle etc.
649 updated = mImpl->ProcessInputEvents() || updated;
652 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
656 void Controller::ProcessModifyEvents()
658 std::vector<ModifyEvent>& events = mImpl->mModifyEvents;
660 for( unsigned int i=0; i<events.size(); ++i )
662 if( ModifyEvent::TEXT_REPLACED == events[0].type )
664 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
665 DALI_ASSERT_DEBUG( 0 == i && "Unexpected TEXT_REPLACED event" );
669 else if( ModifyEvent::TEXT_INSERTED == events[0].type )
673 else if( ModifyEvent::TEXT_DELETED == events[0].type )
675 // Placeholder-text cannot be deleted
676 if( !mImpl->IsShowingPlaceholderText() )
683 // Discard temporary text
687 void Controller::ResetText()
690 mImpl->mLogicalModel->mText.Clear();
693 // We have cleared everything including the placeholder-text
694 mImpl->PlaceholderCleared();
696 // The natural size needs to be re-calculated.
697 mImpl->mRecalculateNaturalSize = true;
699 // Apply modifications to the model
700 mImpl->mOperationsPending = ALL_OPERATIONS;
703 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
705 // Reset the cursor position
706 if( NULL != mImpl->mEventData )
708 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
710 // Update the cursor if it's in editing mode.
711 if( ( EventData::EDITING == mImpl->mEventData->mState ) ||
712 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) )
714 mImpl->mEventData->mUpdateCursorPosition = true;
719 void Controller::ResetScrollPosition()
721 if( NULL != mImpl->mEventData )
723 // Reset the scroll position.
724 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
725 mImpl->mEventData->mScrollAfterUpdatePosition = true;
729 void Controller::TextReplacedEvent()
734 // The natural size needs to be re-calculated.
735 mImpl->mRecalculateNaturalSize = true;
737 // Apply modifications to the model
738 mImpl->mOperationsPending = ALL_OPERATIONS;
739 mImpl->UpdateModel( ALL_OPERATIONS );
740 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
746 void Controller::TextInsertedEvent()
748 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
750 // TODO - Optimize this
753 // The natural size needs to be re-calculated.
754 mImpl->mRecalculateNaturalSize = true;
756 // Apply modifications to the model; TODO - Optimize this
757 mImpl->mOperationsPending = ALL_OPERATIONS;
758 mImpl->UpdateModel( ALL_OPERATIONS );
759 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
764 // Queue a cursor reposition event; this must wait until after DoRelayout()
765 mImpl->mEventData->mUpdateCursorPosition = true;
766 mImpl->mEventData->mScrollAfterUpdatePosition = true;
769 void Controller::TextDeletedEvent()
771 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
773 // TODO - Optimize this
776 // The natural size needs to be re-calculated.
777 mImpl->mRecalculateNaturalSize = true;
779 // Apply modifications to the model; TODO - Optimize this
780 mImpl->mOperationsPending = ALL_OPERATIONS;
781 mImpl->UpdateModel( ALL_OPERATIONS );
782 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
787 // Queue a cursor reposition event; this must wait until after DoRelayout()
788 if( 0u == mImpl->mLogicalModel->mText.Count() )
790 mImpl->mEventData->mUpdateCursorPosition = true;
794 mImpl->mEventData->mScrollAfterDelete = true;
798 bool Controller::DoRelayout( const Size& size,
799 OperationsMask operationsRequired,
802 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
803 bool viewUpdated( false );
805 // Calculate the operations to be done.
806 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
808 if( LAYOUT & operations )
810 // Some vectors with data needed to layout and reorder may be void
811 // after the first time the text has been laid out.
812 // Fill the vectors again.
814 const Length numberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
816 if( 0u == numberOfGlyphs )
818 // Nothing else to do if there is no glyphs.
819 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
823 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
824 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
825 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
826 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
827 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
828 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
829 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
831 // Set the layout parameters.
832 LayoutParameters layoutParameters( size,
834 lineBreakInfo.Begin(),
835 wordBreakInfo.Begin(),
836 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
839 glyphsToCharactersMap.Begin(),
840 charactersPerGlyph.Begin() );
842 // The laid-out lines.
843 // It's not possible to know in how many lines the text is going to be laid-out,
844 // but it can be resized at least with the number of 'paragraphs' to avoid
845 // some re-allocations.
846 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
848 // Delete any previous laid out lines before setting the new ones.
851 // The capacity of the bidirectional paragraph info is the number of paragraphs.
852 lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
854 // Resize the vector of positions to have the same size than the vector of glyphs.
855 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
856 glyphPositions.Resize( numberOfGlyphs );
858 // Whether the last character is a new paragraph character.
859 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
861 // Update the visual model.
862 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
870 if( REORDER & operations )
872 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
874 // Check first if there are paragraphs with bidirectional info.
875 if( 0u != bidirectionalInfo.Count() )
878 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
880 // Reorder the lines.
881 Vector<BidirectionalLineInfoRun> lineBidirectionalInfoRuns;
882 lineBidirectionalInfoRuns.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
883 ReorderLines( bidirectionalInfo,
885 lineBidirectionalInfoRuns );
887 // Set the bidirectional info into the model.
888 const Length numberOfBidirectionalInfoRuns = lineBidirectionalInfoRuns.Count();
889 mImpl->mLogicalModel->SetVisualToLogicalMap( lineBidirectionalInfoRuns.Begin(),
890 numberOfBidirectionalInfoRuns );
892 // Set the bidirectional info per line into the layout parameters.
893 layoutParameters.lineBidirectionalInfoRunsBuffer = lineBidirectionalInfoRuns.Begin();
894 layoutParameters.numberOfBidirectionalInfoRuns = numberOfBidirectionalInfoRuns;
896 // Get the character to glyph conversion table and set into the layout.
897 layoutParameters.charactersToGlyphsBuffer = mImpl->mVisualModel->mCharactersToGlyph.Begin();
899 // Get the glyphs per character table and set into the layout.
900 layoutParameters.glyphsPerCharacterBuffer = mImpl->mVisualModel->mGlyphsPerCharacter.Begin();
902 // Re-layout the text. Reorder those lines with right to left characters.
903 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
906 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
907 for( Vector<BidirectionalLineInfoRun>::Iterator it = lineBidirectionalInfoRuns.Begin(),
908 endIt = lineBidirectionalInfoRuns.End();
912 BidirectionalLineInfoRun& bidiLineInfo = *it;
914 free( bidiLineInfo.visualToLogicalMap );
919 // Sets the actual size.
920 if( UPDATE_ACTUAL_SIZE & operations )
922 mImpl->mVisualModel->SetActualSize( layoutSize );
928 layoutSize = mImpl->mVisualModel->GetActualSize();
931 if( ALIGN & operations )
933 // The laid-out lines.
934 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
936 mImpl->mLayoutEngine.Align( layoutSize,
942 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
946 void Controller::SetMultiLineEnabled( bool enable )
948 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
950 if( layout != mImpl->mLayoutEngine.GetLayout() )
952 // Set the layout type.
953 mImpl->mLayoutEngine.SetLayout( layout );
955 // Set the flags to redo the layout operations
956 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
961 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
963 mImpl->RequestRelayout();
967 bool Controller::IsMultiLineEnabled() const
969 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
972 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
974 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
976 // Set the alignment.
977 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
979 // Set the flag to redo the alignment operation.
980 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
982 mImpl->RequestRelayout();
986 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
988 return mImpl->mLayoutEngine.GetHorizontalAlignment();
991 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
993 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
995 // Set the alignment.
996 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
998 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1000 mImpl->RequestRelayout();
1004 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1006 return mImpl->mLayoutEngine.GetVerticalAlignment();
1009 void Controller::CalculateTextAlignment( const Size& size )
1011 // Get the direction of the first character.
1012 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1014 Size actualSize = mImpl->mVisualModel->GetActualSize();
1015 if( fabsf( actualSize.height ) < Math::MACHINE_EPSILON_1000 )
1017 // Get the line height of the default font.
1018 actualSize.height = mImpl->GetDefaultFontLineHeight();
1021 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1022 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1023 if( firstParagraphDirection &&
1024 ( LayoutEngine::HORIZONTAL_ALIGN_CENTER != horizontalAlignment ) )
1026 if( LayoutEngine::HORIZONTAL_ALIGN_BEGIN == horizontalAlignment )
1028 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1032 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1036 switch( horizontalAlignment )
1038 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1040 mImpl->mAlignmentOffset.x = 0.f;
1043 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1045 const int intOffset = static_cast<int>( 0.5f * ( size.width - actualSize.width ) ); // try to avoid pixel alignment.
1046 mImpl->mAlignmentOffset.x = static_cast<float>( intOffset );
1049 case LayoutEngine::HORIZONTAL_ALIGN_END:
1051 mImpl->mAlignmentOffset.x = size.width - actualSize.width;
1056 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1057 switch( verticalAlignment )
1059 case LayoutEngine::VERTICAL_ALIGN_TOP:
1061 mImpl->mAlignmentOffset.y = 0.f;
1064 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1066 const int intOffset = static_cast<int>( 0.5f * ( size.height - actualSize.height ) ); // try to avoid pixel alignment.
1067 mImpl->mAlignmentOffset.y = static_cast<float>( intOffset );
1070 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1072 mImpl->mAlignmentOffset.y = size.height - actualSize.height;
1078 LayoutEngine& Controller::GetLayoutEngine()
1080 return mImpl->mLayoutEngine;
1083 View& Controller::GetView()
1085 return mImpl->mView;
1088 void Controller::KeyboardFocusGainEvent()
1090 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1092 if( mImpl->mEventData )
1094 mImpl->ChangeState( EventData::EDITING );
1096 if( mImpl->IsShowingPlaceholderText() )
1098 // Show alternative placeholder-text when editing
1099 ShowPlaceholderText();
1102 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1103 mImpl->RequestRelayout();
1107 void Controller::KeyboardFocusLostEvent()
1109 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1111 if( mImpl->mEventData )
1113 if ( EventData::INTERRUPTED != mImpl->mEventData->mState )
1115 mImpl->ChangeState( EventData::INACTIVE );
1117 if( mImpl->IsShowingPlaceholderText() )
1119 // Revert to regular placeholder-text when not editing
1120 ShowPlaceholderText();
1124 mImpl->RequestRelayout();
1127 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1129 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1131 bool textChanged( false );
1133 if( mImpl->mEventData &&
1134 keyEvent.state == KeyEvent::Down )
1136 int keyCode = keyEvent.keyCode;
1137 const std::string& keyString = keyEvent.keyPressed;
1139 // Pre-process to separate modifying events from non-modifying input events.
1140 if( Dali::DALI_KEY_ESCAPE == keyCode )
1142 // Escape key is a special case which causes focus loss
1143 KeyboardFocusLostEvent();
1145 else if( Dali::DALI_KEY_CURSOR_LEFT == keyCode ||
1146 Dali::DALI_KEY_CURSOR_RIGHT == keyCode ||
1147 Dali::DALI_KEY_CURSOR_UP == keyCode ||
1148 Dali::DALI_KEY_CURSOR_DOWN == keyCode )
1150 Event event( Event::CURSOR_KEY_EVENT );
1151 event.p1.mInt = keyCode;
1152 mImpl->mEventData->mEventQueue.push_back( event );
1154 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1156 textChanged = BackspaceKeyEvent();
1158 else if ( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1160 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1161 // Avoids calling the InsertText() method which can delete selected text
1163 else if ( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1164 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1166 mImpl->ChangeState( EventData::INACTIVE );
1167 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1168 // Avoids calling the InsertText() method which can delete selected text
1170 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1172 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1173 // and a character is typed after the type of a upper case latin character.
1179 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1181 // IMF manager is no longer handling key-events
1182 mImpl->ClearPreEditFlag();
1184 InsertText( keyString, COMMIT );
1188 if ( mImpl->mEventData->mState != EventData::INTERRUPTED && mImpl->mEventData->mState != EventData::INACTIVE )
1190 mImpl->ChangeState( EventData::EDITING );
1193 mImpl->RequestRelayout();
1198 // Do this last since it provides callbacks into application code
1199 mImpl->mControlInterface.TextChanged();
1205 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1207 bool removedPrevious( false );
1208 bool maxLengthReached( false );
1210 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1211 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1212 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1213 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1215 // TODO: At the moment the underline runs are only for pre-edit.
1216 mImpl->mVisualModel->mUnderlineRuns.Clear();
1218 Vector<Character> utf32Characters;
1219 Length characterCount( 0u );
1221 // Remove the previous IMF pre-edit (predicitive text)
1222 if( mImpl->mEventData &&
1223 mImpl->mEventData->mPreEditFlag &&
1224 0 != mImpl->mEventData->mPreEditLength )
1226 CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1227 removedPrevious = RemoveText( -static_cast<int>(offset), mImpl->mEventData->mPreEditLength );
1229 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1230 mImpl->mEventData->mPreEditLength = 0;
1234 // Remove the previous Selection
1235 removedPrevious = RemoveSelectedText();
1238 if( ! text.empty() )
1240 // Convert text into UTF-32
1241 utf32Characters.Resize( text.size() );
1243 // This is a bit horrible but std::string returns a (signed) char*
1244 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1246 // Transform a text array encoded in utf8 into an array encoded in utf32.
1247 // It returns the actual number of characters.
1248 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1249 utf32Characters.Resize( characterCount );
1251 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1252 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1255 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1257 // The placeholder text is no longer needed
1258 if( mImpl->IsShowingPlaceholderText() )
1263 mImpl->ChangeState( EventData::EDITING );
1265 // Handle the IMF (predicitive text) state changes
1266 if( mImpl->mEventData )
1268 if( COMMIT == type )
1270 // IMF manager is no longer handling key-events
1271 mImpl->ClearPreEditFlag();
1275 if( !mImpl->mEventData->mPreEditFlag )
1277 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1279 // Record the start of the pre-edit text
1280 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1283 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1284 mImpl->mEventData->mPreEditFlag = true;
1286 // Add the underline for the pre-edit text.
1287 const GlyphIndex* const charactersToGlyphBuffer = mImpl->mVisualModel->mCharactersToGlyph.Begin();
1288 const Length* const glyphsPerCharacterBuffer = mImpl->mVisualModel->mGlyphsPerCharacter.Begin();
1290 const GlyphIndex glyphStart = *( charactersToGlyphBuffer + mImpl->mEventData->mPreEditStartPosition );
1291 const CharacterIndex lastPreEditCharacter = mImpl->mEventData->mPreEditStartPosition + ( ( mImpl->mEventData->mPreEditLength > 0u ) ? mImpl->mEventData->mPreEditLength - 1u : 0u );
1292 const Length numberOfGlyphsLastCharacter = *( glyphsPerCharacterBuffer + lastPreEditCharacter );
1293 const GlyphIndex glyphEnd = *( charactersToGlyphBuffer + lastPreEditCharacter ) + ( numberOfGlyphsLastCharacter > 1u ? numberOfGlyphsLastCharacter - 1u : 0u );
1295 GlyphRun underlineRun;
1296 underlineRun.glyphIndex = glyphStart;
1297 underlineRun.numberOfGlyphs = 1u + glyphEnd - glyphStart;
1299 // TODO: At the moment the underline runs are only for pre-edit.
1300 mImpl->mVisualModel->mUnderlineRuns.PushBack( underlineRun );
1302 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1306 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1308 // Restrict new text to fit within Maximum characters setting
1309 Length maxSizeOfNewText = std::min ( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1310 maxLengthReached = ( characterCount > maxSizeOfNewText );
1312 // Insert at current cursor position
1313 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1315 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1317 if( cursorIndex < numberOfCharactersInModel )
1319 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1323 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1326 cursorIndex += maxSizeOfNewText;
1328 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1331 if( 0u == mImpl->mLogicalModel->mText.Count() &&
1332 mImpl->IsPlaceholderAvailable() )
1334 // Show place-holder if empty after removing the pre-edit text
1335 ShowPlaceholderText();
1336 mImpl->mEventData->mUpdateCursorPosition = true;
1337 mImpl->ClearPreEditFlag();
1339 else if( removedPrevious ||
1340 0 != utf32Characters.Count() )
1342 // Queue an inserted event
1343 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1346 if( maxLengthReached )
1348 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1350 mImpl->ResetImfManager();
1352 // Do this last since it provides callbacks into application code
1353 mImpl->mControlInterface.MaxLengthReached();
1357 bool Controller::RemoveSelectedText()
1359 bool textRemoved( false );
1361 if ( EventData::SELECTING == mImpl->mEventData->mState ||
1362 EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
1364 std::string removedString;
1365 mImpl->RetrieveSelection( removedString, true );
1367 if( !removedString.empty() )
1370 mImpl->ChangeState( EventData::EDITING );
1377 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1379 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1381 if( NULL != mImpl->mEventData )
1383 const bool isShowingPlaceholderText = mImpl->IsShowingPlaceholderText();
1384 if( 1u == tapCount )
1386 if( !isShowingPlaceholderText &&
1387 ( EventData::EDITING == mImpl->mEventData->mState ) )
1389 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
1391 else if( EventData::EDITING_WITH_GRAB_HANDLE != mImpl->mEventData->mState )
1393 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
1394 mImpl->ChangeState( EventData::EDITING );
1397 Event event( Event::TAP_EVENT );
1398 event.p1.mUint = tapCount;
1399 event.p2.mFloat = x;
1400 event.p3.mFloat = y;
1401 mImpl->mEventData->mEventQueue.push_back( event );
1403 mImpl->RequestRelayout();
1405 else if( !isShowingPlaceholderText &&
1406 mImpl->mEventData->mSelectionEnabled &&
1407 ( 2u == tapCount ) )
1409 SelectEvent( x, y, false );
1413 // Reset keyboard as tap event has occurred.
1414 mImpl->ResetImfManager();
1417 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
1419 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1421 if( mImpl->mEventData )
1423 Event event( Event::PAN_EVENT );
1424 event.p1.mInt = state;
1425 event.p2.mFloat = displacement.x;
1426 event.p3.mFloat = displacement.y;
1427 mImpl->mEventData->mEventQueue.push_back( event );
1429 mImpl->RequestRelayout();
1433 void Controller::LongPressEvent( Gesture::State state, float x, float y )
1435 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1437 if ( mImpl->IsShowingPlaceholderText() || mImpl->mLogicalModel->mText.Count() == 0u )
1439 if ( mImpl->mEventData )
1441 Event event( Event::LONG_PRESS_EVENT );
1442 event.p1.mInt = state;
1443 mImpl->mEventData->mEventQueue.push_back( event );
1444 mImpl->RequestRelayout();
1447 else if( mImpl->mEventData )
1449 SelectEvent( x, y, false );
1453 void Controller::SelectEvent( float x, float y, bool selectAll )
1455 if( mImpl->mEventData )
1457 if ( mImpl->mEventData->mState == EventData::SELECTING )
1459 mImpl->ChangeState( EventData::SELECTION_CHANGED );
1463 mImpl->ChangeState( EventData::SELECTING );
1468 Event event( Event::SELECT_ALL );
1469 mImpl->mEventData->mEventQueue.push_back( event );
1473 Event event( Event::SELECT );
1474 event.p2.mFloat = x;
1475 event.p3.mFloat = y;
1476 mImpl->mEventData->mEventQueue.push_back( event );
1479 mImpl->RequestRelayout();
1483 void Controller::GetTargetSize( Vector2& targetSize )
1485 targetSize = mImpl->mVisualModel->mControlSize;
1488 void Controller::AddDecoration( Actor& actor, bool needsClipping )
1490 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
1493 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
1495 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
1497 if( mImpl->mEventData )
1499 switch( handleType )
1503 Event event( Event::GRAB_HANDLE_EVENT );
1504 event.p1.mUint = state;
1505 event.p2.mFloat = x;
1506 event.p3.mFloat = y;
1508 mImpl->mEventData->mEventQueue.push_back( event );
1511 case LEFT_SELECTION_HANDLE:
1513 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
1514 event.p1.mUint = state;
1515 event.p2.mFloat = x;
1516 event.p3.mFloat = y;
1518 mImpl->mEventData->mEventQueue.push_back( event );
1521 case RIGHT_SELECTION_HANDLE:
1523 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
1524 event.p1.mUint = state;
1525 event.p2.mFloat = x;
1526 event.p3.mFloat = y;
1528 mImpl->mEventData->mEventQueue.push_back( event );
1531 case LEFT_SELECTION_HANDLE_MARKER:
1532 case RIGHT_SELECTION_HANDLE_MARKER:
1534 // Markers do not move the handles.
1537 case HANDLE_TYPE_COUNT:
1539 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
1543 mImpl->RequestRelayout();
1547 void Controller::PasteText( const std::string& stringToPaste )
1549 InsertText( stringToPaste, Text::Controller::COMMIT );
1550 mImpl->ChangeState( EventData::EDITING );
1551 mImpl->RequestRelayout();
1554 void Controller::PasteClipboardItemEvent()
1556 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
1557 std::string stringToPaste( notifier.GetContent() );
1558 PasteText( stringToPaste );
1561 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
1563 if( NULL == mImpl->mEventData )
1570 case Toolkit::TextSelectionPopup::CUT:
1572 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
1573 mImpl->mOperationsPending = ALL_OPERATIONS;
1574 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1575 !mImpl->IsPlaceholderAvailable() )
1577 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1581 ShowPlaceholderText();
1582 mImpl->mEventData->mUpdateCursorPosition = true;
1584 mImpl->RequestRelayout();
1585 mImpl->mControlInterface.TextChanged();
1588 case Toolkit::TextSelectionPopup::COPY:
1590 mImpl->SendSelectionToClipboard( false ); // Text not modified
1591 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
1594 case Toolkit::TextSelectionPopup::PASTE:
1596 std::string stringToPaste("");
1597 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
1598 PasteText( stringToPaste );
1601 case Toolkit::TextSelectionPopup::SELECT:
1603 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
1605 if( mImpl->mEventData->mSelectionEnabled )
1607 // Creates a SELECT event.
1608 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
1612 case Toolkit::TextSelectionPopup::SELECT_ALL:
1614 // Creates a SELECT_ALL event
1615 SelectEvent( 0.f, 0.f, true );
1618 case Toolkit::TextSelectionPopup::CLIPBOARD:
1620 mImpl->ShowClipboard();
1623 case Toolkit::TextSelectionPopup::NONE:
1631 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
1633 bool update( false );
1634 bool requestRelayout = false;
1637 unsigned int cursorPosition( 0 );
1639 switch ( imfEvent.eventName )
1641 case ImfManager::COMMIT:
1643 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
1644 requestRelayout = true;
1647 case ImfManager::PREEDIT:
1649 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
1651 requestRelayout = true;
1654 case ImfManager::DELETESURROUNDING:
1656 update = RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
1660 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1661 !mImpl->IsPlaceholderAvailable() )
1663 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1667 ShowPlaceholderText();
1668 mImpl->mEventData->mUpdateCursorPosition = true;
1671 requestRelayout = true;
1674 case ImfManager::GETSURROUNDING:
1677 cursorPosition = GetLogicalCursorPosition();
1679 imfManager.SetSurroundingText( text );
1680 imfManager.SetCursorPosition( cursorPosition );
1683 case ImfManager::VOID:
1690 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
1693 cursorPosition = GetLogicalCursorPosition();
1696 if( requestRelayout )
1698 mImpl->mOperationsPending = ALL_OPERATIONS;
1699 mImpl->RequestRelayout();
1701 // Do this last since it provides callbacks into application code
1702 mImpl->mControlInterface.TextChanged();
1705 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
1707 return callbackData;
1710 Controller::~Controller()
1715 bool Controller::BackspaceKeyEvent()
1717 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
1719 // IMF manager is no longer handling key-events
1720 mImpl->ClearPreEditFlag();
1722 bool removed( false );
1724 if ( EventData::SELECTING == mImpl->mEventData->mState ||
1725 EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
1727 removed = RemoveSelectedText();
1729 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
1731 // Remove the character before the current cursor position
1732 removed = RemoveText( -1, 1 );
1737 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1738 !mImpl->IsPlaceholderAvailable() )
1740 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1744 ShowPlaceholderText();
1745 mImpl->mEventData->mUpdateCursorPosition = true;
1752 void Controller::ShowPlaceholderText()
1754 if( mImpl->IsPlaceholderAvailable() )
1756 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
1758 mImpl->mEventData->mIsShowingPlaceholderText = true;
1760 // Disable handles when showing place-holder text
1761 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
1762 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
1763 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
1765 const char* text( NULL );
1768 // TODO - Switch placeholder text styles when changing state
1769 if( EventData::INACTIVE != mImpl->mEventData->mState &&
1770 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() )
1772 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
1773 size = mImpl->mEventData->mPlaceholderTextActive.size();
1777 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
1778 size = mImpl->mEventData->mPlaceholderTextInactive.size();
1781 // Reset model for showing placeholder.
1782 mImpl->mLogicalModel->mText.Clear();
1784 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
1786 // Convert text into UTF-32
1787 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
1788 utf32Characters.Resize( size );
1790 // This is a bit horrible but std::string returns a (signed) char*
1791 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
1793 // Transform a text array encoded in utf8 into an array encoded in utf32.
1794 // It returns the actual number of characters.
1795 Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
1796 utf32Characters.Resize( characterCount );
1798 // Reset the cursor position
1799 mImpl->mEventData->mPrimaryCursorPosition = 0;
1801 // The natural size needs to be re-calculated.
1802 mImpl->mRecalculateNaturalSize = true;
1804 // Apply modifications to the model
1805 mImpl->mOperationsPending = ALL_OPERATIONS;
1807 // Update the rest of the model during size negotiation
1808 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
1812 void Controller::ClearModelData()
1814 // n.b. This does not Clear the mText from mLogicalModel
1815 mImpl->mLogicalModel->mScriptRuns.Clear();
1816 mImpl->mLogicalModel->mFontRuns.Clear();
1817 mImpl->mLogicalModel->mLineBreakInfo.Clear();
1818 mImpl->mLogicalModel->mWordBreakInfo.Clear();
1819 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
1820 mImpl->mLogicalModel->mCharacterDirections.Clear();
1821 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
1822 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
1823 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
1824 mImpl->mVisualModel->mGlyphs.Clear();
1825 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
1826 mImpl->mVisualModel->mCharactersToGlyph.Clear();
1827 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
1828 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
1829 mImpl->mVisualModel->mGlyphPositions.Clear();
1830 mImpl->mVisualModel->mLines.Clear();
1831 mImpl->mVisualModel->ClearCaches();
1834 void Controller::ClearFontData()
1836 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
1837 mImpl->mLogicalModel->mFontRuns.Clear();
1838 mImpl->mVisualModel->mGlyphs.Clear();
1839 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
1840 mImpl->mVisualModel->mCharactersToGlyph.Clear();
1841 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
1842 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
1843 mImpl->mVisualModel->mGlyphPositions.Clear();
1844 mImpl->mVisualModel->mLines.Clear();
1845 mImpl->mVisualModel->ClearCaches();
1848 Controller::Controller( ControlInterface& controlInterface )
1851 mImpl = new Controller::Impl( controlInterface );
1856 } // namespace Toolkit