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();
41 const unsigned int POINTS_PER_INCH = 72;
43 const std::string EMPTY_STRING("");
44 const unsigned int ZERO = 0u;
46 float ConvertToEven( float value )
48 int intValue(static_cast<int>( value ));
49 return static_cast<float>(intValue % 2 == 0) ? intValue : (intValue + 1);
63 ControllerPtr Controller::New( ControlInterface& controlInterface )
65 return ControllerPtr( new Controller( controlInterface ) );
68 void Controller::EnableTextInput( DecoratorPtr decorator )
70 if( !mImpl->mEventData )
72 mImpl->mEventData = new EventData( decorator );
76 void Controller::SetText( const std::string& text )
78 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
80 // Reset keyboard as text changed
81 mImpl->ResetImfManager();
83 // Remove the previously set text
86 CharacterIndex lastCursorIndex = 0u;
88 if( mImpl->mEventData )
90 // If popup shown then hide it by switching to Editing state
91 if( ( EventData::SELECTING == mImpl->mEventData->mState ) ||
92 ( EventData::SELECTION_CHANGED == mImpl->mEventData->mState ) ||
93 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
94 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
96 mImpl->ChangeState( EventData::EDITING );
102 // Convert text into UTF-32
103 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
104 utf32Characters.Resize( text.size() );
106 // This is a bit horrible but std::string returns a (signed) char*
107 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
109 // Transform a text array encoded in utf8 into an array encoded in utf32.
110 // It returns the actual number of characters.
111 Length characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
112 utf32Characters.Resize( characterCount );
114 DALI_ASSERT_DEBUG( text.size() >= characterCount && "Invalid UTF32 conversion length" );
115 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, text.size(), mImpl->mLogicalModel->mText.Count() );
117 // To reset the cursor position
118 lastCursorIndex = characterCount;
120 // Update the rest of the model during size negotiation
121 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
123 // The natural size needs to be re-calculated.
124 mImpl->mRecalculateNaturalSize = true;
126 // Apply modifications to the model
127 mImpl->mOperationsPending = ALL_OPERATIONS;
131 ShowPlaceholderText();
134 // Resets the cursor position.
135 ResetCursorPosition( lastCursorIndex );
137 // Scrolls the text to make the cursor visible.
138 ResetScrollPosition();
140 mImpl->RequestRelayout();
142 if( mImpl->mEventData )
144 // Cancel previously queued events
145 mImpl->mEventData->mEventQueue.clear();
148 // Notify IMF as text changed
151 // Do this last since it provides callbacks into application code
152 mImpl->mControlInterface.TextChanged();
155 void Controller::GetText( std::string& text ) const
157 if( ! mImpl->IsShowingPlaceholderText() )
159 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
161 if( 0u != utf32Characters.Count() )
163 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
168 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
172 unsigned int Controller::GetLogicalCursorPosition() const
174 if( mImpl->mEventData )
176 return mImpl->mEventData->mPrimaryCursorPosition;
182 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
184 if( mImpl->mEventData )
186 if( PLACEHOLDER_TYPE_INACTIVE == type )
188 mImpl->mEventData->mPlaceholderTextInactive = text;
192 mImpl->mEventData->mPlaceholderTextActive = text;
195 // Update placeholder if there is no text
196 if( mImpl->IsShowingPlaceholderText() ||
197 0u == mImpl->mLogicalModel->mText.Count() )
199 ShowPlaceholderText();
204 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
206 if( mImpl->mEventData )
208 if( PLACEHOLDER_TYPE_INACTIVE == type )
210 text = mImpl->mEventData->mPlaceholderTextInactive;
214 text = mImpl->mEventData->mPlaceholderTextActive;
219 void Controller::SetMaximumNumberOfCharacters( int maxCharacters )
221 if ( maxCharacters >= 0 )
223 mImpl->mMaximumNumberOfCharacters = maxCharacters;
227 int Controller::GetMaximumNumberOfCharacters()
229 return mImpl->mMaximumNumberOfCharacters;
232 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily, bool userDefined )
234 if( !mImpl->mFontDefaults )
236 mImpl->mFontDefaults = new FontDefaults();
239 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
240 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s userDefined: %s\n", defaultFontFamily.c_str(), userDefined ? "true":"false" );
241 mImpl->mUserDefinedFontFamily = userDefined;
243 // Clear the font-specific data
246 mImpl->mOperationsPending = ALL_OPERATIONS;
247 mImpl->mRecalculateNaturalSize = true;
249 mImpl->RequestRelayout();
252 const std::string& Controller::GetDefaultFontFamily() const
254 if( mImpl->mFontDefaults )
256 return mImpl->mFontDefaults->mFontDescription.family;
262 void Controller::SetDefaultFontStyle( const std::string& style )
264 if( !mImpl->mFontDefaults )
266 mImpl->mFontDefaults = new FontDefaults();
269 mImpl->mFontDefaults->mFontStyle = style;
272 const std::string& Controller::GetDefaultFontStyle() const
274 if( mImpl->mFontDefaults )
276 return mImpl->mFontDefaults->mFontStyle;
282 void Controller::SetDefaultFontWidth( FontWidth width )
284 if( !mImpl->mFontDefaults )
286 mImpl->mFontDefaults = new FontDefaults();
289 mImpl->mFontDefaults->mFontDescription.width = width;
291 // Clear the font-specific data
294 mImpl->mOperationsPending = ALL_OPERATIONS;
295 mImpl->mRecalculateNaturalSize = true;
297 mImpl->RequestRelayout();
300 FontWidth Controller::GetDefaultFontWidth() const
302 if( mImpl->mFontDefaults )
304 return mImpl->mFontDefaults->mFontDescription.width;
307 return TextAbstraction::FontWidth::NORMAL;
310 void Controller::SetDefaultFontWeight( FontWeight weight )
312 if( !mImpl->mFontDefaults )
314 mImpl->mFontDefaults = new FontDefaults();
317 mImpl->mFontDefaults->mFontDescription.weight = weight;
319 // Clear the font-specific data
322 mImpl->mOperationsPending = ALL_OPERATIONS;
323 mImpl->mRecalculateNaturalSize = true;
325 mImpl->RequestRelayout();
328 FontWeight Controller::GetDefaultFontWeight() const
330 if( mImpl->mFontDefaults )
332 return mImpl->mFontDefaults->mFontDescription.weight;
335 return TextAbstraction::FontWeight::NORMAL;
338 void Controller::SetDefaultFontSlant( FontSlant slant )
340 if( !mImpl->mFontDefaults )
342 mImpl->mFontDefaults = new FontDefaults();
345 mImpl->mFontDefaults->mFontDescription.slant = slant;
347 // Clear the font-specific data
350 mImpl->mOperationsPending = ALL_OPERATIONS;
351 mImpl->mRecalculateNaturalSize = true;
353 mImpl->RequestRelayout();
356 FontSlant Controller::GetDefaultFontSlant() const
358 if( mImpl->mFontDefaults )
360 return mImpl->mFontDefaults->mFontDescription.slant;
363 return TextAbstraction::FontSlant::NORMAL;
366 void Controller::SetDefaultPointSize( float pointSize )
368 if( !mImpl->mFontDefaults )
370 mImpl->mFontDefaults = new FontDefaults();
373 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
375 unsigned int horizontalDpi( 0u );
376 unsigned int verticalDpi( 0u );
377 mImpl->mFontClient.GetDpi( horizontalDpi, verticalDpi );
379 // Adjust the metrics if the fixed-size font should be down-scaled
380 int maxEmojiSize( pointSize/POINTS_PER_INCH * verticalDpi );
381 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultPointSize %p setting MaxEmojiSize %d\n", this, maxEmojiSize );
382 mImpl->mMetrics->SetMaxEmojiSize( maxEmojiSize );
384 // Clear the font-specific data
387 mImpl->mOperationsPending = ALL_OPERATIONS;
388 mImpl->mRecalculateNaturalSize = true;
390 mImpl->RequestRelayout();
393 float Controller::GetDefaultPointSize() const
395 if( mImpl->mFontDefaults )
397 return mImpl->mFontDefaults->mDefaultPointSize;
403 void Controller::UpdateAfterFontChange( std::string& newDefaultFont )
405 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange");
407 if ( !mImpl->mUserDefinedFontFamily ) // If user defined font then should not update when system font changes
409 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
411 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
412 mImpl->UpdateModel( ALL_OPERATIONS );
413 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
414 mImpl->mRecalculateNaturalSize = true;
415 mImpl->RequestRelayout();
419 void Controller::SetTextColor( const Vector4& textColor )
421 mImpl->mTextColor = textColor;
423 if( !mImpl->IsShowingPlaceholderText() )
425 mImpl->mVisualModel->SetTextColor( textColor );
427 mImpl->RequestRelayout();
431 const Vector4& Controller::GetTextColor() const
433 return mImpl->mTextColor;
436 bool Controller::RemoveText( int cursorOffset, int numberOfChars )
438 bool removed( false );
440 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfChars %d\n",
441 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfChars );
443 if( !mImpl->IsShowingPlaceholderText() )
445 // Delete at current cursor position
446 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
447 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
449 CharacterIndex cursorIndex = oldCursorIndex;
451 // Validate the cursor position & number of characters
452 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
454 cursorIndex = oldCursorIndex + cursorOffset;
457 if( (cursorIndex + numberOfChars) > currentText.Count() )
459 numberOfChars = currentText.Count() - cursorIndex;
462 if( (cursorIndex + numberOfChars) <= currentText.Count() )
464 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
465 Vector<Character>::Iterator last = first + numberOfChars;
467 currentText.Erase( first, last );
469 // Cursor position retreat
470 oldCursorIndex = cursorIndex;
472 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfChars );
480 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
482 if( mImpl->mEventData )
484 mImpl->mEventData->mPlaceholderTextColor = textColor;
487 if( mImpl->IsShowingPlaceholderText() )
489 mImpl->mVisualModel->SetTextColor( textColor );
490 mImpl->RequestRelayout();
494 const Vector4& Controller::GetPlaceholderTextColor() const
496 if( mImpl->mEventData )
498 return mImpl->mEventData->mPlaceholderTextColor;
504 void Controller::SetShadowOffset( const Vector2& shadowOffset )
506 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
508 mImpl->RequestRelayout();
511 const Vector2& Controller::GetShadowOffset() const
513 return mImpl->mVisualModel->GetShadowOffset();
516 void Controller::SetShadowColor( const Vector4& shadowColor )
518 mImpl->mVisualModel->SetShadowColor( shadowColor );
520 mImpl->RequestRelayout();
523 const Vector4& Controller::GetShadowColor() const
525 return mImpl->mVisualModel->GetShadowColor();
528 void Controller::SetUnderlineColor( const Vector4& color )
530 mImpl->mVisualModel->SetUnderlineColor( color );
532 mImpl->RequestRelayout();
535 const Vector4& Controller::GetUnderlineColor() const
537 return mImpl->mVisualModel->GetUnderlineColor();
540 void Controller::SetUnderlineEnabled( bool enabled )
542 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
544 mImpl->RequestRelayout();
547 bool Controller::IsUnderlineEnabled() const
549 return mImpl->mVisualModel->IsUnderlineEnabled();
552 void Controller::SetUnderlineHeight( float height )
554 mImpl->mVisualModel->SetUnderlineHeight( height );
556 mImpl->RequestRelayout();
559 float Controller::GetUnderlineHeight() const
561 return mImpl->mVisualModel->GetUnderlineHeight();
564 void Controller::SetEnableCursorBlink( bool enable )
566 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
568 if( mImpl->mEventData )
570 mImpl->mEventData->mCursorBlinkEnabled = enable;
573 mImpl->mEventData->mDecorator )
575 mImpl->mEventData->mDecorator->StopCursorBlink();
580 bool Controller::GetEnableCursorBlink() const
582 if( mImpl->mEventData )
584 return mImpl->mEventData->mCursorBlinkEnabled;
590 const Vector2& Controller::GetScrollPosition() const
592 if( mImpl->mEventData )
594 return mImpl->mEventData->mScrollPosition;
597 return Vector2::ZERO;
600 const Vector2& Controller::GetAlignmentOffset() const
602 return mImpl->mAlignmentOffset;
605 Vector3 Controller::GetNaturalSize()
607 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
610 // Make sure the model is up-to-date before layouting
611 ProcessModifyEvents();
613 if( mImpl->mRecalculateNaturalSize )
615 // Operations that can be done only once until the text changes.
616 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
624 // Make sure the model is up-to-date before layouting
625 mImpl->UpdateModel( onlyOnceOperations );
627 // Operations that need to be done if the size changes.
628 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
632 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
633 static_cast<OperationsMask>( onlyOnceOperations |
635 naturalSize.GetVectorXY() );
637 // Do not do again the only once operations.
638 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
640 // Do the size related operations again.
641 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
643 // Stores the natural size to avoid recalculate it again
644 // unless the text/style changes.
645 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
647 mImpl->mRecalculateNaturalSize = false;
649 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
653 naturalSize = mImpl->mVisualModel->GetNaturalSize();
655 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
658 naturalSize.x = ConvertToEven( naturalSize.x );
659 naturalSize.y = ConvertToEven( naturalSize.y );
664 float Controller::GetHeightForWidth( float width )
666 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
667 // Make sure the model is up-to-date before layouting
668 ProcessModifyEvents();
671 if( width != mImpl->mVisualModel->mControlSize.width )
673 // Operations that can be done only once until the text changes.
674 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
682 // Make sure the model is up-to-date before layouting
683 mImpl->UpdateModel( onlyOnceOperations );
685 // Operations that need to be done if the size changes.
686 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
690 DoRelayout( Size( width, MAX_FLOAT ),
691 static_cast<OperationsMask>( onlyOnceOperations |
695 // Do not do again the only once operations.
696 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
698 // Do the size related operations again.
699 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
700 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
704 layoutSize = mImpl->mVisualModel->GetActualSize();
705 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
708 return layoutSize.height;
711 bool Controller::Relayout( const Size& size )
713 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
715 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
717 bool glyphsRemoved( false );
718 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
720 mImpl->mVisualModel->mGlyphPositions.Clear();
721 glyphsRemoved = true;
723 // Not worth to relayout if width or height is equal to zero.
724 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
725 return glyphsRemoved;
728 if( size != mImpl->mVisualModel->mControlSize )
730 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
732 // Operations that need to be done if the size changes.
733 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
739 mImpl->mVisualModel->mControlSize = size;
742 // Make sure the model is up-to-date before layouting
743 ProcessModifyEvents();
744 mImpl->UpdateModel( mImpl->mOperationsPending );
747 bool updated = DoRelayout( mImpl->mVisualModel->mControlSize,
748 mImpl->mOperationsPending,
751 // Do not re-do any operation until something changes.
752 mImpl->mOperationsPending = NO_OPERATION;
754 // Keep the current offset and alignment as it will be used to update the decorator's positions.
756 if( mImpl->mEventData )
758 offset = mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition;
761 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
762 CalculateTextAlignment( size );
764 if( mImpl->mEventData )
766 // If there is a nex size, the scroll position needs to be clamped.
767 mImpl->ClampHorizontalScroll( layoutSize );
769 // Update the decorator's positions.
770 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
772 // Move the cursor, grab handle etc.
773 updated = mImpl->ProcessInputEvents() || updated;
776 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
780 void Controller::ProcessModifyEvents()
782 std::vector<ModifyEvent>& events = mImpl->mModifyEvents;
784 for( unsigned int i=0; i<events.size(); ++i )
786 if( ModifyEvent::TEXT_REPLACED == events[i].type )
788 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
789 DALI_ASSERT_DEBUG( 0 == i && "Unexpected TEXT_REPLACED event" );
793 else if( ModifyEvent::TEXT_INSERTED == events[i].type )
797 else if( ModifyEvent::TEXT_DELETED == events[i].type )
799 // Placeholder-text cannot be deleted
800 if( !mImpl->IsShowingPlaceholderText() )
807 if( mImpl->mEventData &&
810 // When the text is being modified, delay cursor blinking
811 mImpl->mEventData->mDecorator->DelayCursorBlink();
814 // Discard temporary text
818 void Controller::ResetText()
821 mImpl->mLogicalModel->mText.Clear();
824 // We have cleared everything including the placeholder-text
825 mImpl->PlaceholderCleared();
827 // The natural size needs to be re-calculated.
828 mImpl->mRecalculateNaturalSize = true;
830 // Apply modifications to the model
831 mImpl->mOperationsPending = ALL_OPERATIONS;
834 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
836 // Reset the cursor position
837 if( NULL != mImpl->mEventData )
839 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
841 // Update the cursor if it's in editing mode.
842 if( ( EventData::EDITING == mImpl->mEventData->mState ) ||
843 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
844 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
846 mImpl->mEventData->mUpdateCursorPosition = true;
851 void Controller::ResetScrollPosition()
853 if( NULL != mImpl->mEventData )
855 // Reset the scroll position.
856 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
857 mImpl->mEventData->mScrollAfterUpdatePosition = true;
861 void Controller::TextReplacedEvent()
866 // The natural size needs to be re-calculated.
867 mImpl->mRecalculateNaturalSize = true;
869 // Apply modifications to the model
870 mImpl->mOperationsPending = ALL_OPERATIONS;
871 mImpl->UpdateModel( ALL_OPERATIONS );
872 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
878 void Controller::TextInsertedEvent()
880 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
882 // TODO - Optimize this
885 // The natural size needs to be re-calculated.
886 mImpl->mRecalculateNaturalSize = true;
888 // Apply modifications to the model; TODO - Optimize this
889 mImpl->mOperationsPending = ALL_OPERATIONS;
890 mImpl->UpdateModel( ALL_OPERATIONS );
891 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
896 // Queue a cursor reposition event; this must wait until after DoRelayout()
897 if( ( EventData::EDITING == mImpl->mEventData->mState ) ||
898 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
899 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
901 mImpl->mEventData->mUpdateCursorPosition = true;
902 mImpl->mEventData->mScrollAfterUpdatePosition = true;
906 void Controller::TextDeletedEvent()
908 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
910 // TODO - Optimize this
913 // The natural size needs to be re-calculated.
914 mImpl->mRecalculateNaturalSize = true;
916 // Apply modifications to the model; TODO - Optimize this
917 mImpl->mOperationsPending = ALL_OPERATIONS;
918 mImpl->UpdateModel( ALL_OPERATIONS );
919 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
924 // Queue a cursor reposition event; this must wait until after DoRelayout()
925 if( 0u == mImpl->mLogicalModel->mText.Count() )
927 mImpl->mEventData->mUpdateCursorPosition = true;
931 mImpl->mEventData->mScrollAfterDelete = true;
935 bool Controller::DoRelayout( const Size& size,
936 OperationsMask operationsRequired,
939 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
940 bool viewUpdated( false );
942 // Calculate the operations to be done.
943 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
945 if( LAYOUT & operations )
947 // Some vectors with data needed to layout and reorder may be void
948 // after the first time the text has been laid out.
949 // Fill the vectors again.
951 const Length numberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
953 if( 0u == numberOfGlyphs )
955 // Nothing else to do if there is no glyphs.
956 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
960 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
961 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
962 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
963 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
964 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
965 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
966 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
968 // Set the layout parameters.
969 LayoutParameters layoutParameters( size,
971 lineBreakInfo.Begin(),
972 wordBreakInfo.Begin(),
973 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
976 glyphsToCharactersMap.Begin(),
977 charactersPerGlyph.Begin() );
979 // The laid-out lines.
980 // It's not possible to know in how many lines the text is going to be laid-out,
981 // but it can be resized at least with the number of 'paragraphs' to avoid
982 // some re-allocations.
983 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
985 // Delete any previous laid out lines before setting the new ones.
988 // The capacity of the bidirectional paragraph info is the number of paragraphs.
989 lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
991 // Resize the vector of positions to have the same size than the vector of glyphs.
992 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
993 glyphPositions.Resize( numberOfGlyphs );
995 // Whether the last character is a new paragraph character.
996 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
998 // Update the visual model.
999 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1006 // Reorder the lines
1007 if( REORDER & operations )
1009 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1011 // Check first if there are paragraphs with bidirectional info.
1012 if( 0u != bidirectionalInfo.Count() )
1015 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1017 // Reorder the lines.
1018 Vector<BidirectionalLineInfoRun> lineBidirectionalInfoRuns;
1019 lineBidirectionalInfoRuns.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1020 ReorderLines( bidirectionalInfo,
1022 lineBidirectionalInfoRuns );
1024 // Set the bidirectional info into the model.
1025 const Length numberOfBidirectionalInfoRuns = lineBidirectionalInfoRuns.Count();
1026 mImpl->mLogicalModel->SetVisualToLogicalMap( lineBidirectionalInfoRuns.Begin(),
1027 numberOfBidirectionalInfoRuns );
1029 // Set the bidirectional info per line into the layout parameters.
1030 layoutParameters.lineBidirectionalInfoRunsBuffer = lineBidirectionalInfoRuns.Begin();
1031 layoutParameters.numberOfBidirectionalInfoRuns = numberOfBidirectionalInfoRuns;
1033 // Get the character to glyph conversion table and set into the layout.
1034 layoutParameters.charactersToGlyphsBuffer = mImpl->mVisualModel->mCharactersToGlyph.Begin();
1036 // Get the glyphs per character table and set into the layout.
1037 layoutParameters.glyphsPerCharacterBuffer = mImpl->mVisualModel->mGlyphsPerCharacter.Begin();
1039 // Re-layout the text. Reorder those lines with right to left characters.
1040 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1043 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
1044 for( Vector<BidirectionalLineInfoRun>::Iterator it = lineBidirectionalInfoRuns.Begin(),
1045 endIt = lineBidirectionalInfoRuns.End();
1049 BidirectionalLineInfoRun& bidiLineInfo = *it;
1051 free( bidiLineInfo.visualToLogicalMap );
1056 // Sets the actual size.
1057 if( UPDATE_ACTUAL_SIZE & operations )
1059 mImpl->mVisualModel->SetActualSize( layoutSize );
1065 layoutSize = mImpl->mVisualModel->GetActualSize();
1068 if( ALIGN & operations )
1070 // The laid-out lines.
1071 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1073 mImpl->mLayoutEngine.Align( layoutSize,
1079 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1083 void Controller::SetMultiLineEnabled( bool enable )
1085 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1087 if( layout != mImpl->mLayoutEngine.GetLayout() )
1089 // Set the layout type.
1090 mImpl->mLayoutEngine.SetLayout( layout );
1092 // Set the flags to redo the layout operations
1093 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1094 UPDATE_ACTUAL_SIZE |
1098 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1100 mImpl->RequestRelayout();
1104 bool Controller::IsMultiLineEnabled() const
1106 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1109 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1111 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1113 // Set the alignment.
1114 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1116 // Set the flag to redo the alignment operation.
1117 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1119 mImpl->RequestRelayout();
1123 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1125 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1128 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1130 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1132 // Set the alignment.
1133 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1135 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1137 mImpl->RequestRelayout();
1141 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1143 return mImpl->mLayoutEngine.GetVerticalAlignment();
1146 void Controller::CalculateTextAlignment( const Size& size )
1148 // Get the direction of the first character.
1149 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1151 Size actualSize = mImpl->mVisualModel->GetActualSize();
1152 if( fabsf( actualSize.height ) < Math::MACHINE_EPSILON_1000 )
1154 // Get the line height of the default font.
1155 actualSize.height = mImpl->GetDefaultFontLineHeight();
1158 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1159 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1160 if( firstParagraphDirection &&
1161 ( LayoutEngine::HORIZONTAL_ALIGN_CENTER != horizontalAlignment ) )
1163 if( LayoutEngine::HORIZONTAL_ALIGN_BEGIN == horizontalAlignment )
1165 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1169 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1173 switch( horizontalAlignment )
1175 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1177 mImpl->mAlignmentOffset.x = 0.f;
1180 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1182 const int intOffset = static_cast<int>( 0.5f * ( size.width - actualSize.width ) ); // try to avoid pixel alignment.
1183 mImpl->mAlignmentOffset.x = static_cast<float>( intOffset );
1186 case LayoutEngine::HORIZONTAL_ALIGN_END:
1188 mImpl->mAlignmentOffset.x = size.width - actualSize.width;
1193 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1194 switch( verticalAlignment )
1196 case LayoutEngine::VERTICAL_ALIGN_TOP:
1198 mImpl->mAlignmentOffset.y = 0.f;
1201 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1203 const int intOffset = static_cast<int>( 0.5f * ( size.height - actualSize.height ) ); // try to avoid pixel alignment.
1204 mImpl->mAlignmentOffset.y = static_cast<float>( intOffset );
1207 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1209 mImpl->mAlignmentOffset.y = size.height - actualSize.height;
1215 LayoutEngine& Controller::GetLayoutEngine()
1217 return mImpl->mLayoutEngine;
1220 View& Controller::GetView()
1222 return mImpl->mView;
1225 void Controller::KeyboardFocusGainEvent()
1227 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1229 if( mImpl->mEventData )
1231 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1232 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1234 mImpl->ChangeState( EventData::EDITING );
1235 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1238 if( mImpl->IsShowingPlaceholderText() )
1240 // Show alternative placeholder-text when editing
1241 ShowPlaceholderText();
1244 mImpl->RequestRelayout();
1248 void Controller::KeyboardFocusLostEvent()
1250 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1252 if( mImpl->mEventData )
1254 if ( EventData::INTERRUPTED != mImpl->mEventData->mState )
1256 mImpl->ChangeState( EventData::INACTIVE );
1258 if( !mImpl->IsShowingRealText() )
1260 // Revert to regular placeholder-text when not editing
1261 ShowPlaceholderText();
1265 mImpl->RequestRelayout();
1268 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1270 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1272 bool textChanged( false );
1274 if( mImpl->mEventData &&
1275 keyEvent.state == KeyEvent::Down )
1277 int keyCode = keyEvent.keyCode;
1278 const std::string& keyString = keyEvent.keyPressed;
1280 // Pre-process to separate modifying events from non-modifying input events.
1281 if( Dali::DALI_KEY_ESCAPE == keyCode )
1283 // Escape key is a special case which causes focus loss
1284 KeyboardFocusLostEvent();
1286 else if( Dali::DALI_KEY_CURSOR_LEFT == keyCode ||
1287 Dali::DALI_KEY_CURSOR_RIGHT == keyCode ||
1288 Dali::DALI_KEY_CURSOR_UP == keyCode ||
1289 Dali::DALI_KEY_CURSOR_DOWN == keyCode )
1291 Event event( Event::CURSOR_KEY_EVENT );
1292 event.p1.mInt = keyCode;
1293 mImpl->mEventData->mEventQueue.push_back( event );
1295 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1297 textChanged = BackspaceKeyEvent();
1299 else if ( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1301 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1302 // Avoids calling the InsertText() method which can delete selected text
1304 else if ( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1305 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1307 mImpl->ChangeState( EventData::INACTIVE );
1308 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1309 // Avoids calling the InsertText() method which can delete selected text
1311 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1313 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1314 // and a character is typed after the type of a upper case latin character.
1320 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1322 // IMF manager is no longer handling key-events
1323 mImpl->ClearPreEditFlag();
1325 InsertText( keyString, COMMIT );
1329 if ( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1330 ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1332 mImpl->ChangeState( EventData::EDITING );
1335 mImpl->RequestRelayout();
1340 // Do this last since it provides callbacks into application code
1341 mImpl->mControlInterface.TextChanged();
1347 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1349 bool removedPrevious( false );
1350 bool maxLengthReached( false );
1352 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1353 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1354 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1355 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1357 // TODO: At the moment the underline runs are only for pre-edit.
1358 mImpl->mVisualModel->mUnderlineRuns.Clear();
1360 Vector<Character> utf32Characters;
1361 Length characterCount( 0u );
1363 // Remove the previous IMF pre-edit (predicitive text)
1364 if( mImpl->mEventData &&
1365 mImpl->mEventData->mPreEditFlag &&
1366 0 != mImpl->mEventData->mPreEditLength )
1368 CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1370 removedPrevious = RemoveText( -static_cast<int>(offset), mImpl->mEventData->mPreEditLength );
1372 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1373 mImpl->mEventData->mPreEditLength = 0;
1377 // Remove the previous Selection
1378 removedPrevious = RemoveSelectedText();
1383 // Convert text into UTF-32
1384 utf32Characters.Resize( text.size() );
1386 // This is a bit horrible but std::string returns a (signed) char*
1387 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1389 // Transform a text array encoded in utf8 into an array encoded in utf32.
1390 // It returns the actual number of characters.
1391 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1392 utf32Characters.Resize( characterCount );
1394 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1395 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1398 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1400 // The placeholder text is no longer needed
1401 if( mImpl->IsShowingPlaceholderText() )
1406 mImpl->ChangeState( EventData::EDITING );
1408 // Handle the IMF (predicitive text) state changes
1409 if( mImpl->mEventData )
1411 if( COMMIT == type )
1413 // IMF manager is no longer handling key-events
1414 mImpl->ClearPreEditFlag();
1418 if( !mImpl->mEventData->mPreEditFlag )
1420 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1422 // Record the start of the pre-edit text
1423 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1426 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1427 mImpl->mEventData->mPreEditFlag = true;
1429 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1433 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1435 // Restrict new text to fit within Maximum characters setting
1436 Length maxSizeOfNewText = std::min ( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1437 maxLengthReached = ( characterCount > maxSizeOfNewText );
1439 // Insert at current cursor position
1440 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1442 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1444 if( cursorIndex < numberOfCharactersInModel )
1446 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1450 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1453 cursorIndex += maxSizeOfNewText;
1455 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1458 if( 0u == mImpl->mLogicalModel->mText.Count() &&
1459 mImpl->IsPlaceholderAvailable() )
1461 // Show place-holder if empty after removing the pre-edit text
1462 ShowPlaceholderText();
1463 mImpl->mEventData->mUpdateCursorPosition = true;
1464 mImpl->ClearPreEditFlag();
1466 else if( removedPrevious ||
1467 0 != utf32Characters.Count() )
1469 // Queue an inserted event
1470 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1473 if( maxLengthReached )
1475 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1477 mImpl->ResetImfManager();
1479 // Do this last since it provides callbacks into application code
1480 mImpl->mControlInterface.MaxLengthReached();
1484 bool Controller::RemoveSelectedText()
1486 bool textRemoved( false );
1488 if ( EventData::SELECTING == mImpl->mEventData->mState ||
1489 EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
1491 std::string removedString;
1492 mImpl->RetrieveSelection( removedString, true );
1494 if( !removedString.empty() )
1497 mImpl->ChangeState( EventData::EDITING );
1504 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1506 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1508 if( NULL != mImpl->mEventData )
1510 if( 1u == tapCount )
1512 // This is to avoid unnecessary relayouts when tapping an empty text-field
1513 bool relayoutNeeded( false );
1515 if( mImpl->IsShowingRealText() &&
1516 EventData::EDITING == mImpl->mEventData->mState )
1518 // Show grab handle on second tap
1519 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
1520 relayoutNeeded = true;
1522 else if( EventData::EDITING != mImpl->mEventData->mState &&
1523 EventData::EDITING_WITH_GRAB_HANDLE != mImpl->mEventData->mState )
1525 if( mImpl->IsShowingPlaceholderText() && ! mImpl->IsFocusedPlaceholderAvailable() )
1527 // Hide placeholder text
1530 // Show cursor on first tap
1531 mImpl->ChangeState( EventData::EDITING );
1532 relayoutNeeded = true;
1534 else if( mImpl->IsShowingRealText() )
1537 relayoutNeeded = true;
1540 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
1541 if( relayoutNeeded )
1543 Event event( Event::TAP_EVENT );
1544 event.p1.mUint = tapCount;
1545 event.p2.mFloat = x;
1546 event.p3.mFloat = y;
1547 mImpl->mEventData->mEventQueue.push_back( event );
1549 mImpl->RequestRelayout();
1552 else if( 2u == tapCount )
1554 if( mImpl->mEventData->mSelectionEnabled &&
1555 mImpl->IsShowingRealText() )
1557 SelectEvent( x, y, false );
1562 // Reset keyboard as tap event has occurred.
1563 mImpl->ResetImfManager();
1566 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
1568 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1570 if( mImpl->mEventData )
1572 Event event( Event::PAN_EVENT );
1573 event.p1.mInt = state;
1574 event.p2.mFloat = displacement.x;
1575 event.p3.mFloat = displacement.y;
1576 mImpl->mEventData->mEventQueue.push_back( event );
1578 mImpl->RequestRelayout();
1582 void Controller::LongPressEvent( Gesture::State state, float x, float y )
1584 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
1586 if( state == Gesture::Started &&
1589 if( ! mImpl->IsShowingRealText() )
1591 Event event( Event::LONG_PRESS_EVENT );
1592 event.p1.mInt = state;
1593 mImpl->mEventData->mEventQueue.push_back( event );
1594 mImpl->RequestRelayout();
1598 // The 1st long-press on inactive text-field is treated as tap
1599 if( EventData::INACTIVE == mImpl->mEventData->mState )
1601 mImpl->ChangeState( EventData::EDITING );
1603 Event event( Event::TAP_EVENT );
1605 event.p2.mFloat = x;
1606 event.p3.mFloat = y;
1607 mImpl->mEventData->mEventQueue.push_back( event );
1609 mImpl->RequestRelayout();
1613 // Reset the imf manger to commit the pre-edit before selecting the text.
1614 mImpl->ResetImfManager();
1616 SelectEvent( x, y, false );
1622 void Controller::SelectEvent( float x, float y, bool selectAll )
1624 if( mImpl->mEventData )
1626 if ( mImpl->mEventData->mState == EventData::SELECTING )
1628 mImpl->ChangeState( EventData::SELECTION_CHANGED );
1632 mImpl->ChangeState( EventData::SELECTING );
1637 Event event( Event::SELECT_ALL );
1638 mImpl->mEventData->mEventQueue.push_back( event );
1642 Event event( Event::SELECT );
1643 event.p2.mFloat = x;
1644 event.p3.mFloat = y;
1645 mImpl->mEventData->mEventQueue.push_back( event );
1648 mImpl->RequestRelayout();
1652 void Controller::GetTargetSize( Vector2& targetSize )
1654 targetSize = mImpl->mVisualModel->mControlSize;
1657 void Controller::AddDecoration( Actor& actor, bool needsClipping )
1659 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
1662 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
1664 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
1666 if( mImpl->mEventData )
1668 switch( handleType )
1672 Event event( Event::GRAB_HANDLE_EVENT );
1673 event.p1.mUint = state;
1674 event.p2.mFloat = x;
1675 event.p3.mFloat = y;
1677 mImpl->mEventData->mEventQueue.push_back( event );
1680 case LEFT_SELECTION_HANDLE:
1682 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
1683 event.p1.mUint = state;
1684 event.p2.mFloat = x;
1685 event.p3.mFloat = y;
1687 mImpl->mEventData->mEventQueue.push_back( event );
1690 case RIGHT_SELECTION_HANDLE:
1692 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
1693 event.p1.mUint = state;
1694 event.p2.mFloat = x;
1695 event.p3.mFloat = y;
1697 mImpl->mEventData->mEventQueue.push_back( event );
1700 case LEFT_SELECTION_HANDLE_MARKER:
1701 case RIGHT_SELECTION_HANDLE_MARKER:
1703 // Markers do not move the handles.
1706 case HANDLE_TYPE_COUNT:
1708 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
1712 mImpl->RequestRelayout();
1716 void Controller::PasteText( const std::string& stringToPaste )
1718 InsertText( stringToPaste, Text::Controller::COMMIT );
1719 mImpl->ChangeState( EventData::EDITING );
1720 mImpl->RequestRelayout();
1723 void Controller::PasteClipboardItemEvent()
1725 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
1726 std::string stringToPaste( notifier.GetContent() );
1727 PasteText( stringToPaste );
1730 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
1732 if( NULL == mImpl->mEventData )
1739 case Toolkit::TextSelectionPopup::CUT:
1741 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
1742 mImpl->mOperationsPending = ALL_OPERATIONS;
1744 // This is to reset the virtual keyboard to Upper-case
1745 if( 0u == mImpl->mLogicalModel->mText.Count() )
1750 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1751 !mImpl->IsPlaceholderAvailable() )
1753 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1757 ShowPlaceholderText();
1758 mImpl->mEventData->mUpdateCursorPosition = true;
1760 mImpl->RequestRelayout();
1761 mImpl->mControlInterface.TextChanged();
1764 case Toolkit::TextSelectionPopup::COPY:
1766 mImpl->SendSelectionToClipboard( false ); // Text not modified
1767 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
1770 case Toolkit::TextSelectionPopup::PASTE:
1772 std::string stringToPaste("");
1773 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
1774 PasteText( stringToPaste );
1777 case Toolkit::TextSelectionPopup::SELECT:
1779 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
1781 if( mImpl->mEventData->mSelectionEnabled )
1783 // Creates a SELECT event.
1784 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
1788 case Toolkit::TextSelectionPopup::SELECT_ALL:
1790 // Creates a SELECT_ALL event
1791 SelectEvent( 0.f, 0.f, true );
1794 case Toolkit::TextSelectionPopup::CLIPBOARD:
1796 mImpl->ShowClipboard();
1799 case Toolkit::TextSelectionPopup::NONE:
1807 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
1809 bool update( false );
1810 bool requestRelayout = false;
1813 unsigned int cursorPosition( 0 );
1815 switch ( imfEvent.eventName )
1817 case ImfManager::COMMIT:
1819 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
1821 requestRelayout = true;
1824 case ImfManager::PREEDIT:
1826 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
1828 requestRelayout = true;
1831 case ImfManager::DELETESURROUNDING:
1833 update = RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
1837 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1838 !mImpl->IsPlaceholderAvailable() )
1840 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1844 ShowPlaceholderText();
1845 mImpl->mEventData->mUpdateCursorPosition = true;
1848 requestRelayout = true;
1851 case ImfManager::GETSURROUNDING:
1854 cursorPosition = GetLogicalCursorPosition();
1856 imfManager.SetSurroundingText( text );
1857 imfManager.SetCursorPosition( cursorPosition );
1860 case ImfManager::VOID:
1867 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
1870 cursorPosition = GetLogicalCursorPosition();
1873 if( requestRelayout )
1875 mImpl->mOperationsPending = ALL_OPERATIONS;
1876 mImpl->RequestRelayout();
1878 // Do this last since it provides callbacks into application code
1879 mImpl->mControlInterface.TextChanged();
1882 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
1884 return callbackData;
1887 Controller::~Controller()
1892 bool Controller::BackspaceKeyEvent()
1894 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
1896 // IMF manager is no longer handling key-events
1897 mImpl->ClearPreEditFlag();
1899 bool removed( false );
1901 if ( EventData::SELECTING == mImpl->mEventData->mState ||
1902 EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
1904 removed = RemoveSelectedText();
1906 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
1908 // Remove the character before the current cursor position
1909 removed = RemoveText( -1, 1 );
1914 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE RemovedText\n", this );
1915 // Notifiy the IMF manager after text changed
1916 // Automatic Upper-case and restarting prediction on an existing word require this.
1919 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1920 !mImpl->IsPlaceholderAvailable() )
1922 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1926 ShowPlaceholderText();
1927 mImpl->mEventData->mUpdateCursorPosition = true;
1934 void Controller::NotifyImfManager()
1936 if( mImpl->mEventData )
1938 ImfManager imfManager = ImfManager::Get();
1942 // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
1945 imfManager.SetSurroundingText( text );
1947 imfManager.SetCursorPosition( GetLogicalCursorPosition() );
1948 imfManager.NotifyCursorPosition();
1953 void Controller::ShowPlaceholderText()
1955 if( mImpl->IsPlaceholderAvailable() )
1957 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
1959 mImpl->mEventData->mIsShowingPlaceholderText = true;
1961 // Disable handles when showing place-holder text
1962 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
1963 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
1964 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
1966 const char* text( NULL );
1969 // TODO - Switch placeholder text styles when changing state
1970 if( EventData::INACTIVE != mImpl->mEventData->mState &&
1971 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() )
1973 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
1974 size = mImpl->mEventData->mPlaceholderTextActive.size();
1978 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
1979 size = mImpl->mEventData->mPlaceholderTextInactive.size();
1982 // Reset model for showing placeholder.
1983 mImpl->mLogicalModel->mText.Clear();
1985 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
1987 // Convert text into UTF-32
1988 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
1989 utf32Characters.Resize( size );
1991 // This is a bit horrible but std::string returns a (signed) char*
1992 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
1994 // Transform a text array encoded in utf8 into an array encoded in utf32.
1995 // It returns the actual number of characters.
1996 Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
1997 utf32Characters.Resize( characterCount );
1999 // Reset the cursor position
2000 mImpl->mEventData->mPrimaryCursorPosition = 0;
2002 // The natural size needs to be re-calculated.
2003 mImpl->mRecalculateNaturalSize = true;
2005 // Apply modifications to the model
2006 mImpl->mOperationsPending = ALL_OPERATIONS;
2008 // Update the rest of the model during size negotiation
2009 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2013 void Controller::ClearModelData()
2015 // n.b. This does not Clear the mText from mLogicalModel
2016 mImpl->mLogicalModel->mScriptRuns.Clear();
2017 mImpl->mLogicalModel->mFontRuns.Clear();
2018 mImpl->mLogicalModel->mLineBreakInfo.Clear();
2019 mImpl->mLogicalModel->mWordBreakInfo.Clear();
2020 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
2021 mImpl->mLogicalModel->mCharacterDirections.Clear();
2022 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
2023 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
2024 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
2025 mImpl->mVisualModel->mGlyphs.Clear();
2026 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2027 mImpl->mVisualModel->mCharactersToGlyph.Clear();
2028 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2029 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2030 mImpl->mVisualModel->mGlyphPositions.Clear();
2031 mImpl->mVisualModel->mLines.Clear();
2032 mImpl->mVisualModel->ClearCaches();
2035 void Controller::ClearFontData()
2037 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2038 mImpl->mLogicalModel->mFontRuns.Clear();
2039 mImpl->mVisualModel->mGlyphs.Clear();
2040 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2041 mImpl->mVisualModel->mCharactersToGlyph.Clear();
2042 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2043 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2044 mImpl->mVisualModel->mGlyphPositions.Clear();
2045 mImpl->mVisualModel->mLines.Clear();
2046 mImpl->mVisualModel->ClearCaches();
2049 Controller::Controller( ControlInterface& controlInterface )
2052 mImpl = new Controller::Impl( controlInterface );
2057 } // namespace Toolkit