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>
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/multi-language-support.h>
32 #include <dali-toolkit/internal/text/script-run.h>
33 #include <dali-toolkit/internal/text/segmentation.h>
34 #include <dali-toolkit/internal/text/shaper.h>
35 #include <dali-toolkit/internal/text/text-controller-impl.h>
36 #include <dali-toolkit/internal/text/text-io.h>
37 #include <dali-toolkit/internal/text/text-view.h>
42 #if defined(DEBUG_ENABLED)
43 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_CONTROLS");
46 const float MAX_FLOAT = std::numeric_limits<float>::max();
48 const std::string EMPTY_STRING("");
50 float ConvertToEven( float value )
52 int intValue(static_cast<int>( value ));
53 return static_cast<float>(intValue % 2 == 0) ? intValue : (intValue + 1);
67 ControllerPtr Controller::New( ControlInterface& controlInterface )
69 return ControllerPtr( new Controller( controlInterface ) );
72 void Controller::EnableTextInput( DecoratorPtr decorator )
74 if( !mImpl->mEventData )
76 mImpl->mEventData = new EventData( decorator );
80 void Controller::SetText( const std::string& text )
82 // Remove the previously set text
85 CharacterIndex lastCursorIndex = 0u;
89 // Convert text into UTF-32
90 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
91 utf32Characters.Resize( text.size() );
93 // This is a bit horrible but std::string returns a (signed) char*
94 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
96 // Transform a text array encoded in utf8 into an array encoded in utf32.
97 // It returns the actual number of characters.
98 Length characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
99 utf32Characters.Resize( characterCount );
101 DALI_ASSERT_DEBUG( text.size() >= characterCount && "Invalid UTF32 conversion length" );
102 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, text.size(), mImpl->mLogicalModel->mText.Count() );
104 // To reset the cursor position
105 lastCursorIndex = characterCount;
107 // Update the rest of the model during size negotiation
108 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
110 // The natural size needs to be re-calculated.
111 mImpl->mRecalculateNaturalSize = true;
113 // Apply modifications to the model
114 mImpl->mOperationsPending = ALL_OPERATIONS;
118 ShowPlaceholderText();
121 // Resets the cursor position.
122 ResetCursorPosition( lastCursorIndex );
124 // Scrolls the text to make the cursor visible.
125 ResetScrollPosition();
127 mImpl->RequestRelayout();
129 if( mImpl->mEventData )
131 // Cancel previously queued events
132 mImpl->mEventData->mEventQueue.clear();
135 // Reset keyboard as text changed
136 mImpl->ResetImfManager();
138 // Do this last since it provides callbacks into application code
139 mImpl->mControlInterface.TextChanged();
142 void Controller::GetText( std::string& text ) const
144 if( ! mImpl->IsShowingPlaceholderText() )
146 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
148 if( 0u != utf32Characters.Count() )
150 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
155 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
159 unsigned int Controller::GetLogicalCursorPosition() const
161 if( mImpl->mEventData )
163 return mImpl->mEventData->mPrimaryCursorPosition;
169 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
171 if( mImpl->mEventData )
173 if( PLACEHOLDER_TYPE_INACTIVE == type )
175 mImpl->mEventData->mPlaceholderTextInactive = text;
179 mImpl->mEventData->mPlaceholderTextActive = text;
182 // Update placeholder if there is no text
183 if( mImpl->IsShowingPlaceholderText() ||
184 0u == mImpl->mLogicalModel->mText.Count() )
186 ShowPlaceholderText();
191 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
193 if( mImpl->mEventData )
195 if( PLACEHOLDER_TYPE_INACTIVE == type )
197 text = mImpl->mEventData->mPlaceholderTextInactive;
201 text = mImpl->mEventData->mPlaceholderTextActive;
206 void Controller::SetMaximumNumberOfCharacters( int maxCharacters )
208 if ( maxCharacters >= 0 )
210 mImpl->mMaximumNumberOfCharacters = maxCharacters;
214 int Controller::GetMaximumNumberOfCharacters()
216 return mImpl->mMaximumNumberOfCharacters;
219 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
221 if( !mImpl->mFontDefaults )
223 mImpl->mFontDefaults = new FontDefaults();
226 mImpl->mFontDefaults->mDefaultFontFamily = defaultFontFamily;
228 // Clear the font-specific data
231 mImpl->mOperationsPending = ALL_OPERATIONS;
232 mImpl->mRecalculateNaturalSize = true;
234 mImpl->RequestRelayout();
237 const std::string& Controller::GetDefaultFontFamily() const
239 if( mImpl->mFontDefaults )
241 return mImpl->mFontDefaults->mDefaultFontFamily;
247 void Controller::SetDefaultFontStyle( const std::string& defaultFontStyle )
249 if( !mImpl->mFontDefaults )
251 mImpl->mFontDefaults = new FontDefaults();
254 mImpl->mFontDefaults->mDefaultFontStyle = defaultFontStyle;
256 // Clear the font-specific data
259 mImpl->mOperationsPending = ALL_OPERATIONS;
260 mImpl->mRecalculateNaturalSize = true;
262 mImpl->RequestRelayout();
265 const std::string& Controller::GetDefaultFontStyle() const
267 if( mImpl->mFontDefaults )
269 return mImpl->mFontDefaults->mDefaultFontStyle;
275 void Controller::SetDefaultPointSize( float pointSize )
277 if( !mImpl->mFontDefaults )
279 mImpl->mFontDefaults = new FontDefaults();
282 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
284 // Clear the font-specific data
287 mImpl->mOperationsPending = ALL_OPERATIONS;
288 mImpl->mRecalculateNaturalSize = true;
290 mImpl->RequestRelayout();
293 float Controller::GetDefaultPointSize() const
295 if( mImpl->mFontDefaults )
297 return mImpl->mFontDefaults->mDefaultPointSize;
303 void Controller::SetTextColor( const Vector4& textColor )
305 mImpl->mTextColor = textColor;
307 if( !mImpl->IsShowingPlaceholderText() )
309 mImpl->mVisualModel->SetTextColor( textColor );
311 mImpl->RequestRelayout();
315 const Vector4& Controller::GetTextColor() const
317 return mImpl->mTextColor;
320 bool Controller::RemoveText( int cursorOffset, int numberOfChars )
322 bool removed( false );
324 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfChars %d\n",
325 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfChars );
327 if( ! mImpl->IsShowingPlaceholderText() )
329 // Delete at current cursor position
330 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
331 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
333 CharacterIndex cursorIndex = oldCursorIndex;
335 // Validate the cursor position & number of characters
336 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
338 cursorIndex = oldCursorIndex + cursorOffset;
341 if( (cursorIndex + numberOfChars) > currentText.Count() )
343 numberOfChars = currentText.Count() - cursorIndex;
346 if( (cursorIndex + numberOfChars) <= currentText.Count() )
348 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
349 Vector<Character>::Iterator last = first + numberOfChars;
351 currentText.Erase( first, last );
353 // Cursor position retreat
354 oldCursorIndex = cursorIndex;
356 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfChars );
364 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
366 if( mImpl->mEventData )
368 mImpl->mEventData->mPlaceholderTextColor = textColor;
371 if( mImpl->IsShowingPlaceholderText() )
373 mImpl->mVisualModel->SetTextColor( textColor );
374 mImpl->RequestRelayout();
378 const Vector4& Controller::GetPlaceholderTextColor() const
380 if( mImpl->mEventData )
382 return mImpl->mEventData->mPlaceholderTextColor;
388 void Controller::SetShadowOffset( const Vector2& shadowOffset )
390 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
392 mImpl->RequestRelayout();
395 const Vector2& Controller::GetShadowOffset() const
397 return mImpl->mVisualModel->GetShadowOffset();
400 void Controller::SetShadowColor( const Vector4& shadowColor )
402 mImpl->mVisualModel->SetShadowColor( shadowColor );
404 mImpl->RequestRelayout();
407 const Vector4& Controller::GetShadowColor() const
409 return mImpl->mVisualModel->GetShadowColor();
412 void Controller::SetUnderlineColor( const Vector4& color )
414 mImpl->mVisualModel->SetUnderlineColor( color );
416 mImpl->RequestRelayout();
419 const Vector4& Controller::GetUnderlineColor() const
421 return mImpl->mVisualModel->GetUnderlineColor();
424 void Controller::SetUnderlineEnabled( bool enabled )
426 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
428 mImpl->RequestRelayout();
431 bool Controller::IsUnderlineEnabled() const
433 return mImpl->mVisualModel->IsUnderlineEnabled();
436 void Controller::SetUnderlineHeight( float height )
438 mImpl->mVisualModel->SetUnderlineHeight( height );
440 mImpl->RequestRelayout();
443 float Controller::GetUnderlineHeight() const
445 return mImpl->mVisualModel->GetUnderlineHeight();
448 void Controller::SetEnableCursorBlink( bool enable )
450 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
452 if( mImpl->mEventData )
454 mImpl->mEventData->mCursorBlinkEnabled = enable;
457 mImpl->mEventData->mDecorator )
459 mImpl->mEventData->mDecorator->StopCursorBlink();
464 bool Controller::GetEnableCursorBlink() const
466 if( mImpl->mEventData )
468 return mImpl->mEventData->mCursorBlinkEnabled;
474 const Vector2& Controller::GetScrollPosition() const
476 if( mImpl->mEventData )
478 return mImpl->mEventData->mScrollPosition;
481 return Vector2::ZERO;
484 const Vector2& Controller::GetAlignmentOffset() const
486 return mImpl->mAlignmentOffset;
489 Vector3 Controller::GetNaturalSize()
491 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
494 // Make sure the model is up-to-date before layouting
495 ProcessModifyEvents();
497 if( mImpl->mRecalculateNaturalSize )
499 // Operations that can be done only once until the text changes.
500 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
508 // Make sure the model is up-to-date before layouting
509 mImpl->UpdateModel( onlyOnceOperations );
511 // Operations that need to be done if the size changes.
512 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
516 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
517 static_cast<OperationsMask>( onlyOnceOperations |
519 naturalSize.GetVectorXY() );
521 // Do not do again the only once operations.
522 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
524 // Do the size related operations again.
525 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
527 // Stores the natural size to avoid recalculate it again
528 // unless the text/style changes.
529 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
531 mImpl->mRecalculateNaturalSize = false;
533 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
537 naturalSize = mImpl->mVisualModel->GetNaturalSize();
539 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
542 naturalSize.x = ConvertToEven( naturalSize.x );
543 naturalSize.y = ConvertToEven( naturalSize.y );
548 float Controller::GetHeightForWidth( float width )
550 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
551 // Make sure the model is up-to-date before layouting
552 ProcessModifyEvents();
555 if( width != mImpl->mControlSize.width )
557 // Operations that can be done only once until the text changes.
558 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
566 // Make sure the model is up-to-date before layouting
567 mImpl->UpdateModel( onlyOnceOperations );
569 // Operations that need to be done if the size changes.
570 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
574 DoRelayout( Size( width, MAX_FLOAT ),
575 static_cast<OperationsMask>( onlyOnceOperations |
579 // Do not do again the only once operations.
580 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
582 // Do the size related operations again.
583 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
584 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
588 layoutSize = mImpl->mVisualModel->GetActualSize();
589 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
592 return layoutSize.height;
595 bool Controller::Relayout( const Size& size )
597 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
599 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
601 bool glyphsRemoved( false );
602 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
604 mImpl->mVisualModel->mGlyphPositions.Clear();
605 glyphsRemoved = true;
607 // Not worth to relayout if width or height is equal to zero.
608 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
609 return glyphsRemoved;
612 if( size != mImpl->mControlSize )
614 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mControlSize.width, mImpl->mControlSize.height );
616 // Operations that need to be done if the size changes.
617 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
623 mImpl->mControlSize = size;
626 // Make sure the model is up-to-date before layouting
627 ProcessModifyEvents();
628 mImpl->UpdateModel( mImpl->mOperationsPending );
631 bool updated = DoRelayout( mImpl->mControlSize,
632 mImpl->mOperationsPending,
635 // Do not re-do any operation until something changes.
636 mImpl->mOperationsPending = NO_OPERATION;
638 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
639 CalculateTextAlignment( size );
641 if( mImpl->mEventData )
643 // Move the cursor, grab handle etc.
644 updated = mImpl->ProcessInputEvents() || updated;
647 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
651 void Controller::ProcessModifyEvents()
653 std::vector<ModifyEvent>& events = mImpl->mModifyEvents;
655 for( unsigned int i=0; i<events.size(); ++i )
657 if( ModifyEvent::TEXT_REPLACED == events[0].type )
659 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
660 DALI_ASSERT_DEBUG( 0 == i && "Unexpected TEXT_REPLACED event" );
664 else if( ModifyEvent::TEXT_INSERTED == events[0].type )
668 else if( ModifyEvent::TEXT_DELETED == events[0].type )
670 // Placeholder-text cannot be deleted
671 if( !mImpl->IsShowingPlaceholderText() )
678 // Discard temporary text
682 void Controller::ResetText()
685 mImpl->mLogicalModel->mText.Clear();
688 // We have cleared everything including the placeholder-text
689 mImpl->PlaceholderCleared();
691 // The natural size needs to be re-calculated.
692 mImpl->mRecalculateNaturalSize = true;
694 // Apply modifications to the model
695 mImpl->mOperationsPending = ALL_OPERATIONS;
698 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
700 // Reset the cursor position
701 if( NULL != mImpl->mEventData )
703 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
705 // Update the cursor if it's in editing mode.
706 if( ( EventData::EDITING == mImpl->mEventData->mState ) ||
707 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) )
709 mImpl->mEventData->mUpdateCursorPosition = true;
714 void Controller::ResetScrollPosition()
716 if( NULL != mImpl->mEventData )
718 // Reset the scroll position.
719 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
720 mImpl->mEventData->mScrollAfterUpdatePosition = true;
724 void Controller::TextReplacedEvent()
729 // The natural size needs to be re-calculated.
730 mImpl->mRecalculateNaturalSize = true;
732 // Apply modifications to the model
733 mImpl->mOperationsPending = ALL_OPERATIONS;
734 mImpl->UpdateModel( ALL_OPERATIONS );
735 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
741 void Controller::TextInsertedEvent()
743 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
745 // TODO - Optimize this
748 // The natural size needs to be re-calculated.
749 mImpl->mRecalculateNaturalSize = true;
751 // Apply modifications to the model; TODO - Optimize this
752 mImpl->mOperationsPending = ALL_OPERATIONS;
753 mImpl->UpdateModel( ALL_OPERATIONS );
754 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
759 // Queue a cursor reposition event; this must wait until after DoRelayout()
760 mImpl->mEventData->mUpdateCursorPosition = true;
761 mImpl->mEventData->mScrollAfterUpdatePosition = true;
764 void Controller::TextDeletedEvent()
766 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
768 // TODO - Optimize this
771 // The natural size needs to be re-calculated.
772 mImpl->mRecalculateNaturalSize = true;
774 // Apply modifications to the model; TODO - Optimize this
775 mImpl->mOperationsPending = ALL_OPERATIONS;
776 mImpl->UpdateModel( ALL_OPERATIONS );
777 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
782 // Queue a cursor reposition event; this must wait until after DoRelayout()
783 mImpl->mEventData->mScrollAfterDelete = true;
786 bool Controller::DoRelayout( const Size& size,
787 OperationsMask operationsRequired,
790 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
791 bool viewUpdated( false );
793 // Calculate the operations to be done.
794 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
796 if( LAYOUT & operations )
798 // Some vectors with data needed to layout and reorder may be void
799 // after the first time the text has been laid out.
800 // Fill the vectors again.
802 Length numberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
804 if( 0u == numberOfGlyphs )
806 // Nothing else to do if there is no glyphs.
807 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
811 Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
812 Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
813 Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
814 Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
815 Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
816 Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
818 // Set the layout parameters.
819 LayoutParameters layoutParameters( size,
820 mImpl->mLogicalModel->mText.Begin(),
821 lineBreakInfo.Begin(),
822 wordBreakInfo.Begin(),
823 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
826 glyphsToCharactersMap.Begin(),
827 charactersPerGlyph.Begin() );
829 // The laid-out lines.
830 // It's not possible to know in how many lines the text is going to be laid-out,
831 // but it can be resized at least with the number of 'paragraphs' to avoid
832 // some re-allocations.
833 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
835 // Delete any previous laid out lines before setting the new ones.
838 // The capacity of the bidirectional paragraph info is the number of paragraphs.
839 lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
841 // Resize the vector of positions to have the same size than the vector of glyphs.
842 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
843 glyphPositions.Resize( numberOfGlyphs );
845 // Update the visual model.
846 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
854 if( REORDER & operations )
856 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
858 // Check first if there are paragraphs with bidirectional info.
859 if( 0u != bidirectionalInfo.Count() )
862 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
864 // Reorder the lines.
865 Vector<BidirectionalLineInfoRun> lineBidirectionalInfoRuns;
866 lineBidirectionalInfoRuns.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
867 ReorderLines( bidirectionalInfo,
869 lineBidirectionalInfoRuns );
871 // Set the bidirectional info into the model.
872 const Length numberOfBidirectionalInfoRuns = lineBidirectionalInfoRuns.Count();
873 mImpl->mLogicalModel->SetVisualToLogicalMap( lineBidirectionalInfoRuns.Begin(),
874 numberOfBidirectionalInfoRuns );
876 // Set the bidirectional info per line into the layout parameters.
877 layoutParameters.lineBidirectionalInfoRunsBuffer = lineBidirectionalInfoRuns.Begin();
878 layoutParameters.numberOfBidirectionalInfoRuns = numberOfBidirectionalInfoRuns;
880 // Get the character to glyph conversion table and set into the layout.
881 layoutParameters.charactersToGlyphsBuffer = mImpl->mVisualModel->mCharactersToGlyph.Begin();
883 // Get the glyphs per character table and set into the layout.
884 layoutParameters.glyphsPerCharacterBuffer = mImpl->mVisualModel->mGlyphsPerCharacter.Begin();
886 // Re-layout the text. Reorder those lines with right to left characters.
887 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
890 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
891 for( Vector<BidirectionalLineInfoRun>::Iterator it = lineBidirectionalInfoRuns.Begin(),
892 endIt = lineBidirectionalInfoRuns.End();
896 BidirectionalLineInfoRun& bidiLineInfo = *it;
898 free( bidiLineInfo.visualToLogicalMap );
903 // Sets the actual size.
904 if( UPDATE_ACTUAL_SIZE & operations )
906 mImpl->mVisualModel->SetActualSize( layoutSize );
912 layoutSize = mImpl->mVisualModel->GetActualSize();
915 if( ALIGN & operations )
917 // The laid-out lines.
918 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
920 mImpl->mLayoutEngine.Align( layoutSize,
926 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
930 void Controller::SetMultiLineEnabled( bool enable )
932 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
934 if( layout != mImpl->mLayoutEngine.GetLayout() )
936 // Set the layout type.
937 mImpl->mLayoutEngine.SetLayout( layout );
939 // Set the flags to redo the layout operations
940 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
945 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
947 mImpl->RequestRelayout();
951 bool Controller::IsMultiLineEnabled() const
953 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
956 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
958 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
960 // Set the alignment.
961 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
963 // Set the flag to redo the alignment operation.
964 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
966 mImpl->RequestRelayout();
970 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
972 return mImpl->mLayoutEngine.GetHorizontalAlignment();
975 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
977 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
979 // Set the alignment.
980 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
982 // Set the flag to redo the alignment operation.
983 // TODO : Is not needed re-layout and reorder again but with the current implementation it is.
984 // Im working on a different patch to fix an issue with the alignment. When that patch
985 // is in, this issue can be fixed.
986 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
991 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
993 mImpl->RequestRelayout();
997 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
999 return mImpl->mLayoutEngine.GetVerticalAlignment();
1002 void Controller::CalculateTextAlignment( const Size& size )
1004 // Get the direction of the first character.
1005 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1007 const Size& actualSize = mImpl->mVisualModel->GetActualSize();
1009 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1010 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1011 if( firstParagraphDirection &&
1012 ( LayoutEngine::HORIZONTAL_ALIGN_CENTER != horizontalAlignment ) )
1014 if( LayoutEngine::HORIZONTAL_ALIGN_BEGIN == horizontalAlignment )
1016 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1020 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1024 switch( horizontalAlignment )
1026 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1028 mImpl->mAlignmentOffset.x = 0.f;
1031 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1033 const int intOffset = static_cast<int>( 0.5f * ( size.width - actualSize.width ) ); // try to avoid pixel alignment.
1034 mImpl->mAlignmentOffset.x = static_cast<float>( intOffset );
1037 case LayoutEngine::HORIZONTAL_ALIGN_END:
1039 mImpl->mAlignmentOffset.x = size.width - actualSize.width;
1044 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1045 switch( verticalAlignment )
1047 case LayoutEngine::VERTICAL_ALIGN_TOP:
1049 mImpl->mAlignmentOffset.y = 0.f;
1052 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1054 const int intOffset = static_cast<int>( 0.5f * ( size.height - actualSize.height ) ); // try to avoid pixel alignment.
1055 mImpl->mAlignmentOffset.y = static_cast<float>( intOffset );
1058 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1060 mImpl->mAlignmentOffset.y = size.height - actualSize.height;
1066 LayoutEngine& Controller::GetLayoutEngine()
1068 return mImpl->mLayoutEngine;
1071 View& Controller::GetView()
1073 return mImpl->mView;
1076 void Controller::KeyboardFocusGainEvent()
1078 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1080 if( mImpl->mEventData )
1082 mImpl->ChangeState( EventData::EDITING );
1084 if( mImpl->IsShowingPlaceholderText() )
1086 // Show alternative placeholder-text when editing
1087 ShowPlaceholderText();
1090 mImpl->RequestRelayout();
1094 void Controller::KeyboardFocusLostEvent()
1096 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1098 if( mImpl->mEventData )
1100 mImpl->ChangeState( EventData::INACTIVE );
1102 if( mImpl->IsShowingPlaceholderText() )
1104 // Revert to regular placeholder-text when not editing
1105 ShowPlaceholderText();
1108 mImpl->RequestRelayout();
1112 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1114 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1116 bool textChanged( false );
1118 if( mImpl->mEventData &&
1119 keyEvent.state == KeyEvent::Down )
1121 int keyCode = keyEvent.keyCode;
1122 const std::string& keyString = keyEvent.keyPressed;
1124 // Pre-process to separate modifying events from non-modifying input events.
1125 if( Dali::DALI_KEY_ESCAPE == keyCode )
1127 // Escape key is a special case which causes focus loss
1128 KeyboardFocusLostEvent();
1130 else if( Dali::DALI_KEY_CURSOR_LEFT == keyCode ||
1131 Dali::DALI_KEY_CURSOR_RIGHT == keyCode ||
1132 Dali::DALI_KEY_CURSOR_UP == keyCode ||
1133 Dali::DALI_KEY_CURSOR_DOWN == keyCode )
1135 Event event( Event::CURSOR_KEY_EVENT );
1136 event.p1.mInt = keyCode;
1137 mImpl->mEventData->mEventQueue.push_back( event );
1139 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1141 textChanged = BackspaceKeyEvent();
1145 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1147 // IMF manager is no longer handling key-events
1148 mImpl->ClearPreEditFlag();
1150 InsertText( keyString, COMMIT );
1154 mImpl->ChangeState( EventData::EDITING ); // todo Confirm this is the best place to change the state of
1156 mImpl->RequestRelayout();
1161 // Do this last since it provides callbacks into application code
1162 mImpl->mControlInterface.TextChanged();
1168 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1170 bool removedPrevious( false );
1171 bool maxLengthReached( false );
1173 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1174 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1175 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1176 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1178 Vector<Character> utf32Characters;
1179 Length characterCount( 0u );
1181 // Remove the previous IMF pre-edit (predicitive text)
1182 if( mImpl->mEventData &&
1183 mImpl->mEventData->mPreEditFlag &&
1184 0 != mImpl->mEventData->mPreEditLength )
1186 CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1187 removedPrevious = RemoveText( -static_cast<int>(offset), mImpl->mEventData->mPreEditLength );
1189 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1190 mImpl->mEventData->mPreEditLength = 0;
1194 // Remove the previous Selection
1195 removedPrevious = RemoveSelectedText();
1198 if( ! text.empty() )
1200 // Convert text into UTF-32
1201 utf32Characters.Resize( text.size() );
1203 // This is a bit horrible but std::string returns a (signed) char*
1204 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1206 // Transform a text array encoded in utf8 into an array encoded in utf32.
1207 // It returns the actual number of characters.
1208 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1209 utf32Characters.Resize( characterCount );
1211 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1212 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1215 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1217 // The placeholder text is no longer needed
1218 if( mImpl->IsShowingPlaceholderText() )
1223 // Handle the IMF (predicitive text) state changes
1224 if( mImpl->mEventData )
1226 if( COMMIT == type )
1228 // IMF manager is no longer handling key-events
1229 mImpl->ClearPreEditFlag();
1233 if( ! mImpl->mEventData->mPreEditFlag )
1235 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1237 // Record the start of the pre-edit text
1238 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1241 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1242 mImpl->mEventData->mPreEditFlag = true;
1244 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1248 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1250 // Restrict new text to fit within Maximum characters setting
1251 Length maxSizeOfNewText = std::min ( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1252 maxLengthReached = ( characterCount > maxSizeOfNewText );
1254 // Insert at current cursor position
1255 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1257 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1259 if( cursorIndex < numberOfCharactersInModel )
1261 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1265 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1268 cursorIndex += maxSizeOfNewText;
1270 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1273 if( 0u == mImpl->mLogicalModel->mText.Count() &&
1274 mImpl->IsPlaceholderAvailable() )
1276 // Show place-holder if empty after removing the pre-edit text
1277 ShowPlaceholderText();
1278 mImpl->mEventData->mUpdateCursorPosition = true;
1279 mImpl->ClearPreEditFlag();
1281 else if( removedPrevious ||
1282 0 != utf32Characters.Count() )
1284 // Queue an inserted event
1285 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1288 if( maxLengthReached )
1290 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1292 mImpl->ResetImfManager();
1294 // Do this last since it provides callbacks into application code
1295 mImpl->mControlInterface.MaxLengthReached();
1299 bool Controller::RemoveSelectedText()
1301 bool textRemoved( false );
1303 if ( EventData::SELECTING == mImpl->mEventData->mState ||
1304 EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
1306 std::string removedString;
1307 mImpl->RetrieveSelection( removedString, true );
1309 if( !removedString.empty() )
1312 mImpl->ChangeState( EventData::EDITING );
1319 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1321 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1323 if( NULL != mImpl->mEventData )
1325 const bool isShowingPlaceholderText = mImpl->IsShowingPlaceholderText();
1326 if( 1u == tapCount )
1328 bool tapDuringEditMode( EventData::EDITING == mImpl->mEventData->mState );
1330 if( !isShowingPlaceholderText && tapDuringEditMode )
1332 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, true );
1333 mImpl->mEventData->mDecorator->SetPopupActive( false );
1336 mImpl->ChangeState( EventData::EDITING );
1338 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
1339 if( mImpl->mEventData )
1341 Event event( Event::TAP_EVENT );
1342 event.p1.mUint = tapCount;
1343 event.p2.mFloat = x;
1344 event.p3.mFloat = y;
1345 mImpl->mEventData->mEventQueue.push_back( event );
1347 mImpl->RequestRelayout();
1350 else if( !isShowingPlaceholderText &&
1351 mImpl->mEventData->mSelectionEnabled &&
1352 ( 2u == tapCount ) )
1354 SelectEvent( x, y, false );
1358 // Reset keyboard as tap event has occurred.
1359 mImpl->ResetImfManager();
1362 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
1364 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1366 if( mImpl->mEventData )
1368 Event event( Event::PAN_EVENT );
1369 event.p1.mInt = state;
1370 event.p2.mFloat = displacement.x;
1371 event.p3.mFloat = displacement.y;
1372 mImpl->mEventData->mEventQueue.push_back( event );
1374 mImpl->RequestRelayout();
1378 void Controller::SelectEvent( float x, float y, bool selectAll )
1380 if( mImpl->mEventData )
1382 if ( mImpl->mEventData->mState == EventData::SELECTING )
1384 mImpl->ChangeState( EventData::SELECTION_CHANGED );
1388 mImpl->ChangeState( EventData::SELECTING );
1393 Event event( Event::SELECT_ALL );
1394 mImpl->mEventData->mEventQueue.push_back( event );
1398 Event event( Event::SELECT );
1399 event.p2.mFloat = x;
1400 event.p3.mFloat = y;
1401 mImpl->mEventData->mEventQueue.push_back( event );
1404 mImpl->RequestRelayout();
1408 void Controller::GetTargetSize( Vector2& targetSize )
1410 targetSize = mImpl->mControlSize;
1413 void Controller::AddDecoration( Actor& actor, bool needsClipping )
1415 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
1418 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
1420 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
1422 if( mImpl->mEventData )
1424 switch( handleType )
1428 Event event( Event::GRAB_HANDLE_EVENT );
1429 event.p1.mUint = state;
1430 event.p2.mFloat = x;
1431 event.p3.mFloat = y;
1433 mImpl->mEventData->mEventQueue.push_back( event );
1436 case LEFT_SELECTION_HANDLE:
1438 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
1439 event.p1.mUint = state;
1440 event.p2.mFloat = x;
1441 event.p3.mFloat = y;
1443 mImpl->mEventData->mEventQueue.push_back( event );
1446 case RIGHT_SELECTION_HANDLE:
1448 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
1449 event.p1.mUint = state;
1450 event.p2.mFloat = x;
1451 event.p3.mFloat = y;
1453 mImpl->mEventData->mEventQueue.push_back( event );
1456 case HANDLE_TYPE_COUNT:
1458 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
1462 mImpl->RequestRelayout();
1466 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
1468 if( NULL == mImpl->mEventData )
1475 case Toolkit::TextSelectionPopup::CUT:
1477 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
1478 mImpl->mOperationsPending = ALL_OPERATIONS;
1479 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1480 !mImpl->IsPlaceholderAvailable() )
1482 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1486 ShowPlaceholderText();
1487 mImpl->mEventData->mUpdateCursorPosition = true;
1489 mImpl->RequestRelayout();
1490 mImpl->mControlInterface.TextChanged();
1493 case Toolkit::TextSelectionPopup::COPY:
1495 mImpl->SendSelectionToClipboard( false ); // Text not modified
1496 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
1499 case Toolkit::TextSelectionPopup::PASTE:
1501 std::string stringToPaste("");
1502 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
1503 InsertText( stringToPaste, Text::Controller::COMMIT );
1504 mImpl->ChangeState( EventData::EDITING );
1505 mImpl->RequestRelayout();
1508 case Toolkit::TextSelectionPopup::SELECT:
1510 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
1512 if( mImpl->mEventData->mSelectionEnabled )
1514 // Creates a SELECT event.
1515 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
1519 case Toolkit::TextSelectionPopup::SELECT_ALL:
1521 // Creates a SELECT_ALL event
1522 SelectEvent( 0.f, 0.f, true );
1525 case Toolkit::TextSelectionPopup::CLIPBOARD:
1529 case Toolkit::TextSelectionPopup::NONE:
1537 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
1539 bool update( false );
1540 bool requestRelayout = false;
1543 unsigned int cursorPosition( 0 );
1545 switch ( imfEvent.eventName )
1547 case ImfManager::COMMIT:
1549 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
1550 requestRelayout = true;
1553 case ImfManager::PREEDIT:
1555 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
1557 requestRelayout = true;
1560 case ImfManager::DELETESURROUNDING:
1562 RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
1563 requestRelayout = true;
1566 case ImfManager::GETSURROUNDING:
1569 cursorPosition = GetLogicalCursorPosition();
1571 imfManager.SetSurroundingText( text );
1572 imfManager.SetCursorPosition( cursorPosition );
1575 case ImfManager::VOID:
1582 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
1585 cursorPosition = GetLogicalCursorPosition();
1588 if( requestRelayout )
1590 mImpl->mOperationsPending = ALL_OPERATIONS;
1591 mImpl->RequestRelayout();
1593 // Do this last since it provides callbacks into application code
1594 mImpl->mControlInterface.TextChanged();
1597 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
1599 return callbackData;
1603 Controller::~Controller()
1608 bool Controller::BackspaceKeyEvent()
1610 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
1612 // IMF manager is no longer handling key-events
1613 mImpl->ClearPreEditFlag();
1615 bool removed( false );
1617 if ( EventData::SELECTING == mImpl->mEventData->mState ||
1618 EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
1620 removed = RemoveSelectedText();
1622 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
1624 // Remove the character before the current cursor position
1625 removed = RemoveText( -1, 1 );
1630 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1631 !mImpl->IsPlaceholderAvailable() )
1633 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1637 ShowPlaceholderText();
1638 mImpl->mEventData->mUpdateCursorPosition = true;
1645 void Controller::ShowPlaceholderText()
1647 if( mImpl->IsPlaceholderAvailable() )
1649 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
1651 mImpl->mEventData->mIsShowingPlaceholderText = true;
1653 // Disable handles when showing place-holder text
1654 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
1655 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
1656 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
1658 const char* text( NULL );
1661 // TODO - Switch placeholder text styles when changing state
1662 if( EventData::INACTIVE != mImpl->mEventData->mState &&
1663 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() )
1665 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
1666 size = mImpl->mEventData->mPlaceholderTextActive.size();
1670 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
1671 size = mImpl->mEventData->mPlaceholderTextInactive.size();
1674 // Reset model for showing placeholder.
1675 mImpl->mLogicalModel->mText.Clear();
1677 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
1679 // Convert text into UTF-32
1680 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
1681 utf32Characters.Resize( size );
1683 // This is a bit horrible but std::string returns a (signed) char*
1684 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
1686 // Transform a text array encoded in utf8 into an array encoded in utf32.
1687 // It returns the actual number of characters.
1688 Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
1689 utf32Characters.Resize( characterCount );
1691 // Reset the cursor position
1692 mImpl->mEventData->mPrimaryCursorPosition = 0;
1694 // The natural size needs to be re-calculated.
1695 mImpl->mRecalculateNaturalSize = true;
1697 // Apply modifications to the model
1698 mImpl->mOperationsPending = ALL_OPERATIONS;
1700 // Update the rest of the model during size negotiation
1701 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
1705 void Controller::ClearModelData()
1707 // n.b. This does not Clear the mText from mLogicalModel
1708 mImpl->mLogicalModel->mScriptRuns.Clear();
1709 mImpl->mLogicalModel->mFontRuns.Clear();
1710 mImpl->mLogicalModel->mLineBreakInfo.Clear();
1711 mImpl->mLogicalModel->mWordBreakInfo.Clear();
1712 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
1713 mImpl->mLogicalModel->mCharacterDirections.Clear();
1714 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
1715 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
1716 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
1717 mImpl->mVisualModel->mGlyphs.Clear();
1718 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
1719 mImpl->mVisualModel->mCharactersToGlyph.Clear();
1720 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
1721 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
1722 mImpl->mVisualModel->mGlyphPositions.Clear();
1723 mImpl->mVisualModel->mLines.Clear();
1724 mImpl->mVisualModel->ClearCaches();
1727 void Controller::ClearFontData()
1729 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
1730 mImpl->mLogicalModel->mFontRuns.Clear();
1731 mImpl->mVisualModel->mGlyphs.Clear();
1732 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
1733 mImpl->mVisualModel->mCharactersToGlyph.Clear();
1734 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
1735 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
1736 mImpl->mVisualModel->mGlyphPositions.Clear();
1737 mImpl->mVisualModel->mLines.Clear();
1738 mImpl->mVisualModel->ClearCaches();
1741 Controller::Controller( ControlInterface& controlInterface )
1744 mImpl = new Controller::Impl( controlInterface );
1749 } // namespace Toolkit