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::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
93 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
95 mImpl->ChangeState( EventData::EDITING );
101 // Convert text into UTF-32
102 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
103 utf32Characters.Resize( text.size() );
105 // This is a bit horrible but std::string returns a (signed) char*
106 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
108 // Transform a text array encoded in utf8 into an array encoded in utf32.
109 // It returns the actual number of characters.
110 Length characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
111 utf32Characters.Resize( characterCount );
113 DALI_ASSERT_DEBUG( text.size() >= characterCount && "Invalid UTF32 conversion length" );
114 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, text.size(), mImpl->mLogicalModel->mText.Count() );
116 // To reset the cursor position
117 lastCursorIndex = characterCount;
119 // Update the rest of the model during size negotiation
120 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
122 // The natural size needs to be re-calculated.
123 mImpl->mRecalculateNaturalSize = true;
125 // Apply modifications to the model
126 mImpl->mOperationsPending = ALL_OPERATIONS;
130 ShowPlaceholderText();
133 // Resets the cursor position.
134 ResetCursorPosition( lastCursorIndex );
136 // Scrolls the text to make the cursor visible.
137 ResetScrollPosition();
139 mImpl->RequestRelayout();
141 if( mImpl->mEventData )
143 // Cancel previously queued events
144 mImpl->mEventData->mEventQueue.clear();
147 // Notify IMF as text changed
150 // Do this last since it provides callbacks into application code
151 mImpl->mControlInterface.TextChanged();
154 void Controller::GetText( std::string& text ) const
156 if( ! mImpl->IsShowingPlaceholderText() )
158 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
160 if( 0u != utf32Characters.Count() )
162 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
167 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
171 unsigned int Controller::GetLogicalCursorPosition() const
173 if( mImpl->mEventData )
175 return mImpl->mEventData->mPrimaryCursorPosition;
181 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
183 if( mImpl->mEventData )
185 if( PLACEHOLDER_TYPE_INACTIVE == type )
187 mImpl->mEventData->mPlaceholderTextInactive = text;
191 mImpl->mEventData->mPlaceholderTextActive = text;
194 // Update placeholder if there is no text
195 if( mImpl->IsShowingPlaceholderText() ||
196 0u == mImpl->mLogicalModel->mText.Count() )
198 ShowPlaceholderText();
203 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
205 if( mImpl->mEventData )
207 if( PLACEHOLDER_TYPE_INACTIVE == type )
209 text = mImpl->mEventData->mPlaceholderTextInactive;
213 text = mImpl->mEventData->mPlaceholderTextActive;
218 void Controller::SetMaximumNumberOfCharacters( int maxCharacters )
220 if ( maxCharacters >= 0 )
222 mImpl->mMaximumNumberOfCharacters = maxCharacters;
226 int Controller::GetMaximumNumberOfCharacters()
228 return mImpl->mMaximumNumberOfCharacters;
231 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
233 if( !mImpl->mFontDefaults )
235 mImpl->mFontDefaults = new FontDefaults();
238 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
239 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
240 mImpl->mUserDefinedFontFamily = true;
242 // Clear the font-specific data
245 mImpl->mOperationsPending = ALL_OPERATIONS;
246 mImpl->mRecalculateNaturalSize = true;
248 mImpl->RequestRelayout();
251 const std::string& Controller::GetDefaultFontFamily() const
253 if( mImpl->mFontDefaults )
255 return mImpl->mFontDefaults->mFontDescription.family;
261 void Controller::SetDefaultFontStyle( const std::string& style )
263 if( !mImpl->mFontDefaults )
265 mImpl->mFontDefaults = new FontDefaults();
268 mImpl->mFontDefaults->mFontStyle = style;
271 const std::string& Controller::GetDefaultFontStyle() const
273 if( mImpl->mFontDefaults )
275 return mImpl->mFontDefaults->mFontStyle;
281 void Controller::SetDefaultFontWidth( FontWidth width )
283 if( !mImpl->mFontDefaults )
285 mImpl->mFontDefaults = new FontDefaults();
288 mImpl->mFontDefaults->mFontDescription.width = width;
290 // Clear the font-specific data
293 mImpl->mOperationsPending = ALL_OPERATIONS;
294 mImpl->mRecalculateNaturalSize = true;
296 mImpl->RequestRelayout();
299 FontWidth Controller::GetDefaultFontWidth() const
301 if( mImpl->mFontDefaults )
303 return mImpl->mFontDefaults->mFontDescription.width;
306 return TextAbstraction::FontWidth::NORMAL;
309 void Controller::SetDefaultFontWeight( FontWeight weight )
311 if( !mImpl->mFontDefaults )
313 mImpl->mFontDefaults = new FontDefaults();
316 mImpl->mFontDefaults->mFontDescription.weight = weight;
318 // Clear the font-specific data
321 mImpl->mOperationsPending = ALL_OPERATIONS;
322 mImpl->mRecalculateNaturalSize = true;
324 mImpl->RequestRelayout();
327 FontWeight Controller::GetDefaultFontWeight() const
329 if( mImpl->mFontDefaults )
331 return mImpl->mFontDefaults->mFontDescription.weight;
334 return TextAbstraction::FontWeight::NORMAL;
337 void Controller::SetDefaultFontSlant( FontSlant slant )
339 if( !mImpl->mFontDefaults )
341 mImpl->mFontDefaults = new FontDefaults();
344 mImpl->mFontDefaults->mFontDescription.slant = slant;
346 // Clear the font-specific data
349 mImpl->mOperationsPending = ALL_OPERATIONS;
350 mImpl->mRecalculateNaturalSize = true;
352 mImpl->RequestRelayout();
355 FontSlant Controller::GetDefaultFontSlant() const
357 if( mImpl->mFontDefaults )
359 return mImpl->mFontDefaults->mFontDescription.slant;
362 return TextAbstraction::FontSlant::NORMAL;
365 void Controller::SetDefaultPointSize( float pointSize )
367 if( !mImpl->mFontDefaults )
369 mImpl->mFontDefaults = new FontDefaults();
372 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
374 unsigned int horizontalDpi( 0u );
375 unsigned int verticalDpi( 0u );
376 mImpl->mFontClient.GetDpi( horizontalDpi, verticalDpi );
378 // Adjust the metrics if the fixed-size font should be down-scaled
379 int maxEmojiSize( pointSize/POINTS_PER_INCH * verticalDpi );
380 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultPointSize %p setting MaxEmojiSize %d\n", this, maxEmojiSize );
381 mImpl->mMetrics->SetMaxEmojiSize( maxEmojiSize );
383 // Clear the font-specific data
386 mImpl->mOperationsPending = ALL_OPERATIONS;
387 mImpl->mRecalculateNaturalSize = true;
389 mImpl->RequestRelayout();
392 float Controller::GetDefaultPointSize() const
394 if( mImpl->mFontDefaults )
396 return mImpl->mFontDefaults->mDefaultPointSize;
402 void Controller::UpdateAfterFontChange( std::string& newDefaultFont )
404 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange");
406 if ( !mImpl->mUserDefinedFontFamily ) // If user defined font then should not update when system font changes
408 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
410 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
411 mImpl->UpdateModel( ALL_OPERATIONS );
412 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
413 mImpl->mRecalculateNaturalSize = true;
414 mImpl->RequestRelayout();
418 void Controller::SetTextColor( const Vector4& textColor )
420 mImpl->mTextColor = textColor;
422 if( !mImpl->IsShowingPlaceholderText() )
424 mImpl->mVisualModel->SetTextColor( textColor );
426 mImpl->RequestRelayout();
430 const Vector4& Controller::GetTextColor() const
432 return mImpl->mTextColor;
435 bool Controller::RemoveText( int cursorOffset, int numberOfChars )
437 bool removed( false );
439 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfChars %d\n",
440 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfChars );
442 if( !mImpl->IsShowingPlaceholderText() )
444 // Delete at current cursor position
445 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
446 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
448 CharacterIndex cursorIndex = oldCursorIndex;
450 // Validate the cursor position & number of characters
451 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
453 cursorIndex = oldCursorIndex + cursorOffset;
456 if( (cursorIndex + numberOfChars) > currentText.Count() )
458 numberOfChars = currentText.Count() - cursorIndex;
461 if( (cursorIndex + numberOfChars) <= currentText.Count() )
463 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
464 Vector<Character>::Iterator last = first + numberOfChars;
466 currentText.Erase( first, last );
468 // Cursor position retreat
469 oldCursorIndex = cursorIndex;
471 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfChars );
479 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
481 if( mImpl->mEventData )
483 mImpl->mEventData->mPlaceholderTextColor = textColor;
486 if( mImpl->IsShowingPlaceholderText() )
488 mImpl->mVisualModel->SetTextColor( textColor );
489 mImpl->RequestRelayout();
493 const Vector4& Controller::GetPlaceholderTextColor() const
495 if( mImpl->mEventData )
497 return mImpl->mEventData->mPlaceholderTextColor;
503 void Controller::SetShadowOffset( const Vector2& shadowOffset )
505 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
507 mImpl->RequestRelayout();
510 const Vector2& Controller::GetShadowOffset() const
512 return mImpl->mVisualModel->GetShadowOffset();
515 void Controller::SetShadowColor( const Vector4& shadowColor )
517 mImpl->mVisualModel->SetShadowColor( shadowColor );
519 mImpl->RequestRelayout();
522 const Vector4& Controller::GetShadowColor() const
524 return mImpl->mVisualModel->GetShadowColor();
527 void Controller::SetUnderlineColor( const Vector4& color )
529 mImpl->mVisualModel->SetUnderlineColor( color );
531 mImpl->RequestRelayout();
534 const Vector4& Controller::GetUnderlineColor() const
536 return mImpl->mVisualModel->GetUnderlineColor();
539 void Controller::SetUnderlineEnabled( bool enabled )
541 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
543 mImpl->RequestRelayout();
546 bool Controller::IsUnderlineEnabled() const
548 return mImpl->mVisualModel->IsUnderlineEnabled();
551 void Controller::SetUnderlineHeight( float height )
553 mImpl->mVisualModel->SetUnderlineHeight( height );
555 mImpl->RequestRelayout();
558 float Controller::GetUnderlineHeight() const
560 return mImpl->mVisualModel->GetUnderlineHeight();
563 void Controller::SetEnableCursorBlink( bool enable )
565 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
567 if( mImpl->mEventData )
569 mImpl->mEventData->mCursorBlinkEnabled = enable;
572 mImpl->mEventData->mDecorator )
574 mImpl->mEventData->mDecorator->StopCursorBlink();
579 bool Controller::GetEnableCursorBlink() const
581 if( mImpl->mEventData )
583 return mImpl->mEventData->mCursorBlinkEnabled;
589 const Vector2& Controller::GetScrollPosition() const
591 if( mImpl->mEventData )
593 return mImpl->mEventData->mScrollPosition;
596 return Vector2::ZERO;
599 const Vector2& Controller::GetAlignmentOffset() const
601 return mImpl->mAlignmentOffset;
604 Vector3 Controller::GetNaturalSize()
606 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
609 // Make sure the model is up-to-date before layouting
610 ProcessModifyEvents();
612 if( mImpl->mRecalculateNaturalSize )
614 // Operations that can be done only once until the text changes.
615 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
623 // Make sure the model is up-to-date before layouting
624 mImpl->UpdateModel( onlyOnceOperations );
626 // Operations that need to be done if the size changes.
627 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
631 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
632 static_cast<OperationsMask>( onlyOnceOperations |
634 naturalSize.GetVectorXY() );
636 // Do not do again the only once operations.
637 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
639 // Do the size related operations again.
640 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
642 // Stores the natural size to avoid recalculate it again
643 // unless the text/style changes.
644 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
646 mImpl->mRecalculateNaturalSize = false;
648 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
652 naturalSize = mImpl->mVisualModel->GetNaturalSize();
654 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
657 naturalSize.x = ConvertToEven( naturalSize.x );
658 naturalSize.y = ConvertToEven( naturalSize.y );
663 float Controller::GetHeightForWidth( float width )
665 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
666 // Make sure the model is up-to-date before layouting
667 ProcessModifyEvents();
670 if( width != mImpl->mVisualModel->mControlSize.width )
672 // Operations that can be done only once until the text changes.
673 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
681 // Make sure the model is up-to-date before layouting
682 mImpl->UpdateModel( onlyOnceOperations );
684 // Operations that need to be done if the size changes.
685 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
689 DoRelayout( Size( width, MAX_FLOAT ),
690 static_cast<OperationsMask>( onlyOnceOperations |
694 // Do not do again the only once operations.
695 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
697 // Do the size related operations again.
698 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
699 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
703 layoutSize = mImpl->mVisualModel->GetActualSize();
704 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
707 return layoutSize.height;
710 bool Controller::Relayout( const Size& size )
712 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
714 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
716 bool glyphsRemoved( false );
717 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
719 mImpl->mVisualModel->mGlyphPositions.Clear();
720 glyphsRemoved = true;
722 // Not worth to relayout if width or height is equal to zero.
723 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
724 return glyphsRemoved;
727 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
731 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
733 // Operations that need to be done if the size changes.
734 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
740 mImpl->mVisualModel->mControlSize = size;
743 // Make sure the model is up-to-date before layouting
744 ProcessModifyEvents();
745 mImpl->UpdateModel( mImpl->mOperationsPending );
748 bool updated = DoRelayout( mImpl->mVisualModel->mControlSize,
749 mImpl->mOperationsPending,
752 // Do not re-do any operation until something changes.
753 mImpl->mOperationsPending = NO_OPERATION;
755 // Keep the current offset and alignment as it will be used to update the decorator's positions (if the size changes).
757 if( newSize && mImpl->mEventData )
759 offset = mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition;
762 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
763 CalculateTextAlignment( size );
765 if( mImpl->mEventData )
769 // If there is a new size, the scroll position needs to be clamped.
770 mImpl->ClampHorizontalScroll( layoutSize );
772 // Update the decorator's positions is needed if there is a new size.
773 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
776 // Move the cursor, grab handle etc.
777 updated = mImpl->ProcessInputEvents() || updated;
780 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
784 void Controller::ProcessModifyEvents()
786 std::vector<ModifyEvent>& events = mImpl->mModifyEvents;
788 for( unsigned int i=0; i<events.size(); ++i )
790 if( ModifyEvent::TEXT_REPLACED == events[i].type )
792 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
793 DALI_ASSERT_DEBUG( 0 == i && "Unexpected TEXT_REPLACED event" );
797 else if( ModifyEvent::TEXT_INSERTED == events[i].type )
801 else if( ModifyEvent::TEXT_DELETED == events[i].type )
803 // Placeholder-text cannot be deleted
804 if( !mImpl->IsShowingPlaceholderText() )
811 if( mImpl->mEventData &&
814 // When the text is being modified, delay cursor blinking
815 mImpl->mEventData->mDecorator->DelayCursorBlink();
818 // Discard temporary text
822 void Controller::ResetText()
825 mImpl->mLogicalModel->mText.Clear();
828 // We have cleared everything including the placeholder-text
829 mImpl->PlaceholderCleared();
831 // The natural size needs to be re-calculated.
832 mImpl->mRecalculateNaturalSize = true;
834 // Apply modifications to the model
835 mImpl->mOperationsPending = ALL_OPERATIONS;
838 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
840 // Reset the cursor position
841 if( NULL != mImpl->mEventData )
843 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
845 // Update the cursor if it's in editing mode.
846 if( ( EventData::EDITING == mImpl->mEventData->mState ) ||
847 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
848 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
850 mImpl->mEventData->mUpdateCursorPosition = true;
855 void Controller::ResetScrollPosition()
857 if( NULL != mImpl->mEventData )
859 // Reset the scroll position.
860 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
861 mImpl->mEventData->mScrollAfterUpdatePosition = true;
865 void Controller::TextReplacedEvent()
870 // The natural size needs to be re-calculated.
871 mImpl->mRecalculateNaturalSize = true;
873 // Apply modifications to the model
874 mImpl->mOperationsPending = ALL_OPERATIONS;
875 mImpl->UpdateModel( ALL_OPERATIONS );
876 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
882 void Controller::TextInsertedEvent()
884 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
886 // TODO - Optimize this
889 // The natural size needs to be re-calculated.
890 mImpl->mRecalculateNaturalSize = true;
892 // Apply modifications to the model; TODO - Optimize this
893 mImpl->mOperationsPending = ALL_OPERATIONS;
894 mImpl->UpdateModel( ALL_OPERATIONS );
895 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
900 // Queue a cursor reposition event; this must wait until after DoRelayout()
901 if( ( EventData::EDITING == mImpl->mEventData->mState ) ||
902 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
903 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
905 mImpl->mEventData->mUpdateCursorPosition = true;
906 mImpl->mEventData->mScrollAfterUpdatePosition = true;
910 void Controller::TextDeletedEvent()
912 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
914 // TODO - Optimize this
917 // The natural size needs to be re-calculated.
918 mImpl->mRecalculateNaturalSize = true;
920 // Apply modifications to the model; TODO - Optimize this
921 mImpl->mOperationsPending = ALL_OPERATIONS;
922 mImpl->UpdateModel( ALL_OPERATIONS );
923 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
928 // Queue a cursor reposition event; this must wait until after DoRelayout()
929 mImpl->mEventData->mUpdateCursorPosition = true;
930 if( 0u != mImpl->mLogicalModel->mText.Count() )
932 mImpl->mEventData->mScrollAfterDelete = true;
936 bool Controller::DoRelayout( const Size& size,
937 OperationsMask operationsRequired,
940 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
941 bool viewUpdated( false );
943 // Calculate the operations to be done.
944 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
946 if( LAYOUT & operations )
948 // Some vectors with data needed to layout and reorder may be void
949 // after the first time the text has been laid out.
950 // Fill the vectors again.
952 const Length numberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
954 if( 0u == numberOfGlyphs )
956 // Nothing else to do if there is no glyphs.
957 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
961 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
962 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
963 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
964 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
965 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
966 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
967 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
969 // Set the layout parameters.
970 LayoutParameters layoutParameters( size,
972 lineBreakInfo.Begin(),
973 wordBreakInfo.Begin(),
974 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
977 glyphsToCharactersMap.Begin(),
978 charactersPerGlyph.Begin() );
980 // The laid-out lines.
981 // It's not possible to know in how many lines the text is going to be laid-out,
982 // but it can be resized at least with the number of 'paragraphs' to avoid
983 // some re-allocations.
984 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
986 // Delete any previous laid out lines before setting the new ones.
989 // The capacity of the bidirectional paragraph info is the number of paragraphs.
990 lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
992 // Resize the vector of positions to have the same size than the vector of glyphs.
993 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
994 glyphPositions.Resize( numberOfGlyphs );
996 // Whether the last character is a new paragraph character.
997 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
999 // Update the visual model.
1000 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1007 // Reorder the lines
1008 if( REORDER & operations )
1010 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1012 // Check first if there are paragraphs with bidirectional info.
1013 if( 0u != bidirectionalInfo.Count() )
1016 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1018 // Reorder the lines.
1019 Vector<BidirectionalLineInfoRun> lineBidirectionalInfoRuns;
1020 lineBidirectionalInfoRuns.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1021 ReorderLines( bidirectionalInfo,
1023 lineBidirectionalInfoRuns );
1025 // Set the bidirectional info into the model.
1026 const Length numberOfBidirectionalInfoRuns = lineBidirectionalInfoRuns.Count();
1027 mImpl->mLogicalModel->SetVisualToLogicalMap( lineBidirectionalInfoRuns.Begin(),
1028 numberOfBidirectionalInfoRuns );
1030 // Set the bidirectional info per line into the layout parameters.
1031 layoutParameters.lineBidirectionalInfoRunsBuffer = lineBidirectionalInfoRuns.Begin();
1032 layoutParameters.numberOfBidirectionalInfoRuns = numberOfBidirectionalInfoRuns;
1034 // Get the character to glyph conversion table and set into the layout.
1035 layoutParameters.charactersToGlyphsBuffer = mImpl->mVisualModel->mCharactersToGlyph.Begin();
1037 // Get the glyphs per character table and set into the layout.
1038 layoutParameters.glyphsPerCharacterBuffer = mImpl->mVisualModel->mGlyphsPerCharacter.Begin();
1040 // Re-layout the text. Reorder those lines with right to left characters.
1041 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1044 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
1045 for( Vector<BidirectionalLineInfoRun>::Iterator it = lineBidirectionalInfoRuns.Begin(),
1046 endIt = lineBidirectionalInfoRuns.End();
1050 BidirectionalLineInfoRun& bidiLineInfo = *it;
1052 free( bidiLineInfo.visualToLogicalMap );
1057 // Sets the actual size.
1058 if( UPDATE_ACTUAL_SIZE & operations )
1060 mImpl->mVisualModel->SetActualSize( layoutSize );
1066 layoutSize = mImpl->mVisualModel->GetActualSize();
1069 if( ALIGN & operations )
1071 // The laid-out lines.
1072 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1074 mImpl->mLayoutEngine.Align( layoutSize,
1080 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1084 void Controller::SetMultiLineEnabled( bool enable )
1086 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1088 if( layout != mImpl->mLayoutEngine.GetLayout() )
1090 // Set the layout type.
1091 mImpl->mLayoutEngine.SetLayout( layout );
1093 // Set the flags to redo the layout operations
1094 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1095 UPDATE_ACTUAL_SIZE |
1099 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1101 mImpl->RequestRelayout();
1105 bool Controller::IsMultiLineEnabled() const
1107 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1110 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1112 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1114 // Set the alignment.
1115 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1117 // Set the flag to redo the alignment operation.
1118 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1120 mImpl->RequestRelayout();
1124 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1126 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1129 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1131 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1133 // Set the alignment.
1134 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1136 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1138 mImpl->RequestRelayout();
1142 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1144 return mImpl->mLayoutEngine.GetVerticalAlignment();
1147 void Controller::CalculateTextAlignment( const Size& size )
1149 // Get the direction of the first character.
1150 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1152 Size actualSize = mImpl->mVisualModel->GetActualSize();
1153 if( fabsf( actualSize.height ) < Math::MACHINE_EPSILON_1000 )
1155 // Get the line height of the default font.
1156 actualSize.height = mImpl->GetDefaultFontLineHeight();
1159 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1160 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1161 if( firstParagraphDirection &&
1162 ( LayoutEngine::HORIZONTAL_ALIGN_CENTER != horizontalAlignment ) )
1164 if( LayoutEngine::HORIZONTAL_ALIGN_BEGIN == horizontalAlignment )
1166 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1170 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1174 switch( horizontalAlignment )
1176 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1178 mImpl->mAlignmentOffset.x = 0.f;
1181 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1183 mImpl->mAlignmentOffset.x = floorf( 0.5f * ( size.width - actualSize.width ) ); // try to avoid pixel alignment.
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 mImpl->mAlignmentOffset.y = floorf( 0.5f * ( size.height - actualSize.height ) ); // try to avoid pixel alignment.
1206 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1208 mImpl->mAlignmentOffset.y = size.height - actualSize.height;
1214 LayoutEngine& Controller::GetLayoutEngine()
1216 return mImpl->mLayoutEngine;
1219 View& Controller::GetView()
1221 return mImpl->mView;
1224 void Controller::KeyboardFocusGainEvent()
1226 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1228 if( mImpl->mEventData )
1230 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1231 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1233 mImpl->ChangeState( EventData::EDITING );
1234 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1237 if( mImpl->IsShowingPlaceholderText() )
1239 // Show alternative placeholder-text when editing
1240 ShowPlaceholderText();
1243 mImpl->RequestRelayout();
1247 void Controller::KeyboardFocusLostEvent()
1249 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1251 if( mImpl->mEventData )
1253 if ( EventData::INTERRUPTED != mImpl->mEventData->mState )
1255 mImpl->ChangeState( EventData::INACTIVE );
1257 if( !mImpl->IsShowingRealText() )
1259 // Revert to regular placeholder-text when not editing
1260 ShowPlaceholderText();
1264 mImpl->RequestRelayout();
1267 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1269 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1271 bool textChanged( false );
1273 if( mImpl->mEventData &&
1274 keyEvent.state == KeyEvent::Down )
1276 int keyCode = keyEvent.keyCode;
1277 const std::string& keyString = keyEvent.keyPressed;
1279 // Pre-process to separate modifying events from non-modifying input events.
1280 if( Dali::DALI_KEY_ESCAPE == keyCode )
1282 // Escape key is a special case which causes focus loss
1283 KeyboardFocusLostEvent();
1285 else if( Dali::DALI_KEY_CURSOR_LEFT == keyCode ||
1286 Dali::DALI_KEY_CURSOR_RIGHT == keyCode ||
1287 Dali::DALI_KEY_CURSOR_UP == keyCode ||
1288 Dali::DALI_KEY_CURSOR_DOWN == keyCode )
1290 Event event( Event::CURSOR_KEY_EVENT );
1291 event.p1.mInt = keyCode;
1292 mImpl->mEventData->mEventQueue.push_back( event );
1294 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1296 textChanged = BackspaceKeyEvent();
1298 else if ( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1300 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1301 // Avoids calling the InsertText() method which can delete selected text
1303 else if ( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1304 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1306 mImpl->ChangeState( EventData::INACTIVE );
1307 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1308 // Avoids calling the InsertText() method which can delete selected text
1310 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1312 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1313 // and a character is typed after the type of a upper case latin character.
1319 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1321 // IMF manager is no longer handling key-events
1322 mImpl->ClearPreEditFlag();
1324 InsertText( keyString, COMMIT );
1328 if ( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1329 ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1331 mImpl->ChangeState( EventData::EDITING );
1334 mImpl->RequestRelayout();
1339 // Do this last since it provides callbacks into application code
1340 mImpl->mControlInterface.TextChanged();
1346 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1348 bool removedPrevious( false );
1349 bool maxLengthReached( false );
1351 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1352 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1353 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1354 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1356 // TODO: At the moment the underline runs are only for pre-edit.
1357 mImpl->mVisualModel->mUnderlineRuns.Clear();
1359 Vector<Character> utf32Characters;
1360 Length characterCount( 0u );
1362 // Remove the previous IMF pre-edit (predicitive text)
1363 if( mImpl->mEventData &&
1364 mImpl->mEventData->mPreEditFlag &&
1365 0 != mImpl->mEventData->mPreEditLength )
1367 CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1369 removedPrevious = RemoveText( -static_cast<int>(offset), mImpl->mEventData->mPreEditLength );
1371 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1372 mImpl->mEventData->mPreEditLength = 0;
1376 // Remove the previous Selection
1377 removedPrevious = RemoveSelectedText();
1382 // Convert text into UTF-32
1383 utf32Characters.Resize( text.size() );
1385 // This is a bit horrible but std::string returns a (signed) char*
1386 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1388 // Transform a text array encoded in utf8 into an array encoded in utf32.
1389 // It returns the actual number of characters.
1390 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1391 utf32Characters.Resize( characterCount );
1393 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1394 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1397 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1399 // The placeholder text is no longer needed
1400 if( mImpl->IsShowingPlaceholderText() )
1405 mImpl->ChangeState( EventData::EDITING );
1407 // Handle the IMF (predicitive text) state changes
1408 if( mImpl->mEventData )
1410 if( COMMIT == type )
1412 // IMF manager is no longer handling key-events
1413 mImpl->ClearPreEditFlag();
1417 if( !mImpl->mEventData->mPreEditFlag )
1419 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1421 // Record the start of the pre-edit text
1422 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1425 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1426 mImpl->mEventData->mPreEditFlag = true;
1428 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1432 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1434 // Restrict new text to fit within Maximum characters setting
1435 Length maxSizeOfNewText = std::min ( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1436 maxLengthReached = ( characterCount > maxSizeOfNewText );
1438 // Insert at current cursor position
1439 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1441 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1443 if( cursorIndex < numberOfCharactersInModel )
1445 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1449 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1452 cursorIndex += maxSizeOfNewText;
1454 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1457 if( 0u == mImpl->mLogicalModel->mText.Count() &&
1458 mImpl->IsPlaceholderAvailable() )
1460 // Show place-holder if empty after removing the pre-edit text
1461 ShowPlaceholderText();
1462 mImpl->mEventData->mUpdateCursorPosition = true;
1463 mImpl->ClearPreEditFlag();
1465 else if( removedPrevious ||
1466 0 != utf32Characters.Count() )
1468 // Queue an inserted event
1469 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1472 if( maxLengthReached )
1474 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1476 mImpl->ResetImfManager();
1478 // Do this last since it provides callbacks into application code
1479 mImpl->mControlInterface.MaxLengthReached();
1483 bool Controller::RemoveSelectedText()
1485 bool textRemoved( false );
1487 if( EventData::SELECTING == mImpl->mEventData->mState )
1489 std::string removedString;
1490 mImpl->RetrieveSelection( removedString, true );
1492 if( !removedString.empty() )
1495 mImpl->ChangeState( EventData::EDITING );
1502 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1504 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1506 if( NULL != mImpl->mEventData )
1508 if( 1u == tapCount )
1510 // This is to avoid unnecessary relayouts when tapping an empty text-field
1511 bool relayoutNeeded( false );
1513 if( mImpl->IsShowingRealText() &&
1514 EventData::EDITING == mImpl->mEventData->mState )
1516 // Show grab handle on second tap
1517 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
1518 relayoutNeeded = true;
1520 else if( EventData::EDITING != mImpl->mEventData->mState &&
1521 EventData::EDITING_WITH_GRAB_HANDLE != mImpl->mEventData->mState )
1523 if( mImpl->IsShowingPlaceholderText() && ! mImpl->IsFocusedPlaceholderAvailable() )
1525 // Hide placeholder text
1528 // Show cursor on first tap
1529 mImpl->ChangeState( EventData::EDITING );
1530 relayoutNeeded = true;
1532 else if( mImpl->IsShowingRealText() )
1535 relayoutNeeded = true;
1538 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
1539 if( relayoutNeeded )
1541 Event event( Event::TAP_EVENT );
1542 event.p1.mUint = tapCount;
1543 event.p2.mFloat = x;
1544 event.p3.mFloat = y;
1545 mImpl->mEventData->mEventQueue.push_back( event );
1547 mImpl->RequestRelayout();
1550 else if( 2u == tapCount )
1552 if( mImpl->mEventData->mSelectionEnabled &&
1553 mImpl->IsShowingRealText() )
1555 SelectEvent( x, y, false );
1560 // Reset keyboard as tap event has occurred.
1561 mImpl->ResetImfManager();
1564 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
1566 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1568 if( mImpl->mEventData )
1570 Event event( Event::PAN_EVENT );
1571 event.p1.mInt = state;
1572 event.p2.mFloat = displacement.x;
1573 event.p3.mFloat = displacement.y;
1574 mImpl->mEventData->mEventQueue.push_back( event );
1576 mImpl->RequestRelayout();
1580 void Controller::LongPressEvent( Gesture::State state, float x, float y )
1582 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
1584 if( state == Gesture::Started &&
1587 if( ! mImpl->IsShowingRealText() )
1589 Event event( Event::LONG_PRESS_EVENT );
1590 event.p1.mInt = state;
1591 mImpl->mEventData->mEventQueue.push_back( event );
1592 mImpl->RequestRelayout();
1596 // The 1st long-press on inactive text-field is treated as tap
1597 if( EventData::INACTIVE == mImpl->mEventData->mState )
1599 mImpl->ChangeState( EventData::EDITING );
1601 Event event( Event::TAP_EVENT );
1603 event.p2.mFloat = x;
1604 event.p3.mFloat = y;
1605 mImpl->mEventData->mEventQueue.push_back( event );
1607 mImpl->RequestRelayout();
1611 // Reset the imf manger to commit the pre-edit before selecting the text.
1612 mImpl->ResetImfManager();
1614 SelectEvent( x, y, false );
1620 void Controller::SelectEvent( float x, float y, bool selectAll )
1622 if( mImpl->mEventData )
1624 mImpl->ChangeState( EventData::SELECTING );
1628 Event event( Event::SELECT_ALL );
1629 mImpl->mEventData->mEventQueue.push_back( event );
1633 Event event( Event::SELECT );
1634 event.p2.mFloat = x;
1635 event.p3.mFloat = y;
1636 mImpl->mEventData->mEventQueue.push_back( event );
1639 mImpl->RequestRelayout();
1643 void Controller::GetTargetSize( Vector2& targetSize )
1645 targetSize = mImpl->mVisualModel->mControlSize;
1648 void Controller::AddDecoration( Actor& actor, bool needsClipping )
1650 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
1653 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
1655 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
1657 if( mImpl->mEventData )
1659 switch( handleType )
1663 Event event( Event::GRAB_HANDLE_EVENT );
1664 event.p1.mUint = state;
1665 event.p2.mFloat = x;
1666 event.p3.mFloat = y;
1668 mImpl->mEventData->mEventQueue.push_back( event );
1671 case LEFT_SELECTION_HANDLE:
1673 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
1674 event.p1.mUint = state;
1675 event.p2.mFloat = x;
1676 event.p3.mFloat = y;
1678 mImpl->mEventData->mEventQueue.push_back( event );
1681 case RIGHT_SELECTION_HANDLE:
1683 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
1684 event.p1.mUint = state;
1685 event.p2.mFloat = x;
1686 event.p3.mFloat = y;
1688 mImpl->mEventData->mEventQueue.push_back( event );
1691 case LEFT_SELECTION_HANDLE_MARKER:
1692 case RIGHT_SELECTION_HANDLE_MARKER:
1694 // Markers do not move the handles.
1697 case HANDLE_TYPE_COUNT:
1699 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
1703 mImpl->RequestRelayout();
1707 void Controller::PasteText( const std::string& stringToPaste )
1709 InsertText( stringToPaste, Text::Controller::COMMIT );
1710 mImpl->ChangeState( EventData::EDITING );
1711 mImpl->RequestRelayout();
1713 // Do this last since it provides callbacks into application code
1714 mImpl->mControlInterface.TextChanged();
1717 void Controller::PasteClipboardItemEvent()
1719 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
1720 std::string stringToPaste( notifier.GetContent() );
1721 PasteText( stringToPaste );
1724 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
1726 if( NULL == mImpl->mEventData )
1733 case Toolkit::TextSelectionPopup::CUT:
1735 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
1736 mImpl->mOperationsPending = ALL_OPERATIONS;
1738 // This is to reset the virtual keyboard to Upper-case
1739 if( 0u == mImpl->mLogicalModel->mText.Count() )
1744 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1745 !mImpl->IsPlaceholderAvailable() )
1747 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1751 ShowPlaceholderText();
1752 mImpl->mEventData->mUpdateCursorPosition = true;
1754 mImpl->RequestRelayout();
1755 mImpl->mControlInterface.TextChanged();
1758 case Toolkit::TextSelectionPopup::COPY:
1760 mImpl->SendSelectionToClipboard( false ); // Text not modified
1761 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
1764 case Toolkit::TextSelectionPopup::PASTE:
1766 std::string stringToPaste("");
1767 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
1768 PasteText( stringToPaste );
1771 case Toolkit::TextSelectionPopup::SELECT:
1773 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
1775 if( mImpl->mEventData->mSelectionEnabled )
1777 // Creates a SELECT event.
1778 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
1782 case Toolkit::TextSelectionPopup::SELECT_ALL:
1784 // Creates a SELECT_ALL event
1785 SelectEvent( 0.f, 0.f, true );
1788 case Toolkit::TextSelectionPopup::CLIPBOARD:
1790 mImpl->ShowClipboard();
1793 case Toolkit::TextSelectionPopup::NONE:
1801 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
1803 bool update( false );
1804 bool requestRelayout = false;
1807 unsigned int cursorPosition( 0 );
1809 switch ( imfEvent.eventName )
1811 case ImfManager::COMMIT:
1813 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
1815 requestRelayout = true;
1818 case ImfManager::PREEDIT:
1820 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
1822 requestRelayout = true;
1825 case ImfManager::DELETESURROUNDING:
1827 update = RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
1831 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1832 !mImpl->IsPlaceholderAvailable() )
1834 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1838 ShowPlaceholderText();
1839 mImpl->mEventData->mUpdateCursorPosition = true;
1842 requestRelayout = true;
1845 case ImfManager::GETSURROUNDING:
1848 cursorPosition = GetLogicalCursorPosition();
1850 imfManager.SetSurroundingText( text );
1851 imfManager.SetCursorPosition( cursorPosition );
1854 case ImfManager::VOID:
1861 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
1864 cursorPosition = GetLogicalCursorPosition();
1867 if( requestRelayout )
1869 mImpl->mOperationsPending = ALL_OPERATIONS;
1870 mImpl->RequestRelayout();
1872 // Do this last since it provides callbacks into application code
1873 mImpl->mControlInterface.TextChanged();
1876 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
1878 return callbackData;
1881 Controller::~Controller()
1886 bool Controller::BackspaceKeyEvent()
1888 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
1890 // IMF manager is no longer handling key-events
1891 mImpl->ClearPreEditFlag();
1893 bool removed( false );
1895 if( EventData::SELECTING == mImpl->mEventData->mState )
1897 removed = RemoveSelectedText();
1899 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
1901 // Remove the character before the current cursor position
1902 removed = RemoveText( -1, 1 );
1907 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE RemovedText\n", this );
1908 // Notifiy the IMF manager after text changed
1909 // Automatic Upper-case and restarting prediction on an existing word require this.
1912 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1913 !mImpl->IsPlaceholderAvailable() )
1915 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1919 ShowPlaceholderText();
1920 mImpl->mEventData->mUpdateCursorPosition = true;
1927 void Controller::NotifyImfManager()
1929 if( mImpl->mEventData )
1931 ImfManager imfManager = ImfManager::Get();
1935 // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
1938 imfManager.SetSurroundingText( text );
1940 imfManager.SetCursorPosition( GetLogicalCursorPosition() );
1941 imfManager.NotifyCursorPosition();
1946 void Controller::ShowPlaceholderText()
1948 if( mImpl->IsPlaceholderAvailable() )
1950 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
1952 mImpl->mEventData->mIsShowingPlaceholderText = true;
1954 // Disable handles when showing place-holder text
1955 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
1956 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
1957 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
1959 const char* text( NULL );
1962 // TODO - Switch placeholder text styles when changing state
1963 if( EventData::INACTIVE != mImpl->mEventData->mState &&
1964 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() )
1966 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
1967 size = mImpl->mEventData->mPlaceholderTextActive.size();
1971 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
1972 size = mImpl->mEventData->mPlaceholderTextInactive.size();
1975 // Reset model for showing placeholder.
1976 mImpl->mLogicalModel->mText.Clear();
1978 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
1980 // Convert text into UTF-32
1981 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
1982 utf32Characters.Resize( size );
1984 // This is a bit horrible but std::string returns a (signed) char*
1985 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
1987 // Transform a text array encoded in utf8 into an array encoded in utf32.
1988 // It returns the actual number of characters.
1989 Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
1990 utf32Characters.Resize( characterCount );
1992 // Reset the cursor position
1993 mImpl->mEventData->mPrimaryCursorPosition = 0;
1995 // The natural size needs to be re-calculated.
1996 mImpl->mRecalculateNaturalSize = true;
1998 // Apply modifications to the model
1999 mImpl->mOperationsPending = ALL_OPERATIONS;
2001 // Update the rest of the model during size negotiation
2002 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2006 void Controller::ClearModelData()
2008 // n.b. This does not Clear the mText from mLogicalModel
2009 mImpl->mLogicalModel->mScriptRuns.Clear();
2010 mImpl->mLogicalModel->mFontRuns.Clear();
2011 mImpl->mLogicalModel->mLineBreakInfo.Clear();
2012 mImpl->mLogicalModel->mWordBreakInfo.Clear();
2013 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
2014 mImpl->mLogicalModel->mCharacterDirections.Clear();
2015 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
2016 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
2017 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
2018 mImpl->mVisualModel->mGlyphs.Clear();
2019 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2020 mImpl->mVisualModel->mCharactersToGlyph.Clear();
2021 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2022 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2023 mImpl->mVisualModel->mGlyphPositions.Clear();
2024 mImpl->mVisualModel->mLines.Clear();
2025 mImpl->mVisualModel->ClearCaches();
2028 void Controller::ClearFontData()
2030 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2031 mImpl->mLogicalModel->mFontRuns.Clear();
2032 mImpl->mVisualModel->mGlyphs.Clear();
2033 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2034 mImpl->mVisualModel->mCharactersToGlyph.Clear();
2035 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2036 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2037 mImpl->mVisualModel->mGlyphPositions.Clear();
2038 mImpl->mVisualModel->mLines.Clear();
2039 mImpl->mVisualModel->ClearCaches();
2042 Controller::Controller( ControlInterface& controlInterface )
2045 mImpl = new Controller::Impl( controlInterface );
2050 } // namespace Toolkit