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/multi-language-support.h>
33 #include <dali-toolkit/internal/text/script-run.h>
34 #include <dali-toolkit/internal/text/segmentation.h>
35 #include <dali-toolkit/internal/text/shaper.h>
36 #include <dali-toolkit/internal/text/text-controller-impl.h>
37 #include <dali-toolkit/internal/text/text-io.h>
38 #include <dali-toolkit/internal/text/text-view.h>
43 #if defined(DEBUG_ENABLED)
44 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_CONTROLS");
47 const float MAX_FLOAT = std::numeric_limits<float>::max();
49 const std::string EMPTY_STRING("");
51 float ConvertToEven( float value )
53 int intValue(static_cast<int>( value ));
54 return static_cast<float>(intValue % 2 == 0) ? intValue : (intValue + 1);
68 ControllerPtr Controller::New( ControlInterface& controlInterface )
70 return ControllerPtr( new Controller( controlInterface ) );
73 void Controller::EnableTextInput( DecoratorPtr decorator )
75 if( !mImpl->mEventData )
77 mImpl->mEventData = new EventData( decorator );
81 void Controller::SetText( const std::string& text )
83 // Remove the previously set text
86 CharacterIndex lastCursorIndex = 0u;
88 if( mImpl->mEventData )
90 // If popup shown then hide it by switching to Editing state
91 if ( EventData::SELECTING == mImpl->mEventData->mState ||
92 EventData::SELECTION_CHANGED == mImpl->mEventData->mState ||
93 EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState )
95 mImpl->ChangeState( EventData::EDITING );
101 // Convert text into UTF-32
102 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
103 utf32Characters.Resize( text.size() );
105 // This is a bit horrible but std::string returns a (signed) char*
106 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
108 // Transform a text array encoded in utf8 into an array encoded in utf32.
109 // It returns the actual number of characters.
110 Length characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
111 utf32Characters.Resize( characterCount );
113 DALI_ASSERT_DEBUG( text.size() >= characterCount && "Invalid UTF32 conversion length" );
114 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, text.size(), mImpl->mLogicalModel->mText.Count() );
116 // To reset the cursor position
117 lastCursorIndex = characterCount;
119 // Update the rest of the model during size negotiation
120 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
122 // The natural size needs to be re-calculated.
123 mImpl->mRecalculateNaturalSize = true;
125 // Apply modifications to the model
126 mImpl->mOperationsPending = ALL_OPERATIONS;
130 ShowPlaceholderText();
133 // Resets the cursor position.
134 ResetCursorPosition( lastCursorIndex );
136 // Scrolls the text to make the cursor visible.
137 ResetScrollPosition();
139 mImpl->RequestRelayout();
141 if( mImpl->mEventData )
143 // Cancel previously queued events
144 mImpl->mEventData->mEventQueue.clear();
147 // Reset keyboard as text changed
148 mImpl->ResetImfManager();
150 // Do this last since it provides callbacks into application code
151 mImpl->mControlInterface.TextChanged();
154 void Controller::GetText( std::string& text ) const
156 if( ! mImpl->IsShowingPlaceholderText() )
158 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
160 if( 0u != utf32Characters.Count() )
162 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
167 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
171 unsigned int Controller::GetLogicalCursorPosition() const
173 if( mImpl->mEventData )
175 return mImpl->mEventData->mPrimaryCursorPosition;
181 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
183 if( mImpl->mEventData )
185 if( PLACEHOLDER_TYPE_INACTIVE == type )
187 mImpl->mEventData->mPlaceholderTextInactive = text;
191 mImpl->mEventData->mPlaceholderTextActive = text;
194 // Update placeholder if there is no text
195 if( mImpl->IsShowingPlaceholderText() ||
196 0u == mImpl->mLogicalModel->mText.Count() )
198 ShowPlaceholderText();
203 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
205 if( mImpl->mEventData )
207 if( PLACEHOLDER_TYPE_INACTIVE == type )
209 text = mImpl->mEventData->mPlaceholderTextInactive;
213 text = mImpl->mEventData->mPlaceholderTextActive;
218 void Controller::SetMaximumNumberOfCharacters( int maxCharacters )
220 if ( maxCharacters >= 0 )
222 mImpl->mMaximumNumberOfCharacters = maxCharacters;
226 int Controller::GetMaximumNumberOfCharacters()
228 return mImpl->mMaximumNumberOfCharacters;
231 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
233 if( !mImpl->mFontDefaults )
235 mImpl->mFontDefaults = new FontDefaults();
238 mImpl->mFontDefaults->mDefaultFontFamily = defaultFontFamily;
240 // Clear the font-specific data
243 mImpl->mOperationsPending = ALL_OPERATIONS;
244 mImpl->mRecalculateNaturalSize = true;
246 mImpl->RequestRelayout();
249 const std::string& Controller::GetDefaultFontFamily() const
251 if( mImpl->mFontDefaults )
253 return mImpl->mFontDefaults->mDefaultFontFamily;
259 void Controller::SetDefaultFontStyle( const std::string& defaultFontStyle )
261 if( !mImpl->mFontDefaults )
263 mImpl->mFontDefaults = new FontDefaults();
266 mImpl->mFontDefaults->mDefaultFontStyle = defaultFontStyle;
268 // Clear the font-specific data
271 mImpl->mOperationsPending = ALL_OPERATIONS;
272 mImpl->mRecalculateNaturalSize = true;
274 mImpl->RequestRelayout();
277 const std::string& Controller::GetDefaultFontStyle() const
279 if( mImpl->mFontDefaults )
281 return mImpl->mFontDefaults->mDefaultFontStyle;
287 void Controller::SetDefaultPointSize( float pointSize )
289 if( !mImpl->mFontDefaults )
291 mImpl->mFontDefaults = new FontDefaults();
294 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
296 // Clear the font-specific data
299 mImpl->mOperationsPending = ALL_OPERATIONS;
300 mImpl->mRecalculateNaturalSize = true;
302 mImpl->RequestRelayout();
305 float Controller::GetDefaultPointSize() const
307 if( mImpl->mFontDefaults )
309 return mImpl->mFontDefaults->mDefaultPointSize;
315 void Controller::SetTextColor( const Vector4& textColor )
317 mImpl->mTextColor = textColor;
319 if( !mImpl->IsShowingPlaceholderText() )
321 mImpl->mVisualModel->SetTextColor( textColor );
323 mImpl->RequestRelayout();
327 const Vector4& Controller::GetTextColor() const
329 return mImpl->mTextColor;
332 bool Controller::RemoveText( int cursorOffset, int numberOfChars )
334 bool removed( false );
336 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfChars %d\n",
337 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfChars );
339 if( ! mImpl->IsShowingPlaceholderText() )
341 // Delete at current cursor position
342 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
343 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
345 CharacterIndex cursorIndex = oldCursorIndex;
347 // Validate the cursor position & number of characters
348 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
350 cursorIndex = oldCursorIndex + cursorOffset;
353 if( (cursorIndex + numberOfChars) > currentText.Count() )
355 numberOfChars = currentText.Count() - cursorIndex;
358 if( (cursorIndex + numberOfChars) <= currentText.Count() )
360 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
361 Vector<Character>::Iterator last = first + numberOfChars;
363 currentText.Erase( first, last );
365 // Cursor position retreat
366 oldCursorIndex = cursorIndex;
368 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfChars );
376 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
378 if( mImpl->mEventData )
380 mImpl->mEventData->mPlaceholderTextColor = textColor;
383 if( mImpl->IsShowingPlaceholderText() )
385 mImpl->mVisualModel->SetTextColor( textColor );
386 mImpl->RequestRelayout();
390 const Vector4& Controller::GetPlaceholderTextColor() const
392 if( mImpl->mEventData )
394 return mImpl->mEventData->mPlaceholderTextColor;
400 void Controller::SetShadowOffset( const Vector2& shadowOffset )
402 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
404 mImpl->RequestRelayout();
407 const Vector2& Controller::GetShadowOffset() const
409 return mImpl->mVisualModel->GetShadowOffset();
412 void Controller::SetShadowColor( const Vector4& shadowColor )
414 mImpl->mVisualModel->SetShadowColor( shadowColor );
416 mImpl->RequestRelayout();
419 const Vector4& Controller::GetShadowColor() const
421 return mImpl->mVisualModel->GetShadowColor();
424 void Controller::SetUnderlineColor( const Vector4& color )
426 mImpl->mVisualModel->SetUnderlineColor( color );
428 mImpl->RequestRelayout();
431 const Vector4& Controller::GetUnderlineColor() const
433 return mImpl->mVisualModel->GetUnderlineColor();
436 void Controller::SetUnderlineEnabled( bool enabled )
438 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
440 mImpl->RequestRelayout();
443 bool Controller::IsUnderlineEnabled() const
445 return mImpl->mVisualModel->IsUnderlineEnabled();
448 void Controller::SetUnderlineHeight( float height )
450 mImpl->mVisualModel->SetUnderlineHeight( height );
452 mImpl->RequestRelayout();
455 float Controller::GetUnderlineHeight() const
457 return mImpl->mVisualModel->GetUnderlineHeight();
460 void Controller::SetEnableCursorBlink( bool enable )
462 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
464 if( mImpl->mEventData )
466 mImpl->mEventData->mCursorBlinkEnabled = enable;
469 mImpl->mEventData->mDecorator )
471 mImpl->mEventData->mDecorator->StopCursorBlink();
476 bool Controller::GetEnableCursorBlink() const
478 if( mImpl->mEventData )
480 return mImpl->mEventData->mCursorBlinkEnabled;
486 const Vector2& Controller::GetScrollPosition() const
488 if( mImpl->mEventData )
490 return mImpl->mEventData->mScrollPosition;
493 return Vector2::ZERO;
496 const Vector2& Controller::GetAlignmentOffset() const
498 return mImpl->mAlignmentOffset;
501 Vector3 Controller::GetNaturalSize()
503 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
506 // Make sure the model is up-to-date before layouting
507 ProcessModifyEvents();
509 if( mImpl->mRecalculateNaturalSize )
511 // Operations that can be done only once until the text changes.
512 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
520 // Make sure the model is up-to-date before layouting
521 mImpl->UpdateModel( onlyOnceOperations );
523 // Operations that need to be done if the size changes.
524 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
528 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
529 static_cast<OperationsMask>( onlyOnceOperations |
531 naturalSize.GetVectorXY() );
533 // Do not do again the only once operations.
534 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
536 // Do the size related operations again.
537 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
539 // Stores the natural size to avoid recalculate it again
540 // unless the text/style changes.
541 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
543 mImpl->mRecalculateNaturalSize = false;
545 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
549 naturalSize = mImpl->mVisualModel->GetNaturalSize();
551 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
554 naturalSize.x = ConvertToEven( naturalSize.x );
555 naturalSize.y = ConvertToEven( naturalSize.y );
560 float Controller::GetHeightForWidth( float width )
562 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
563 // Make sure the model is up-to-date before layouting
564 ProcessModifyEvents();
567 if( width != mImpl->mVisualModel->mControlSize.width )
569 // Operations that can be done only once until the text changes.
570 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
578 // Make sure the model is up-to-date before layouting
579 mImpl->UpdateModel( onlyOnceOperations );
581 // Operations that need to be done if the size changes.
582 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
586 DoRelayout( Size( width, MAX_FLOAT ),
587 static_cast<OperationsMask>( onlyOnceOperations |
591 // Do not do again the only once operations.
592 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
594 // Do the size related operations again.
595 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
596 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
600 layoutSize = mImpl->mVisualModel->GetActualSize();
601 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
604 return layoutSize.height;
607 bool Controller::Relayout( const Size& size )
609 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
611 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
613 bool glyphsRemoved( false );
614 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
616 mImpl->mVisualModel->mGlyphPositions.Clear();
617 glyphsRemoved = true;
619 // Not worth to relayout if width or height is equal to zero.
620 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
621 return glyphsRemoved;
624 if( size != mImpl->mVisualModel->mControlSize )
626 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
628 // Operations that need to be done if the size changes.
629 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
635 mImpl->mVisualModel->mControlSize = size;
638 // Make sure the model is up-to-date before layouting
639 ProcessModifyEvents();
640 mImpl->UpdateModel( mImpl->mOperationsPending );
643 bool updated = DoRelayout( mImpl->mVisualModel->mControlSize,
644 mImpl->mOperationsPending,
647 // Do not re-do any operation until something changes.
648 mImpl->mOperationsPending = NO_OPERATION;
650 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
651 CalculateTextAlignment( size );
653 if( mImpl->mEventData )
655 // Move the cursor, grab handle etc.
656 updated = mImpl->ProcessInputEvents() || updated;
659 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
663 void Controller::ProcessModifyEvents()
665 std::vector<ModifyEvent>& events = mImpl->mModifyEvents;
667 for( unsigned int i=0; i<events.size(); ++i )
669 if( ModifyEvent::TEXT_REPLACED == events[0].type )
671 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
672 DALI_ASSERT_DEBUG( 0 == i && "Unexpected TEXT_REPLACED event" );
676 else if( ModifyEvent::TEXT_INSERTED == events[0].type )
680 else if( ModifyEvent::TEXT_DELETED == events[0].type )
682 // Placeholder-text cannot be deleted
683 if( !mImpl->IsShowingPlaceholderText() )
690 // Discard temporary text
694 void Controller::ResetText()
697 mImpl->mLogicalModel->mText.Clear();
700 // We have cleared everything including the placeholder-text
701 mImpl->PlaceholderCleared();
703 // The natural size needs to be re-calculated.
704 mImpl->mRecalculateNaturalSize = true;
706 // Apply modifications to the model
707 mImpl->mOperationsPending = ALL_OPERATIONS;
710 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
712 // Reset the cursor position
713 if( NULL != mImpl->mEventData )
715 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
717 // Update the cursor if it's in editing mode.
718 if( ( EventData::EDITING == mImpl->mEventData->mState ) ||
719 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) )
721 mImpl->mEventData->mUpdateCursorPosition = true;
726 void Controller::ResetScrollPosition()
728 if( NULL != mImpl->mEventData )
730 // Reset the scroll position.
731 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
732 mImpl->mEventData->mScrollAfterUpdatePosition = true;
736 void Controller::TextReplacedEvent()
741 // The natural size needs to be re-calculated.
742 mImpl->mRecalculateNaturalSize = true;
744 // Apply modifications to the model
745 mImpl->mOperationsPending = ALL_OPERATIONS;
746 mImpl->UpdateModel( ALL_OPERATIONS );
747 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
753 void Controller::TextInsertedEvent()
755 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
757 // TODO - Optimize this
760 // The natural size needs to be re-calculated.
761 mImpl->mRecalculateNaturalSize = true;
763 // Apply modifications to the model; TODO - Optimize this
764 mImpl->mOperationsPending = ALL_OPERATIONS;
765 mImpl->UpdateModel( ALL_OPERATIONS );
766 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
771 // Queue a cursor reposition event; this must wait until after DoRelayout()
772 mImpl->mEventData->mUpdateCursorPosition = true;
773 mImpl->mEventData->mScrollAfterUpdatePosition = true;
776 void Controller::TextDeletedEvent()
778 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
780 // TODO - Optimize this
783 // The natural size needs to be re-calculated.
784 mImpl->mRecalculateNaturalSize = true;
786 // Apply modifications to the model; TODO - Optimize this
787 mImpl->mOperationsPending = ALL_OPERATIONS;
788 mImpl->UpdateModel( ALL_OPERATIONS );
789 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
794 // Queue a cursor reposition event; this must wait until after DoRelayout()
795 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 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 Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
824 Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
825 Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
826 Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
827 Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
828 Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
830 // Set the layout parameters.
831 LayoutParameters layoutParameters( size,
832 mImpl->mLogicalModel->mText.Begin(),
833 lineBreakInfo.Begin(),
834 wordBreakInfo.Begin(),
835 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
838 glyphsToCharactersMap.Begin(),
839 charactersPerGlyph.Begin() );
841 // The laid-out lines.
842 // It's not possible to know in how many lines the text is going to be laid-out,
843 // but it can be resized at least with the number of 'paragraphs' to avoid
844 // some re-allocations.
845 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
847 // Delete any previous laid out lines before setting the new ones.
850 // The capacity of the bidirectional paragraph info is the number of paragraphs.
851 lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
853 // Resize the vector of positions to have the same size than the vector of glyphs.
854 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
855 glyphPositions.Resize( numberOfGlyphs );
857 // Update the visual model.
858 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
866 if( REORDER & operations )
868 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
870 // Check first if there are paragraphs with bidirectional info.
871 if( 0u != bidirectionalInfo.Count() )
874 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
876 // Reorder the lines.
877 Vector<BidirectionalLineInfoRun> lineBidirectionalInfoRuns;
878 lineBidirectionalInfoRuns.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
879 ReorderLines( bidirectionalInfo,
881 lineBidirectionalInfoRuns );
883 // Set the bidirectional info into the model.
884 const Length numberOfBidirectionalInfoRuns = lineBidirectionalInfoRuns.Count();
885 mImpl->mLogicalModel->SetVisualToLogicalMap( lineBidirectionalInfoRuns.Begin(),
886 numberOfBidirectionalInfoRuns );
888 // Set the bidirectional info per line into the layout parameters.
889 layoutParameters.lineBidirectionalInfoRunsBuffer = lineBidirectionalInfoRuns.Begin();
890 layoutParameters.numberOfBidirectionalInfoRuns = numberOfBidirectionalInfoRuns;
892 // Get the character to glyph conversion table and set into the layout.
893 layoutParameters.charactersToGlyphsBuffer = mImpl->mVisualModel->mCharactersToGlyph.Begin();
895 // Get the glyphs per character table and set into the layout.
896 layoutParameters.glyphsPerCharacterBuffer = mImpl->mVisualModel->mGlyphsPerCharacter.Begin();
898 // Re-layout the text. Reorder those lines with right to left characters.
899 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
902 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
903 for( Vector<BidirectionalLineInfoRun>::Iterator it = lineBidirectionalInfoRuns.Begin(),
904 endIt = lineBidirectionalInfoRuns.End();
908 BidirectionalLineInfoRun& bidiLineInfo = *it;
910 free( bidiLineInfo.visualToLogicalMap );
915 // Sets the actual size.
916 if( UPDATE_ACTUAL_SIZE & operations )
918 mImpl->mVisualModel->SetActualSize( layoutSize );
924 layoutSize = mImpl->mVisualModel->GetActualSize();
927 if( ALIGN & operations )
929 // The laid-out lines.
930 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
932 mImpl->mLayoutEngine.Align( layoutSize,
938 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
942 void Controller::SetMultiLineEnabled( bool enable )
944 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
946 if( layout != mImpl->mLayoutEngine.GetLayout() )
948 // Set the layout type.
949 mImpl->mLayoutEngine.SetLayout( layout );
951 // Set the flags to redo the layout operations
952 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
957 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
959 mImpl->RequestRelayout();
963 bool Controller::IsMultiLineEnabled() const
965 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
968 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
970 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
972 // Set the alignment.
973 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
975 // Set the flag to redo the alignment operation.
976 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
978 mImpl->RequestRelayout();
982 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
984 return mImpl->mLayoutEngine.GetHorizontalAlignment();
987 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
989 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
991 // Set the alignment.
992 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
994 // Set the flag to redo the alignment operation.
995 // TODO : Is not needed re-layout and reorder again but with the current implementation it is.
996 // Im working on a different patch to fix an issue with the alignment. When that patch
997 // is in, this issue can be fixed.
998 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1003 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1005 mImpl->RequestRelayout();
1009 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1011 return mImpl->mLayoutEngine.GetVerticalAlignment();
1014 void Controller::CalculateTextAlignment( const Size& size )
1016 // Get the direction of the first character.
1017 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1019 Size actualSize = mImpl->mVisualModel->GetActualSize();
1020 if( fabsf( actualSize.height ) < Math::MACHINE_EPSILON_1000 )
1022 // Get the line height of the default font.
1023 actualSize.height = mImpl->GetDefaultFontLineHeight();
1026 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1027 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1028 if( firstParagraphDirection &&
1029 ( LayoutEngine::HORIZONTAL_ALIGN_CENTER != horizontalAlignment ) )
1031 if( LayoutEngine::HORIZONTAL_ALIGN_BEGIN == horizontalAlignment )
1033 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1037 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1041 switch( horizontalAlignment )
1043 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1045 mImpl->mAlignmentOffset.x = 0.f;
1048 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1050 const int intOffset = static_cast<int>( 0.5f * ( size.width - actualSize.width ) ); // try to avoid pixel alignment.
1051 mImpl->mAlignmentOffset.x = static_cast<float>( intOffset );
1054 case LayoutEngine::HORIZONTAL_ALIGN_END:
1056 mImpl->mAlignmentOffset.x = size.width - actualSize.width;
1061 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1062 switch( verticalAlignment )
1064 case LayoutEngine::VERTICAL_ALIGN_TOP:
1066 mImpl->mAlignmentOffset.y = 0.f;
1069 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1071 const int intOffset = static_cast<int>( 0.5f * ( size.height - actualSize.height ) ); // try to avoid pixel alignment.
1072 mImpl->mAlignmentOffset.y = static_cast<float>( intOffset );
1075 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1077 mImpl->mAlignmentOffset.y = size.height - actualSize.height;
1083 LayoutEngine& Controller::GetLayoutEngine()
1085 return mImpl->mLayoutEngine;
1088 View& Controller::GetView()
1090 return mImpl->mView;
1093 void Controller::KeyboardFocusGainEvent()
1095 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1097 if( mImpl->mEventData )
1099 mImpl->ChangeState( EventData::EDITING );
1101 if( mImpl->IsShowingPlaceholderText() )
1103 // Show alternative placeholder-text when editing
1104 ShowPlaceholderText();
1107 mImpl->RequestRelayout();
1111 void Controller::KeyboardFocusLostEvent()
1113 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1115 if( mImpl->mEventData )
1117 if ( EventData::INTERRUPTED != mImpl->mEventData->mState )
1119 mImpl->ChangeState( EventData::INACTIVE );
1121 if( mImpl->IsShowingPlaceholderText() )
1123 // Revert to regular placeholder-text when not editing
1124 ShowPlaceholderText();
1128 mImpl->RequestRelayout();
1131 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1133 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1135 bool textChanged( false );
1137 if( mImpl->mEventData &&
1138 keyEvent.state == KeyEvent::Down )
1140 int keyCode = keyEvent.keyCode;
1141 const std::string& keyString = keyEvent.keyPressed;
1143 // Pre-process to separate modifying events from non-modifying input events.
1144 if( Dali::DALI_KEY_ESCAPE == keyCode )
1146 // Escape key is a special case which causes focus loss
1147 KeyboardFocusLostEvent();
1149 else if( Dali::DALI_KEY_CURSOR_LEFT == keyCode ||
1150 Dali::DALI_KEY_CURSOR_RIGHT == keyCode ||
1151 Dali::DALI_KEY_CURSOR_UP == keyCode ||
1152 Dali::DALI_KEY_CURSOR_DOWN == keyCode )
1154 Event event( Event::CURSOR_KEY_EVENT );
1155 event.p1.mInt = keyCode;
1156 mImpl->mEventData->mEventQueue.push_back( event );
1158 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1160 textChanged = BackspaceKeyEvent();
1162 else if ( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1164 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1165 // Avoids calling the InsertText() method which can delete selected text
1167 else if ( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1168 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1170 mImpl->ChangeState( EventData::INACTIVE );
1171 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1172 // Avoids calling the InsertText() method which can delete selected text
1176 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1178 // IMF manager is no longer handling key-events
1179 mImpl->ClearPreEditFlag();
1181 InsertText( keyString, COMMIT );
1185 if ( mImpl->mEventData->mState != EventData::INTERRUPTED && mImpl->mEventData->mState != EventData::INACTIVE )
1187 mImpl->ChangeState( EventData::EDITING );
1190 mImpl->RequestRelayout();
1195 // Do this last since it provides callbacks into application code
1196 mImpl->mControlInterface.TextChanged();
1202 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1204 bool removedPrevious( false );
1205 bool maxLengthReached( false );
1207 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1208 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1209 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1210 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1212 Vector<Character> utf32Characters;
1213 Length characterCount( 0u );
1215 // Remove the previous IMF pre-edit (predicitive text)
1216 if( mImpl->mEventData &&
1217 mImpl->mEventData->mPreEditFlag &&
1218 0 != mImpl->mEventData->mPreEditLength )
1220 CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1221 removedPrevious = RemoveText( -static_cast<int>(offset), mImpl->mEventData->mPreEditLength );
1223 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1224 mImpl->mEventData->mPreEditLength = 0;
1228 // Remove the previous Selection
1229 removedPrevious = RemoveSelectedText();
1232 if( ! text.empty() )
1234 // Convert text into UTF-32
1235 utf32Characters.Resize( text.size() );
1237 // This is a bit horrible but std::string returns a (signed) char*
1238 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1240 // Transform a text array encoded in utf8 into an array encoded in utf32.
1241 // It returns the actual number of characters.
1242 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1243 utf32Characters.Resize( characterCount );
1245 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1246 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1249 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1251 // The placeholder text is no longer needed
1252 if( mImpl->IsShowingPlaceholderText() )
1257 mImpl->ChangeState( EventData::EDITING );
1259 // Handle the IMF (predicitive text) state changes
1260 if( mImpl->mEventData )
1262 if( COMMIT == type )
1264 // IMF manager is no longer handling key-events
1265 mImpl->ClearPreEditFlag();
1269 if( ! mImpl->mEventData->mPreEditFlag )
1271 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1273 // Record the start of the pre-edit text
1274 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1277 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1278 mImpl->mEventData->mPreEditFlag = true;
1280 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1284 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1286 // Restrict new text to fit within Maximum characters setting
1287 Length maxSizeOfNewText = std::min ( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1288 maxLengthReached = ( characterCount > maxSizeOfNewText );
1290 // Insert at current cursor position
1291 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1293 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1295 if( cursorIndex < numberOfCharactersInModel )
1297 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1301 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1304 cursorIndex += maxSizeOfNewText;
1306 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1309 if( 0u == mImpl->mLogicalModel->mText.Count() &&
1310 mImpl->IsPlaceholderAvailable() )
1312 // Show place-holder if empty after removing the pre-edit text
1313 ShowPlaceholderText();
1314 mImpl->mEventData->mUpdateCursorPosition = true;
1315 mImpl->ClearPreEditFlag();
1317 else if( removedPrevious ||
1318 0 != utf32Characters.Count() )
1320 // Queue an inserted event
1321 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1324 if( maxLengthReached )
1326 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1328 mImpl->ResetImfManager();
1330 // Do this last since it provides callbacks into application code
1331 mImpl->mControlInterface.MaxLengthReached();
1335 bool Controller::RemoveSelectedText()
1337 bool textRemoved( false );
1339 if ( EventData::SELECTING == mImpl->mEventData->mState ||
1340 EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
1342 std::string removedString;
1343 mImpl->RetrieveSelection( removedString, true );
1345 if( !removedString.empty() )
1348 mImpl->ChangeState( EventData::EDITING );
1355 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1357 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1359 if( NULL != mImpl->mEventData )
1361 const bool isShowingPlaceholderText = mImpl->IsShowingPlaceholderText();
1362 if( 1u == tapCount )
1364 if( !isShowingPlaceholderText &&
1365 ( EventData::EDITING == mImpl->mEventData->mState ) )
1367 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
1369 else if( EventData::EDITING_WITH_GRAB_HANDLE != mImpl->mEventData->mState )
1371 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
1372 mImpl->ChangeState( EventData::EDITING );
1375 Event event( Event::TAP_EVENT );
1376 event.p1.mUint = tapCount;
1377 event.p2.mFloat = x;
1378 event.p3.mFloat = y;
1379 mImpl->mEventData->mEventQueue.push_back( event );
1381 mImpl->RequestRelayout();
1383 else if( !isShowingPlaceholderText &&
1384 mImpl->mEventData->mSelectionEnabled &&
1385 ( 2u == tapCount ) )
1387 SelectEvent( x, y, false );
1391 // Reset keyboard as tap event has occurred.
1392 mImpl->ResetImfManager();
1395 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
1397 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1399 if( mImpl->mEventData )
1401 Event event( Event::PAN_EVENT );
1402 event.p1.mInt = state;
1403 event.p2.mFloat = displacement.x;
1404 event.p3.mFloat = displacement.y;
1405 mImpl->mEventData->mEventQueue.push_back( event );
1407 mImpl->RequestRelayout();
1411 void Controller::LongPressEvent( Gesture::State state, float x, float y )
1413 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1415 if ( mImpl->IsShowingPlaceholderText() || mImpl->mLogicalModel->mText.Count() == 0u )
1417 if ( mImpl->mEventData )
1419 Event event( Event::LONG_PRESS_EVENT );
1420 event.p1.mInt = state;
1421 mImpl->mEventData->mEventQueue.push_back( event );
1422 mImpl->RequestRelayout();
1425 else if( mImpl->mEventData )
1427 SelectEvent( x, y, false );
1431 void Controller::SelectEvent( float x, float y, bool selectAll )
1433 if( mImpl->mEventData )
1435 if ( mImpl->mEventData->mState == EventData::SELECTING )
1437 mImpl->ChangeState( EventData::SELECTION_CHANGED );
1441 mImpl->ChangeState( EventData::SELECTING );
1446 Event event( Event::SELECT_ALL );
1447 mImpl->mEventData->mEventQueue.push_back( event );
1451 Event event( Event::SELECT );
1452 event.p2.mFloat = x;
1453 event.p3.mFloat = y;
1454 mImpl->mEventData->mEventQueue.push_back( event );
1457 mImpl->RequestRelayout();
1461 void Controller::GetTargetSize( Vector2& targetSize )
1463 targetSize = mImpl->mVisualModel->mControlSize;
1466 void Controller::AddDecoration( Actor& actor, bool needsClipping )
1468 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
1471 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
1473 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
1475 if( mImpl->mEventData )
1477 switch( handleType )
1481 Event event( Event::GRAB_HANDLE_EVENT );
1482 event.p1.mUint = state;
1483 event.p2.mFloat = x;
1484 event.p3.mFloat = y;
1486 mImpl->mEventData->mEventQueue.push_back( event );
1489 case LEFT_SELECTION_HANDLE:
1491 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
1492 event.p1.mUint = state;
1493 event.p2.mFloat = x;
1494 event.p3.mFloat = y;
1496 mImpl->mEventData->mEventQueue.push_back( event );
1499 case RIGHT_SELECTION_HANDLE:
1501 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
1502 event.p1.mUint = state;
1503 event.p2.mFloat = x;
1504 event.p3.mFloat = y;
1506 mImpl->mEventData->mEventQueue.push_back( event );
1509 case HANDLE_TYPE_COUNT:
1511 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
1515 mImpl->RequestRelayout();
1519 void Controller::PasteText( const std::string& stringToPaste )
1521 InsertText( stringToPaste, Text::Controller::COMMIT );
1522 mImpl->ChangeState( EventData::EDITING );
1523 mImpl->RequestRelayout();
1526 void Controller::PasteClipboardItemEvent()
1528 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
1529 std::string stringToPaste( notifier.GetContent() );
1530 PasteText( stringToPaste );
1533 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
1535 if( NULL == mImpl->mEventData )
1542 case Toolkit::TextSelectionPopup::CUT:
1544 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
1545 mImpl->mOperationsPending = ALL_OPERATIONS;
1546 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1547 !mImpl->IsPlaceholderAvailable() )
1549 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1553 ShowPlaceholderText();
1554 mImpl->mEventData->mUpdateCursorPosition = true;
1556 mImpl->RequestRelayout();
1557 mImpl->mControlInterface.TextChanged();
1560 case Toolkit::TextSelectionPopup::COPY:
1562 mImpl->SendSelectionToClipboard( false ); // Text not modified
1563 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
1566 case Toolkit::TextSelectionPopup::PASTE:
1568 std::string stringToPaste("");
1569 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
1570 PasteText( stringToPaste );
1573 case Toolkit::TextSelectionPopup::SELECT:
1575 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
1577 if( mImpl->mEventData->mSelectionEnabled )
1579 // Creates a SELECT event.
1580 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
1584 case Toolkit::TextSelectionPopup::SELECT_ALL:
1586 // Creates a SELECT_ALL event
1587 SelectEvent( 0.f, 0.f, true );
1590 case Toolkit::TextSelectionPopup::CLIPBOARD:
1592 mImpl->ShowClipboard();
1595 case Toolkit::TextSelectionPopup::NONE:
1603 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
1605 bool update( false );
1606 bool requestRelayout = false;
1609 unsigned int cursorPosition( 0 );
1611 switch ( imfEvent.eventName )
1613 case ImfManager::COMMIT:
1615 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
1616 requestRelayout = true;
1619 case ImfManager::PREEDIT:
1621 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
1623 requestRelayout = true;
1626 case ImfManager::DELETESURROUNDING:
1628 RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
1629 requestRelayout = true;
1632 case ImfManager::GETSURROUNDING:
1635 cursorPosition = GetLogicalCursorPosition();
1637 imfManager.SetSurroundingText( text );
1638 imfManager.SetCursorPosition( cursorPosition );
1641 case ImfManager::VOID:
1648 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
1651 cursorPosition = GetLogicalCursorPosition();
1654 if( requestRelayout )
1656 mImpl->mOperationsPending = ALL_OPERATIONS;
1657 mImpl->RequestRelayout();
1659 // Do this last since it provides callbacks into application code
1660 mImpl->mControlInterface.TextChanged();
1663 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
1665 return callbackData;
1668 Controller::~Controller()
1673 bool Controller::BackspaceKeyEvent()
1675 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
1677 // IMF manager is no longer handling key-events
1678 mImpl->ClearPreEditFlag();
1680 bool removed( false );
1682 if ( EventData::SELECTING == mImpl->mEventData->mState ||
1683 EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
1685 removed = RemoveSelectedText();
1687 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
1689 // Remove the character before the current cursor position
1690 removed = RemoveText( -1, 1 );
1695 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1696 !mImpl->IsPlaceholderAvailable() )
1698 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1702 ShowPlaceholderText();
1703 mImpl->mEventData->mUpdateCursorPosition = true;
1710 void Controller::ShowPlaceholderText()
1712 if( mImpl->IsPlaceholderAvailable() )
1714 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
1716 mImpl->mEventData->mIsShowingPlaceholderText = true;
1718 // Disable handles when showing place-holder text
1719 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
1720 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
1721 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
1723 const char* text( NULL );
1726 // TODO - Switch placeholder text styles when changing state
1727 if( EventData::INACTIVE != mImpl->mEventData->mState &&
1728 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() )
1730 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
1731 size = mImpl->mEventData->mPlaceholderTextActive.size();
1735 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
1736 size = mImpl->mEventData->mPlaceholderTextInactive.size();
1739 // Reset model for showing placeholder.
1740 mImpl->mLogicalModel->mText.Clear();
1742 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
1744 // Convert text into UTF-32
1745 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
1746 utf32Characters.Resize( size );
1748 // This is a bit horrible but std::string returns a (signed) char*
1749 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
1751 // Transform a text array encoded in utf8 into an array encoded in utf32.
1752 // It returns the actual number of characters.
1753 Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
1754 utf32Characters.Resize( characterCount );
1756 // Reset the cursor position
1757 mImpl->mEventData->mPrimaryCursorPosition = 0;
1759 // The natural size needs to be re-calculated.
1760 mImpl->mRecalculateNaturalSize = true;
1762 // Apply modifications to the model
1763 mImpl->mOperationsPending = ALL_OPERATIONS;
1765 // Update the rest of the model during size negotiation
1766 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
1770 void Controller::ClearModelData()
1772 // n.b. This does not Clear the mText from mLogicalModel
1773 mImpl->mLogicalModel->mScriptRuns.Clear();
1774 mImpl->mLogicalModel->mFontRuns.Clear();
1775 mImpl->mLogicalModel->mLineBreakInfo.Clear();
1776 mImpl->mLogicalModel->mWordBreakInfo.Clear();
1777 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
1778 mImpl->mLogicalModel->mCharacterDirections.Clear();
1779 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
1780 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
1781 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
1782 mImpl->mVisualModel->mGlyphs.Clear();
1783 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
1784 mImpl->mVisualModel->mCharactersToGlyph.Clear();
1785 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
1786 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
1787 mImpl->mVisualModel->mGlyphPositions.Clear();
1788 mImpl->mVisualModel->mLines.Clear();
1789 mImpl->mVisualModel->ClearCaches();
1792 void Controller::ClearFontData()
1794 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
1795 mImpl->mLogicalModel->mFontRuns.Clear();
1796 mImpl->mVisualModel->mGlyphs.Clear();
1797 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
1798 mImpl->mVisualModel->mCharactersToGlyph.Clear();
1799 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
1800 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
1801 mImpl->mVisualModel->mGlyphPositions.Clear();
1802 mImpl->mVisualModel->mLines.Clear();
1803 mImpl->mVisualModel->ClearCaches();
1806 Controller::Controller( ControlInterface& controlInterface )
1809 mImpl = new Controller::Impl( controlInterface );
1814 } // namespace Toolkit