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::NoLogging, 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 ) ||
87 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
89 mImpl->ChangeState( EventData::EDITING );
95 // Convert text into UTF-32
96 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
97 utf32Characters.Resize( text.size() );
99 // This is a bit horrible but std::string returns a (signed) char*
100 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
102 // Transform a text array encoded in utf8 into an array encoded in utf32.
103 // It returns the actual number of characters.
104 Length characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
105 utf32Characters.Resize( characterCount );
107 DALI_ASSERT_DEBUG( text.size() >= characterCount && "Invalid UTF32 conversion length" );
108 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, text.size(), mImpl->mLogicalModel->mText.Count() );
110 // To reset the cursor position
111 lastCursorIndex = characterCount;
113 // Update the rest of the model during size negotiation
114 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
116 // The natural size needs to be re-calculated.
117 mImpl->mRecalculateNaturalSize = true;
119 // Apply modifications to the model
120 mImpl->mOperationsPending = ALL_OPERATIONS;
124 ShowPlaceholderText();
127 // Resets the cursor position.
128 ResetCursorPosition( lastCursorIndex );
130 // Scrolls the text to make the cursor visible.
131 ResetScrollPosition();
133 mImpl->RequestRelayout();
135 if( mImpl->mEventData )
137 // Cancel previously queued events
138 mImpl->mEventData->mEventQueue.clear();
141 // Reset keyboard as text changed
142 mImpl->ResetImfManager();
144 // Do this last since it provides callbacks into application code
145 mImpl->mControlInterface.TextChanged();
148 void Controller::GetText( std::string& text ) const
150 if( ! mImpl->IsShowingPlaceholderText() )
152 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
154 if( 0u != utf32Characters.Count() )
156 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
161 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
165 unsigned int Controller::GetLogicalCursorPosition() const
167 if( mImpl->mEventData )
169 return mImpl->mEventData->mPrimaryCursorPosition;
175 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
177 if( mImpl->mEventData )
179 if( PLACEHOLDER_TYPE_INACTIVE == type )
181 mImpl->mEventData->mPlaceholderTextInactive = text;
185 mImpl->mEventData->mPlaceholderTextActive = text;
188 // Update placeholder if there is no text
189 if( mImpl->IsShowingPlaceholderText() ||
190 0u == mImpl->mLogicalModel->mText.Count() )
192 ShowPlaceholderText();
197 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
199 if( mImpl->mEventData )
201 if( PLACEHOLDER_TYPE_INACTIVE == type )
203 text = mImpl->mEventData->mPlaceholderTextInactive;
207 text = mImpl->mEventData->mPlaceholderTextActive;
212 void Controller::SetMaximumNumberOfCharacters( int maxCharacters )
214 if ( maxCharacters >= 0 )
216 mImpl->mMaximumNumberOfCharacters = maxCharacters;
220 int Controller::GetMaximumNumberOfCharacters()
222 return mImpl->mMaximumNumberOfCharacters;
225 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
227 if( !mImpl->mFontDefaults )
229 mImpl->mFontDefaults = new FontDefaults();
232 mImpl->mFontDefaults->mDefaultFontFamily = defaultFontFamily;
234 // Clear the font-specific data
237 mImpl->mOperationsPending = ALL_OPERATIONS;
238 mImpl->mRecalculateNaturalSize = true;
240 mImpl->RequestRelayout();
243 const std::string& Controller::GetDefaultFontFamily() const
245 if( mImpl->mFontDefaults )
247 return mImpl->mFontDefaults->mDefaultFontFamily;
253 void Controller::SetDefaultFontStyle( const std::string& defaultFontStyle )
255 if( !mImpl->mFontDefaults )
257 mImpl->mFontDefaults = new FontDefaults();
260 mImpl->mFontDefaults->mDefaultFontStyle = defaultFontStyle;
262 // Clear the font-specific data
265 mImpl->mOperationsPending = ALL_OPERATIONS;
266 mImpl->mRecalculateNaturalSize = true;
268 mImpl->RequestRelayout();
271 const std::string& Controller::GetDefaultFontStyle() const
273 if( mImpl->mFontDefaults )
275 return mImpl->mFontDefaults->mDefaultFontStyle;
281 void Controller::SetDefaultPointSize( float pointSize )
283 if( !mImpl->mFontDefaults )
285 mImpl->mFontDefaults = new FontDefaults();
288 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
290 // Clear the font-specific data
293 mImpl->mOperationsPending = ALL_OPERATIONS;
294 mImpl->mRecalculateNaturalSize = true;
296 mImpl->RequestRelayout();
299 float Controller::GetDefaultPointSize() const
301 if( mImpl->mFontDefaults )
303 return mImpl->mFontDefaults->mDefaultPointSize;
309 void Controller::SetTextColor( const Vector4& textColor )
311 mImpl->mTextColor = textColor;
313 if( !mImpl->IsShowingPlaceholderText() )
315 mImpl->mVisualModel->SetTextColor( textColor );
317 mImpl->RequestRelayout();
321 const Vector4& Controller::GetTextColor() const
323 return mImpl->mTextColor;
326 bool Controller::RemoveText( int cursorOffset, int numberOfChars )
328 bool removed( false );
330 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfChars %d\n",
331 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfChars );
333 if( !mImpl->IsShowingPlaceholderText() )
335 // Delete at current cursor position
336 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
337 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
339 CharacterIndex cursorIndex = oldCursorIndex;
341 // Validate the cursor position & number of characters
342 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
344 cursorIndex = oldCursorIndex + cursorOffset;
347 if( (cursorIndex + numberOfChars) > currentText.Count() )
349 numberOfChars = currentText.Count() - cursorIndex;
352 if( (cursorIndex + numberOfChars) <= currentText.Count() )
354 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
355 Vector<Character>::Iterator last = first + numberOfChars;
357 currentText.Erase( first, last );
359 // Cursor position retreat
360 oldCursorIndex = cursorIndex;
362 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfChars );
370 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
372 if( mImpl->mEventData )
374 mImpl->mEventData->mPlaceholderTextColor = textColor;
377 if( mImpl->IsShowingPlaceholderText() )
379 mImpl->mVisualModel->SetTextColor( textColor );
380 mImpl->RequestRelayout();
384 const Vector4& Controller::GetPlaceholderTextColor() const
386 if( mImpl->mEventData )
388 return mImpl->mEventData->mPlaceholderTextColor;
394 void Controller::SetShadowOffset( const Vector2& shadowOffset )
396 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
398 mImpl->RequestRelayout();
401 const Vector2& Controller::GetShadowOffset() const
403 return mImpl->mVisualModel->GetShadowOffset();
406 void Controller::SetShadowColor( const Vector4& shadowColor )
408 mImpl->mVisualModel->SetShadowColor( shadowColor );
410 mImpl->RequestRelayout();
413 const Vector4& Controller::GetShadowColor() const
415 return mImpl->mVisualModel->GetShadowColor();
418 void Controller::SetUnderlineColor( const Vector4& color )
420 mImpl->mVisualModel->SetUnderlineColor( color );
422 mImpl->RequestRelayout();
425 const Vector4& Controller::GetUnderlineColor() const
427 return mImpl->mVisualModel->GetUnderlineColor();
430 void Controller::SetUnderlineEnabled( bool enabled )
432 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
434 mImpl->RequestRelayout();
437 bool Controller::IsUnderlineEnabled() const
439 return mImpl->mVisualModel->IsUnderlineEnabled();
442 void Controller::SetUnderlineHeight( float height )
444 mImpl->mVisualModel->SetUnderlineHeight( height );
446 mImpl->RequestRelayout();
449 float Controller::GetUnderlineHeight() const
451 return mImpl->mVisualModel->GetUnderlineHeight();
454 void Controller::SetEnableCursorBlink( bool enable )
456 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
458 if( mImpl->mEventData )
460 mImpl->mEventData->mCursorBlinkEnabled = enable;
463 mImpl->mEventData->mDecorator )
465 mImpl->mEventData->mDecorator->StopCursorBlink();
470 bool Controller::GetEnableCursorBlink() const
472 if( mImpl->mEventData )
474 return mImpl->mEventData->mCursorBlinkEnabled;
480 const Vector2& Controller::GetScrollPosition() const
482 if( mImpl->mEventData )
484 return mImpl->mEventData->mScrollPosition;
487 return Vector2::ZERO;
490 const Vector2& Controller::GetAlignmentOffset() const
492 return mImpl->mAlignmentOffset;
495 Vector3 Controller::GetNaturalSize()
497 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
500 // Make sure the model is up-to-date before layouting
501 ProcessModifyEvents();
503 if( mImpl->mRecalculateNaturalSize )
505 // Operations that can be done only once until the text changes.
506 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
514 // Make sure the model is up-to-date before layouting
515 mImpl->UpdateModel( onlyOnceOperations );
517 // Operations that need to be done if the size changes.
518 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
522 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
523 static_cast<OperationsMask>( onlyOnceOperations |
525 naturalSize.GetVectorXY() );
527 // Do not do again the only once operations.
528 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
530 // Do the size related operations again.
531 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
533 // Stores the natural size to avoid recalculate it again
534 // unless the text/style changes.
535 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
537 mImpl->mRecalculateNaturalSize = false;
539 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
543 naturalSize = mImpl->mVisualModel->GetNaturalSize();
545 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
548 naturalSize.x = ConvertToEven( naturalSize.x );
549 naturalSize.y = ConvertToEven( naturalSize.y );
554 float Controller::GetHeightForWidth( float width )
556 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
557 // Make sure the model is up-to-date before layouting
558 ProcessModifyEvents();
561 if( width != mImpl->mVisualModel->mControlSize.width )
563 // Operations that can be done only once until the text changes.
564 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
572 // Make sure the model is up-to-date before layouting
573 mImpl->UpdateModel( onlyOnceOperations );
575 // Operations that need to be done if the size changes.
576 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
580 DoRelayout( Size( width, MAX_FLOAT ),
581 static_cast<OperationsMask>( onlyOnceOperations |
585 // Do not do again the only once operations.
586 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
588 // Do the size related operations again.
589 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
590 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
594 layoutSize = mImpl->mVisualModel->GetActualSize();
595 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
598 return layoutSize.height;
601 bool Controller::Relayout( const Size& size )
603 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
605 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
607 bool glyphsRemoved( false );
608 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
610 mImpl->mVisualModel->mGlyphPositions.Clear();
611 glyphsRemoved = true;
613 // Not worth to relayout if width or height is equal to zero.
614 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
615 return glyphsRemoved;
618 if( size != mImpl->mVisualModel->mControlSize )
620 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
622 // Operations that need to be done if the size changes.
623 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
629 mImpl->mVisualModel->mControlSize = size;
632 // Make sure the model is up-to-date before layouting
633 ProcessModifyEvents();
634 mImpl->UpdateModel( mImpl->mOperationsPending );
637 bool updated = DoRelayout( mImpl->mVisualModel->mControlSize,
638 mImpl->mOperationsPending,
641 // Do not re-do any operation until something changes.
642 mImpl->mOperationsPending = NO_OPERATION;
644 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
645 CalculateTextAlignment( size );
647 if( mImpl->mEventData )
649 // Move the cursor, grab handle etc.
650 updated = mImpl->ProcessInputEvents() || updated;
653 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
657 void Controller::ProcessModifyEvents()
659 std::vector<ModifyEvent>& events = mImpl->mModifyEvents;
661 for( unsigned int i=0; i<events.size(); ++i )
663 if( ModifyEvent::TEXT_REPLACED == events[0].type )
665 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
666 DALI_ASSERT_DEBUG( 0 == i && "Unexpected TEXT_REPLACED event" );
670 else if( ModifyEvent::TEXT_INSERTED == events[0].type )
674 else if( ModifyEvent::TEXT_DELETED == events[0].type )
676 // Placeholder-text cannot be deleted
677 if( !mImpl->IsShowingPlaceholderText() )
684 // Discard temporary text
688 void Controller::ResetText()
691 mImpl->mLogicalModel->mText.Clear();
694 // We have cleared everything including the placeholder-text
695 mImpl->PlaceholderCleared();
697 // The natural size needs to be re-calculated.
698 mImpl->mRecalculateNaturalSize = true;
700 // Apply modifications to the model
701 mImpl->mOperationsPending = ALL_OPERATIONS;
704 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
706 // Reset the cursor position
707 if( NULL != mImpl->mEventData )
709 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
711 // Update the cursor if it's in editing mode.
712 if( ( EventData::EDITING == mImpl->mEventData->mState ) ||
713 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
714 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
716 mImpl->mEventData->mUpdateCursorPosition = true;
721 void Controller::ResetScrollPosition()
723 if( NULL != mImpl->mEventData )
725 // Reset the scroll position.
726 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
727 mImpl->mEventData->mScrollAfterUpdatePosition = true;
731 void Controller::TextReplacedEvent()
736 // The natural size needs to be re-calculated.
737 mImpl->mRecalculateNaturalSize = true;
739 // Apply modifications to the model
740 mImpl->mOperationsPending = ALL_OPERATIONS;
741 mImpl->UpdateModel( ALL_OPERATIONS );
742 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
748 void Controller::TextInsertedEvent()
750 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
752 // TODO - Optimize this
755 // The natural size needs to be re-calculated.
756 mImpl->mRecalculateNaturalSize = true;
758 // Apply modifications to the model; TODO - Optimize this
759 mImpl->mOperationsPending = ALL_OPERATIONS;
760 mImpl->UpdateModel( ALL_OPERATIONS );
761 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
766 // Queue a cursor reposition event; this must wait until after DoRelayout()
767 if( ( EventData::EDITING == mImpl->mEventData->mState ) ||
768 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
769 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
771 mImpl->mEventData->mUpdateCursorPosition = true;
772 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 if( 0u == mImpl->mLogicalModel->mText.Count() )
797 mImpl->mEventData->mUpdateCursorPosition = true;
801 mImpl->mEventData->mScrollAfterDelete = true;
805 bool Controller::DoRelayout( const Size& size,
806 OperationsMask operationsRequired,
809 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
810 bool viewUpdated( false );
812 // Calculate the operations to be done.
813 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
815 if( LAYOUT & operations )
817 // Some vectors with data needed to layout and reorder may be void
818 // after the first time the text has been laid out.
819 // Fill the vectors again.
821 const Length numberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
823 if( 0u == numberOfGlyphs )
825 // Nothing else to do if there is no glyphs.
826 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
830 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
831 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
832 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
833 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
834 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
835 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
836 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
838 // Set the layout parameters.
839 LayoutParameters layoutParameters( size,
841 lineBreakInfo.Begin(),
842 wordBreakInfo.Begin(),
843 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
846 glyphsToCharactersMap.Begin(),
847 charactersPerGlyph.Begin() );
849 // The laid-out lines.
850 // It's not possible to know in how many lines the text is going to be laid-out,
851 // but it can be resized at least with the number of 'paragraphs' to avoid
852 // some re-allocations.
853 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
855 // Delete any previous laid out lines before setting the new ones.
858 // The capacity of the bidirectional paragraph info is the number of paragraphs.
859 lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
861 // Resize the vector of positions to have the same size than the vector of glyphs.
862 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
863 glyphPositions.Resize( numberOfGlyphs );
865 // Whether the last character is a new paragraph character.
866 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
868 // Update the visual model.
869 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
877 if( REORDER & operations )
879 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
881 // Check first if there are paragraphs with bidirectional info.
882 if( 0u != bidirectionalInfo.Count() )
885 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
887 // Reorder the lines.
888 Vector<BidirectionalLineInfoRun> lineBidirectionalInfoRuns;
889 lineBidirectionalInfoRuns.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
890 ReorderLines( bidirectionalInfo,
892 lineBidirectionalInfoRuns );
894 // Set the bidirectional info into the model.
895 const Length numberOfBidirectionalInfoRuns = lineBidirectionalInfoRuns.Count();
896 mImpl->mLogicalModel->SetVisualToLogicalMap( lineBidirectionalInfoRuns.Begin(),
897 numberOfBidirectionalInfoRuns );
899 // Set the bidirectional info per line into the layout parameters.
900 layoutParameters.lineBidirectionalInfoRunsBuffer = lineBidirectionalInfoRuns.Begin();
901 layoutParameters.numberOfBidirectionalInfoRuns = numberOfBidirectionalInfoRuns;
903 // Get the character to glyph conversion table and set into the layout.
904 layoutParameters.charactersToGlyphsBuffer = mImpl->mVisualModel->mCharactersToGlyph.Begin();
906 // Get the glyphs per character table and set into the layout.
907 layoutParameters.glyphsPerCharacterBuffer = mImpl->mVisualModel->mGlyphsPerCharacter.Begin();
909 // Re-layout the text. Reorder those lines with right to left characters.
910 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
913 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
914 for( Vector<BidirectionalLineInfoRun>::Iterator it = lineBidirectionalInfoRuns.Begin(),
915 endIt = lineBidirectionalInfoRuns.End();
919 BidirectionalLineInfoRun& bidiLineInfo = *it;
921 free( bidiLineInfo.visualToLogicalMap );
926 // Sets the actual size.
927 if( UPDATE_ACTUAL_SIZE & operations )
929 mImpl->mVisualModel->SetActualSize( layoutSize );
935 layoutSize = mImpl->mVisualModel->GetActualSize();
938 if( ALIGN & operations )
940 // The laid-out lines.
941 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
943 mImpl->mLayoutEngine.Align( layoutSize,
949 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
953 void Controller::SetMultiLineEnabled( bool enable )
955 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
957 if( layout != mImpl->mLayoutEngine.GetLayout() )
959 // Set the layout type.
960 mImpl->mLayoutEngine.SetLayout( layout );
962 // Set the flags to redo the layout operations
963 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
968 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
970 mImpl->RequestRelayout();
974 bool Controller::IsMultiLineEnabled() const
976 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
979 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
981 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
983 // Set the alignment.
984 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
986 // Set the flag to redo the alignment operation.
987 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
989 mImpl->RequestRelayout();
993 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
995 return mImpl->mLayoutEngine.GetHorizontalAlignment();
998 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1000 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1002 // Set the alignment.
1003 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1005 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1007 mImpl->RequestRelayout();
1011 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1013 return mImpl->mLayoutEngine.GetVerticalAlignment();
1016 void Controller::CalculateTextAlignment( const Size& size )
1018 // Get the direction of the first character.
1019 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1021 Size actualSize = mImpl->mVisualModel->GetActualSize();
1022 if( fabsf( actualSize.height ) < Math::MACHINE_EPSILON_1000 )
1024 // Get the line height of the default font.
1025 actualSize.height = mImpl->GetDefaultFontLineHeight();
1028 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1029 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1030 if( firstParagraphDirection &&
1031 ( LayoutEngine::HORIZONTAL_ALIGN_CENTER != horizontalAlignment ) )
1033 if( LayoutEngine::HORIZONTAL_ALIGN_BEGIN == horizontalAlignment )
1035 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1039 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1043 switch( horizontalAlignment )
1045 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1047 mImpl->mAlignmentOffset.x = 0.f;
1050 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1052 const int intOffset = static_cast<int>( 0.5f * ( size.width - actualSize.width ) ); // try to avoid pixel alignment.
1053 mImpl->mAlignmentOffset.x = static_cast<float>( intOffset );
1056 case LayoutEngine::HORIZONTAL_ALIGN_END:
1058 mImpl->mAlignmentOffset.x = size.width - actualSize.width;
1063 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1064 switch( verticalAlignment )
1066 case LayoutEngine::VERTICAL_ALIGN_TOP:
1068 mImpl->mAlignmentOffset.y = 0.f;
1071 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1073 const int intOffset = static_cast<int>( 0.5f * ( size.height - actualSize.height ) ); // try to avoid pixel alignment.
1074 mImpl->mAlignmentOffset.y = static_cast<float>( intOffset );
1077 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1079 mImpl->mAlignmentOffset.y = size.height - actualSize.height;
1085 LayoutEngine& Controller::GetLayoutEngine()
1087 return mImpl->mLayoutEngine;
1090 View& Controller::GetView()
1092 return mImpl->mView;
1095 void Controller::KeyboardFocusGainEvent()
1097 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1099 if( mImpl->mEventData )
1101 mImpl->ChangeState( EventData::EDITING );
1103 if( mImpl->IsShowingPlaceholderText() )
1105 // Show alternative placeholder-text when editing
1106 ShowPlaceholderText();
1109 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1110 mImpl->RequestRelayout();
1114 void Controller::KeyboardFocusLostEvent()
1116 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1118 if( mImpl->mEventData )
1120 if ( EventData::INTERRUPTED != mImpl->mEventData->mState )
1122 mImpl->ChangeState( EventData::INACTIVE );
1124 if( mImpl->IsShowingPlaceholderText() )
1126 // Revert to regular placeholder-text when not editing
1127 ShowPlaceholderText();
1131 mImpl->RequestRelayout();
1134 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1136 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1138 bool textChanged( false );
1140 if( mImpl->mEventData &&
1141 keyEvent.state == KeyEvent::Down )
1143 int keyCode = keyEvent.keyCode;
1144 const std::string& keyString = keyEvent.keyPressed;
1146 // Pre-process to separate modifying events from non-modifying input events.
1147 if( Dali::DALI_KEY_ESCAPE == keyCode )
1149 // Escape key is a special case which causes focus loss
1150 KeyboardFocusLostEvent();
1152 else if( Dali::DALI_KEY_CURSOR_LEFT == keyCode ||
1153 Dali::DALI_KEY_CURSOR_RIGHT == keyCode ||
1154 Dali::DALI_KEY_CURSOR_UP == keyCode ||
1155 Dali::DALI_KEY_CURSOR_DOWN == keyCode )
1157 Event event( Event::CURSOR_KEY_EVENT );
1158 event.p1.mInt = keyCode;
1159 mImpl->mEventData->mEventQueue.push_back( event );
1161 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1163 textChanged = BackspaceKeyEvent();
1165 else if ( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1167 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1168 // Avoids calling the InsertText() method which can delete selected text
1170 else if ( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1171 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1173 mImpl->ChangeState( EventData::INACTIVE );
1174 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1175 // Avoids calling the InsertText() method which can delete selected text
1177 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1179 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1180 // and a character is typed after the type of a upper case latin character.
1186 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1188 // IMF manager is no longer handling key-events
1189 mImpl->ClearPreEditFlag();
1191 InsertText( keyString, COMMIT );
1195 if ( mImpl->mEventData->mState != EventData::INTERRUPTED && mImpl->mEventData->mState != EventData::INACTIVE )
1197 mImpl->ChangeState( EventData::EDITING );
1200 mImpl->RequestRelayout();
1205 // Do this last since it provides callbacks into application code
1206 mImpl->mControlInterface.TextChanged();
1212 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1214 bool removedPrevious( false );
1215 bool maxLengthReached( false );
1217 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1218 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1219 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1220 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1222 // TODO: At the moment the underline runs are only for pre-edit.
1223 mImpl->mVisualModel->mUnderlineRuns.Clear();
1225 Vector<Character> utf32Characters;
1226 Length characterCount( 0u );
1228 // Remove the previous IMF pre-edit (predicitive text)
1229 if( mImpl->mEventData &&
1230 mImpl->mEventData->mPreEditFlag &&
1231 0 != mImpl->mEventData->mPreEditLength )
1233 CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1235 removedPrevious = RemoveText( -static_cast<int>(offset), mImpl->mEventData->mPreEditLength );
1237 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1238 mImpl->mEventData->mPreEditLength = 0;
1242 // Remove the previous Selection
1243 removedPrevious = RemoveSelectedText();
1248 // Convert text into UTF-32
1249 utf32Characters.Resize( text.size() );
1251 // This is a bit horrible but std::string returns a (signed) char*
1252 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1254 // Transform a text array encoded in utf8 into an array encoded in utf32.
1255 // It returns the actual number of characters.
1256 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1257 utf32Characters.Resize( characterCount );
1259 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1260 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1263 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1265 // The placeholder text is no longer needed
1266 if( mImpl->IsShowingPlaceholderText() )
1271 mImpl->ChangeState( EventData::EDITING );
1273 // Handle the IMF (predicitive text) state changes
1274 if( mImpl->mEventData )
1276 if( COMMIT == type )
1278 // IMF manager is no longer handling key-events
1279 mImpl->ClearPreEditFlag();
1283 if( !mImpl->mEventData->mPreEditFlag )
1285 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1287 // Record the start of the pre-edit text
1288 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1291 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1292 mImpl->mEventData->mPreEditFlag = true;
1294 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1298 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1300 // Restrict new text to fit within Maximum characters setting
1301 Length maxSizeOfNewText = std::min ( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1302 maxLengthReached = ( characterCount > maxSizeOfNewText );
1304 // Insert at current cursor position
1305 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1307 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1309 if( cursorIndex < numberOfCharactersInModel )
1311 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1315 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1318 cursorIndex += maxSizeOfNewText;
1320 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1323 if( 0u == mImpl->mLogicalModel->mText.Count() &&
1324 mImpl->IsPlaceholderAvailable() )
1326 // Show place-holder if empty after removing the pre-edit text
1327 ShowPlaceholderText();
1328 mImpl->mEventData->mUpdateCursorPosition = true;
1329 mImpl->ClearPreEditFlag();
1331 else if( removedPrevious ||
1332 0 != utf32Characters.Count() )
1334 // Queue an inserted event
1335 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1338 if( maxLengthReached )
1340 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1342 mImpl->ResetImfManager();
1344 // Do this last since it provides callbacks into application code
1345 mImpl->mControlInterface.MaxLengthReached();
1349 bool Controller::RemoveSelectedText()
1351 bool textRemoved( false );
1353 if ( EventData::SELECTING == mImpl->mEventData->mState ||
1354 EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
1356 std::string removedString;
1357 mImpl->RetrieveSelection( removedString, true );
1359 if( !removedString.empty() )
1362 mImpl->ChangeState( EventData::EDITING );
1369 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1371 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1373 if( NULL != mImpl->mEventData )
1375 if( 1u == tapCount )
1377 if( mImpl->IsShowingRealText() &&
1378 EventData::EDITING == mImpl->mEventData->mState )
1380 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
1382 else if( EventData::EDITING_WITH_GRAB_HANDLE != mImpl->mEventData->mState )
1384 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
1385 mImpl->ChangeState( EventData::EDITING );
1388 Event event( Event::TAP_EVENT );
1389 event.p1.mUint = tapCount;
1390 event.p2.mFloat = x;
1391 event.p3.mFloat = y;
1392 mImpl->mEventData->mEventQueue.push_back( event );
1394 mImpl->RequestRelayout();
1396 else if( 2u == tapCount )
1398 if( mImpl->mEventData->mSelectionEnabled &&
1399 mImpl->IsShowingRealText() )
1401 SelectEvent( x, y, false );
1406 // Reset keyboard as tap event has occurred.
1407 mImpl->ResetImfManager();
1410 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
1412 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1414 if( mImpl->mEventData )
1416 Event event( Event::PAN_EVENT );
1417 event.p1.mInt = state;
1418 event.p2.mFloat = displacement.x;
1419 event.p3.mFloat = displacement.y;
1420 mImpl->mEventData->mEventQueue.push_back( event );
1422 mImpl->RequestRelayout();
1426 void Controller::LongPressEvent( Gesture::State state, float x, float y )
1428 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1430 if ( mImpl->IsShowingPlaceholderText() || mImpl->mLogicalModel->mText.Count() == 0u )
1432 if ( mImpl->mEventData )
1434 Event event( Event::LONG_PRESS_EVENT );
1435 event.p1.mInt = state;
1436 mImpl->mEventData->mEventQueue.push_back( event );
1437 mImpl->RequestRelayout();
1440 else if( mImpl->mEventData )
1442 SelectEvent( x, y, false );
1446 void Controller::SelectEvent( float x, float y, bool selectAll )
1448 if( mImpl->mEventData )
1450 if ( mImpl->mEventData->mState == EventData::SELECTING )
1452 mImpl->ChangeState( EventData::SELECTION_CHANGED );
1456 mImpl->ChangeState( EventData::SELECTING );
1461 Event event( Event::SELECT_ALL );
1462 mImpl->mEventData->mEventQueue.push_back( event );
1466 Event event( Event::SELECT );
1467 event.p2.mFloat = x;
1468 event.p3.mFloat = y;
1469 mImpl->mEventData->mEventQueue.push_back( event );
1472 mImpl->RequestRelayout();
1476 void Controller::GetTargetSize( Vector2& targetSize )
1478 targetSize = mImpl->mVisualModel->mControlSize;
1481 void Controller::AddDecoration( Actor& actor, bool needsClipping )
1483 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
1486 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
1488 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
1490 if( mImpl->mEventData )
1492 switch( handleType )
1496 Event event( Event::GRAB_HANDLE_EVENT );
1497 event.p1.mUint = state;
1498 event.p2.mFloat = x;
1499 event.p3.mFloat = y;
1501 mImpl->mEventData->mEventQueue.push_back( event );
1504 case LEFT_SELECTION_HANDLE:
1506 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
1507 event.p1.mUint = state;
1508 event.p2.mFloat = x;
1509 event.p3.mFloat = y;
1511 mImpl->mEventData->mEventQueue.push_back( event );
1514 case RIGHT_SELECTION_HANDLE:
1516 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
1517 event.p1.mUint = state;
1518 event.p2.mFloat = x;
1519 event.p3.mFloat = y;
1521 mImpl->mEventData->mEventQueue.push_back( event );
1524 case LEFT_SELECTION_HANDLE_MARKER:
1525 case RIGHT_SELECTION_HANDLE_MARKER:
1527 // Markers do not move the handles.
1530 case HANDLE_TYPE_COUNT:
1532 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
1536 mImpl->RequestRelayout();
1540 void Controller::PasteText( const std::string& stringToPaste )
1542 InsertText( stringToPaste, Text::Controller::COMMIT );
1543 mImpl->ChangeState( EventData::EDITING );
1544 mImpl->RequestRelayout();
1547 void Controller::PasteClipboardItemEvent()
1549 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
1550 std::string stringToPaste( notifier.GetContent() );
1551 PasteText( stringToPaste );
1554 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
1556 if( NULL == mImpl->mEventData )
1563 case Toolkit::TextSelectionPopup::CUT:
1565 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
1566 mImpl->mOperationsPending = ALL_OPERATIONS;
1567 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1568 !mImpl->IsPlaceholderAvailable() )
1570 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1574 ShowPlaceholderText();
1575 mImpl->mEventData->mUpdateCursorPosition = true;
1577 mImpl->RequestRelayout();
1578 mImpl->mControlInterface.TextChanged();
1581 case Toolkit::TextSelectionPopup::COPY:
1583 mImpl->SendSelectionToClipboard( false ); // Text not modified
1584 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
1587 case Toolkit::TextSelectionPopup::PASTE:
1589 std::string stringToPaste("");
1590 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
1591 PasteText( stringToPaste );
1594 case Toolkit::TextSelectionPopup::SELECT:
1596 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
1598 if( mImpl->mEventData->mSelectionEnabled )
1600 // Creates a SELECT event.
1601 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
1605 case Toolkit::TextSelectionPopup::SELECT_ALL:
1607 // Creates a SELECT_ALL event
1608 SelectEvent( 0.f, 0.f, true );
1611 case Toolkit::TextSelectionPopup::CLIPBOARD:
1613 mImpl->ShowClipboard();
1616 case Toolkit::TextSelectionPopup::NONE:
1624 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
1626 bool update( false );
1627 bool requestRelayout = false;
1630 unsigned int cursorPosition( 0 );
1632 switch ( imfEvent.eventName )
1634 case ImfManager::COMMIT:
1636 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
1637 requestRelayout = true;
1640 case ImfManager::PREEDIT:
1642 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
1644 requestRelayout = true;
1647 case ImfManager::DELETESURROUNDING:
1649 update = RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
1653 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1654 !mImpl->IsPlaceholderAvailable() )
1656 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1660 ShowPlaceholderText();
1661 mImpl->mEventData->mUpdateCursorPosition = true;
1664 requestRelayout = true;
1667 case ImfManager::GETSURROUNDING:
1670 cursorPosition = GetLogicalCursorPosition();
1672 imfManager.SetSurroundingText( text );
1673 imfManager.SetCursorPosition( cursorPosition );
1676 case ImfManager::VOID:
1683 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
1686 cursorPosition = GetLogicalCursorPosition();
1689 if( requestRelayout )
1691 mImpl->mOperationsPending = ALL_OPERATIONS;
1692 mImpl->RequestRelayout();
1694 // Do this last since it provides callbacks into application code
1695 mImpl->mControlInterface.TextChanged();
1698 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
1700 return callbackData;
1703 Controller::~Controller()
1708 bool Controller::BackspaceKeyEvent()
1710 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
1712 // IMF manager is no longer handling key-events
1713 mImpl->ClearPreEditFlag();
1715 bool removed( false );
1717 if ( EventData::SELECTING == mImpl->mEventData->mState ||
1718 EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
1720 removed = RemoveSelectedText();
1722 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
1724 // Remove the character before the current cursor position
1725 removed = RemoveText( -1, 1 );
1730 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1731 !mImpl->IsPlaceholderAvailable() )
1733 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1737 ShowPlaceholderText();
1738 mImpl->mEventData->mUpdateCursorPosition = true;
1745 void Controller::ShowPlaceholderText()
1747 if( mImpl->IsPlaceholderAvailable() )
1749 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
1751 mImpl->mEventData->mIsShowingPlaceholderText = true;
1753 // Disable handles when showing place-holder text
1754 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
1755 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
1756 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
1758 const char* text( NULL );
1761 // TODO - Switch placeholder text styles when changing state
1762 if( EventData::INACTIVE != mImpl->mEventData->mState &&
1763 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() )
1765 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
1766 size = mImpl->mEventData->mPlaceholderTextActive.size();
1770 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
1771 size = mImpl->mEventData->mPlaceholderTextInactive.size();
1774 // Reset model for showing placeholder.
1775 mImpl->mLogicalModel->mText.Clear();
1777 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
1779 // Convert text into UTF-32
1780 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
1781 utf32Characters.Resize( size );
1783 // This is a bit horrible but std::string returns a (signed) char*
1784 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
1786 // Transform a text array encoded in utf8 into an array encoded in utf32.
1787 // It returns the actual number of characters.
1788 Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
1789 utf32Characters.Resize( characterCount );
1791 // Reset the cursor position
1792 mImpl->mEventData->mPrimaryCursorPosition = 0;
1794 // The natural size needs to be re-calculated.
1795 mImpl->mRecalculateNaturalSize = true;
1797 // Apply modifications to the model
1798 mImpl->mOperationsPending = ALL_OPERATIONS;
1800 // Update the rest of the model during size negotiation
1801 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
1805 void Controller::ClearModelData()
1807 // n.b. This does not Clear the mText from mLogicalModel
1808 mImpl->mLogicalModel->mScriptRuns.Clear();
1809 mImpl->mLogicalModel->mFontRuns.Clear();
1810 mImpl->mLogicalModel->mLineBreakInfo.Clear();
1811 mImpl->mLogicalModel->mWordBreakInfo.Clear();
1812 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
1813 mImpl->mLogicalModel->mCharacterDirections.Clear();
1814 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
1815 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
1816 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
1817 mImpl->mVisualModel->mGlyphs.Clear();
1818 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
1819 mImpl->mVisualModel->mCharactersToGlyph.Clear();
1820 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
1821 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
1822 mImpl->mVisualModel->mGlyphPositions.Clear();
1823 mImpl->mVisualModel->mLines.Clear();
1824 mImpl->mVisualModel->ClearCaches();
1827 void Controller::ClearFontData()
1829 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
1830 mImpl->mLogicalModel->mFontRuns.Clear();
1831 mImpl->mVisualModel->mGlyphs.Clear();
1832 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
1833 mImpl->mVisualModel->mCharactersToGlyph.Clear();
1834 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
1835 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
1836 mImpl->mVisualModel->mGlyphPositions.Clear();
1837 mImpl->mVisualModel->mLines.Clear();
1838 mImpl->mVisualModel->ClearCaches();
1841 Controller::Controller( ControlInterface& controlInterface )
1844 mImpl = new Controller::Impl( controlInterface );
1849 } // namespace Toolkit