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");
409 if ( !mImpl->mUserDefinedFontFamily ) // If user defined font then should not update when system font changes
411 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
412 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
413 mImpl->UpdateModel( ALL_OPERATIONS );
414 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
415 mImpl->mRecalculateNaturalSize = true;
416 mImpl->RequestRelayout();
420 void Controller::SetTextColor( const Vector4& textColor )
422 mImpl->mTextColor = textColor;
424 if( !mImpl->IsShowingPlaceholderText() )
426 mImpl->mVisualModel->SetTextColor( textColor );
428 mImpl->RequestRelayout();
432 const Vector4& Controller::GetTextColor() const
434 return mImpl->mTextColor;
437 bool Controller::RemoveText( int cursorOffset, int numberOfChars )
439 bool removed( false );
441 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfChars %d\n",
442 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfChars );
444 if( !mImpl->IsShowingPlaceholderText() )
446 // Delete at current cursor position
447 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
448 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
450 CharacterIndex cursorIndex = oldCursorIndex;
452 // Validate the cursor position & number of characters
453 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
455 cursorIndex = oldCursorIndex + cursorOffset;
458 if( (cursorIndex + numberOfChars) > currentText.Count() )
460 numberOfChars = currentText.Count() - cursorIndex;
463 if( (cursorIndex + numberOfChars) <= currentText.Count() )
465 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
466 Vector<Character>::Iterator last = first + numberOfChars;
468 currentText.Erase( first, last );
470 // Cursor position retreat
471 oldCursorIndex = cursorIndex;
473 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfChars );
481 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
483 if( mImpl->mEventData )
485 mImpl->mEventData->mPlaceholderTextColor = textColor;
488 if( mImpl->IsShowingPlaceholderText() )
490 mImpl->mVisualModel->SetTextColor( textColor );
491 mImpl->RequestRelayout();
495 const Vector4& Controller::GetPlaceholderTextColor() const
497 if( mImpl->mEventData )
499 return mImpl->mEventData->mPlaceholderTextColor;
505 void Controller::SetShadowOffset( const Vector2& shadowOffset )
507 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
509 mImpl->RequestRelayout();
512 const Vector2& Controller::GetShadowOffset() const
514 return mImpl->mVisualModel->GetShadowOffset();
517 void Controller::SetShadowColor( const Vector4& shadowColor )
519 mImpl->mVisualModel->SetShadowColor( shadowColor );
521 mImpl->RequestRelayout();
524 const Vector4& Controller::GetShadowColor() const
526 return mImpl->mVisualModel->GetShadowColor();
529 void Controller::SetUnderlineColor( const Vector4& color )
531 mImpl->mVisualModel->SetUnderlineColor( color );
533 mImpl->RequestRelayout();
536 const Vector4& Controller::GetUnderlineColor() const
538 return mImpl->mVisualModel->GetUnderlineColor();
541 void Controller::SetUnderlineEnabled( bool enabled )
543 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
545 mImpl->RequestRelayout();
548 bool Controller::IsUnderlineEnabled() const
550 return mImpl->mVisualModel->IsUnderlineEnabled();
553 void Controller::SetUnderlineHeight( float height )
555 mImpl->mVisualModel->SetUnderlineHeight( height );
557 mImpl->RequestRelayout();
560 float Controller::GetUnderlineHeight() const
562 return mImpl->mVisualModel->GetUnderlineHeight();
565 void Controller::SetEnableCursorBlink( bool enable )
567 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
569 if( mImpl->mEventData )
571 mImpl->mEventData->mCursorBlinkEnabled = enable;
574 mImpl->mEventData->mDecorator )
576 mImpl->mEventData->mDecorator->StopCursorBlink();
581 bool Controller::GetEnableCursorBlink() const
583 if( mImpl->mEventData )
585 return mImpl->mEventData->mCursorBlinkEnabled;
591 const Vector2& Controller::GetScrollPosition() const
593 if( mImpl->mEventData )
595 return mImpl->mEventData->mScrollPosition;
598 return Vector2::ZERO;
601 const Vector2& Controller::GetAlignmentOffset() const
603 return mImpl->mAlignmentOffset;
606 Vector3 Controller::GetNaturalSize()
608 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
611 // Make sure the model is up-to-date before layouting
612 ProcessModifyEvents();
614 if( mImpl->mRecalculateNaturalSize )
616 // Operations that can be done only once until the text changes.
617 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
625 // Make sure the model is up-to-date before layouting
626 mImpl->UpdateModel( onlyOnceOperations );
628 // Operations that need to be done if the size changes.
629 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
633 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
634 static_cast<OperationsMask>( onlyOnceOperations |
636 naturalSize.GetVectorXY() );
638 // Do not do again the only once operations.
639 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
641 // Do the size related operations again.
642 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
644 // Stores the natural size to avoid recalculate it again
645 // unless the text/style changes.
646 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
648 mImpl->mRecalculateNaturalSize = false;
650 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
654 naturalSize = mImpl->mVisualModel->GetNaturalSize();
656 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
659 naturalSize.x = ConvertToEven( naturalSize.x );
660 naturalSize.y = ConvertToEven( naturalSize.y );
665 float Controller::GetHeightForWidth( float width )
667 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
668 // Make sure the model is up-to-date before layouting
669 ProcessModifyEvents();
672 if( width != mImpl->mVisualModel->mControlSize.width )
674 // Operations that can be done only once until the text changes.
675 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
683 // Make sure the model is up-to-date before layouting
684 mImpl->UpdateModel( onlyOnceOperations );
686 // Operations that need to be done if the size changes.
687 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
691 DoRelayout( Size( width, MAX_FLOAT ),
692 static_cast<OperationsMask>( onlyOnceOperations |
696 // Do not do again the only once operations.
697 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
699 // Do the size related operations again.
700 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
701 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
705 layoutSize = mImpl->mVisualModel->GetActualSize();
706 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
709 return layoutSize.height;
712 bool Controller::Relayout( const Size& size )
714 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
716 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
718 bool glyphsRemoved( false );
719 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
721 mImpl->mVisualModel->mGlyphPositions.Clear();
722 glyphsRemoved = true;
724 // Not worth to relayout if width or height is equal to zero.
725 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
726 return glyphsRemoved;
729 if( 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.
757 if( 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 )
767 // If there is a nex size, the scroll position needs to be clamped.
768 mImpl->ClampHorizontalScroll( layoutSize );
770 // Update the decorator's positions.
771 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
773 // Move the cursor, grab handle etc.
774 updated = mImpl->ProcessInputEvents() || updated;
777 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
781 void Controller::ProcessModifyEvents()
783 std::vector<ModifyEvent>& events = mImpl->mModifyEvents;
785 for( unsigned int i=0; i<events.size(); ++i )
787 if( ModifyEvent::TEXT_REPLACED == events[0].type )
789 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
790 DALI_ASSERT_DEBUG( 0 == i && "Unexpected TEXT_REPLACED event" );
794 else if( ModifyEvent::TEXT_INSERTED == events[0].type )
798 else if( ModifyEvent::TEXT_DELETED == events[0].type )
800 // Placeholder-text cannot be deleted
801 if( !mImpl->IsShowingPlaceholderText() )
808 if( mImpl->mEventData &&
811 // When the text is being modified, delay cursor blinking
812 mImpl->mEventData->mDecorator->DelayCursorBlink();
815 // Discard temporary text
819 void Controller::ResetText()
822 mImpl->mLogicalModel->mText.Clear();
825 // We have cleared everything including the placeholder-text
826 mImpl->PlaceholderCleared();
828 // The natural size needs to be re-calculated.
829 mImpl->mRecalculateNaturalSize = true;
831 // Apply modifications to the model
832 mImpl->mOperationsPending = ALL_OPERATIONS;
835 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
837 // Reset the cursor position
838 if( NULL != mImpl->mEventData )
840 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
842 // Update the cursor if it's in editing mode.
843 if( ( EventData::EDITING == mImpl->mEventData->mState ) ||
844 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
845 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
847 mImpl->mEventData->mUpdateCursorPosition = true;
852 void Controller::ResetScrollPosition()
854 if( NULL != mImpl->mEventData )
856 // Reset the scroll position.
857 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
858 mImpl->mEventData->mScrollAfterUpdatePosition = true;
862 void Controller::TextReplacedEvent()
867 // The natural size needs to be re-calculated.
868 mImpl->mRecalculateNaturalSize = true;
870 // Apply modifications to the model
871 mImpl->mOperationsPending = ALL_OPERATIONS;
872 mImpl->UpdateModel( ALL_OPERATIONS );
873 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
879 void Controller::TextInsertedEvent()
881 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
883 // TODO - Optimize this
886 // The natural size needs to be re-calculated.
887 mImpl->mRecalculateNaturalSize = true;
889 // Apply modifications to the model; TODO - Optimize this
890 mImpl->mOperationsPending = ALL_OPERATIONS;
891 mImpl->UpdateModel( ALL_OPERATIONS );
892 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
897 // Queue a cursor reposition event; this must wait until after DoRelayout()
898 if( ( EventData::EDITING == mImpl->mEventData->mState ) ||
899 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
900 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
902 mImpl->mEventData->mUpdateCursorPosition = true;
903 mImpl->mEventData->mScrollAfterUpdatePosition = true;
907 void Controller::TextDeletedEvent()
909 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
911 // TODO - Optimize this
914 // The natural size needs to be re-calculated.
915 mImpl->mRecalculateNaturalSize = true;
917 // Apply modifications to the model; TODO - Optimize this
918 mImpl->mOperationsPending = ALL_OPERATIONS;
919 mImpl->UpdateModel( ALL_OPERATIONS );
920 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
925 // Queue a cursor reposition event; this must wait until after DoRelayout()
926 if( 0u == mImpl->mLogicalModel->mText.Count() )
928 mImpl->mEventData->mUpdateCursorPosition = true;
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 const int intOffset = static_cast<int>( 0.5f * ( size.width - actualSize.width ) ); // try to avoid pixel alignment.
1184 mImpl->mAlignmentOffset.x = static_cast<float>( intOffset );
1187 case LayoutEngine::HORIZONTAL_ALIGN_END:
1189 mImpl->mAlignmentOffset.x = size.width - actualSize.width;
1194 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1195 switch( verticalAlignment )
1197 case LayoutEngine::VERTICAL_ALIGN_TOP:
1199 mImpl->mAlignmentOffset.y = 0.f;
1202 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1204 const int intOffset = static_cast<int>( 0.5f * ( size.height - actualSize.height ) ); // try to avoid pixel alignment.
1205 mImpl->mAlignmentOffset.y = static_cast<float>( intOffset );
1208 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1210 mImpl->mAlignmentOffset.y = size.height - actualSize.height;
1216 LayoutEngine& Controller::GetLayoutEngine()
1218 return mImpl->mLayoutEngine;
1221 View& Controller::GetView()
1223 return mImpl->mView;
1226 void Controller::KeyboardFocusGainEvent()
1228 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1230 if( mImpl->mEventData )
1232 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1233 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1235 mImpl->ChangeState( EventData::EDITING );
1236 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1239 if( mImpl->IsShowingPlaceholderText() )
1241 // Show alternative placeholder-text when editing
1242 ShowPlaceholderText();
1245 mImpl->RequestRelayout();
1249 void Controller::KeyboardFocusLostEvent()
1251 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1253 if( mImpl->mEventData )
1255 if ( EventData::INTERRUPTED != mImpl->mEventData->mState )
1257 mImpl->ChangeState( EventData::INACTIVE );
1259 if( mImpl->IsShowingPlaceholderText() )
1261 // Revert to regular placeholder-text when not editing
1262 ShowPlaceholderText();
1266 mImpl->RequestRelayout();
1269 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1271 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1273 bool textChanged( false );
1275 if( mImpl->mEventData &&
1276 keyEvent.state == KeyEvent::Down )
1278 int keyCode = keyEvent.keyCode;
1279 const std::string& keyString = keyEvent.keyPressed;
1281 // Pre-process to separate modifying events from non-modifying input events.
1282 if( Dali::DALI_KEY_ESCAPE == keyCode )
1284 // Escape key is a special case which causes focus loss
1285 KeyboardFocusLostEvent();
1287 else if( Dali::DALI_KEY_CURSOR_LEFT == keyCode ||
1288 Dali::DALI_KEY_CURSOR_RIGHT == keyCode ||
1289 Dali::DALI_KEY_CURSOR_UP == keyCode ||
1290 Dali::DALI_KEY_CURSOR_DOWN == keyCode )
1292 Event event( Event::CURSOR_KEY_EVENT );
1293 event.p1.mInt = keyCode;
1294 mImpl->mEventData->mEventQueue.push_back( event );
1296 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1298 textChanged = BackspaceKeyEvent();
1300 else if ( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1302 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1303 // Avoids calling the InsertText() method which can delete selected text
1305 else if ( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1306 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1308 mImpl->ChangeState( EventData::INACTIVE );
1309 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1310 // Avoids calling the InsertText() method which can delete selected text
1312 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1314 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1315 // and a character is typed after the type of a upper case latin character.
1321 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1323 // IMF manager is no longer handling key-events
1324 mImpl->ClearPreEditFlag();
1326 InsertText( keyString, COMMIT );
1330 if ( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1331 ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1333 mImpl->ChangeState( EventData::EDITING );
1336 mImpl->RequestRelayout();
1341 // Do this last since it provides callbacks into application code
1342 mImpl->mControlInterface.TextChanged();
1348 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1350 bool removedPrevious( false );
1351 bool maxLengthReached( false );
1353 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1354 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1355 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1356 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1358 // TODO: At the moment the underline runs are only for pre-edit.
1359 mImpl->mVisualModel->mUnderlineRuns.Clear();
1361 Vector<Character> utf32Characters;
1362 Length characterCount( 0u );
1364 // Remove the previous IMF pre-edit (predicitive text)
1365 if( mImpl->mEventData &&
1366 mImpl->mEventData->mPreEditFlag &&
1367 0 != mImpl->mEventData->mPreEditLength )
1369 CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1371 removedPrevious = RemoveText( -static_cast<int>(offset), mImpl->mEventData->mPreEditLength );
1373 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1374 mImpl->mEventData->mPreEditLength = 0;
1378 // Remove the previous Selection
1379 removedPrevious = RemoveSelectedText();
1384 // Convert text into UTF-32
1385 utf32Characters.Resize( text.size() );
1387 // This is a bit horrible but std::string returns a (signed) char*
1388 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1390 // Transform a text array encoded in utf8 into an array encoded in utf32.
1391 // It returns the actual number of characters.
1392 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1393 utf32Characters.Resize( characterCount );
1395 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1396 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1399 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1401 // The placeholder text is no longer needed
1402 if( mImpl->IsShowingPlaceholderText() )
1407 mImpl->ChangeState( EventData::EDITING );
1409 // Handle the IMF (predicitive text) state changes
1410 if( mImpl->mEventData )
1412 if( COMMIT == type )
1414 // IMF manager is no longer handling key-events
1415 mImpl->ClearPreEditFlag();
1419 if( !mImpl->mEventData->mPreEditFlag )
1421 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1423 // Record the start of the pre-edit text
1424 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1427 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1428 mImpl->mEventData->mPreEditFlag = true;
1430 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1434 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1436 // Restrict new text to fit within Maximum characters setting
1437 Length maxSizeOfNewText = std::min ( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1438 maxLengthReached = ( characterCount > maxSizeOfNewText );
1440 // Insert at current cursor position
1441 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1443 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1445 if( cursorIndex < numberOfCharactersInModel )
1447 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1451 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1454 cursorIndex += maxSizeOfNewText;
1456 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1459 if( 0u == mImpl->mLogicalModel->mText.Count() &&
1460 mImpl->IsPlaceholderAvailable() )
1462 // Show place-holder if empty after removing the pre-edit text
1463 ShowPlaceholderText();
1464 mImpl->mEventData->mUpdateCursorPosition = true;
1465 mImpl->ClearPreEditFlag();
1467 else if( removedPrevious ||
1468 0 != utf32Characters.Count() )
1470 // Queue an inserted event
1471 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1474 if( maxLengthReached )
1476 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1478 mImpl->ResetImfManager();
1480 // Do this last since it provides callbacks into application code
1481 mImpl->mControlInterface.MaxLengthReached();
1485 bool Controller::RemoveSelectedText()
1487 bool textRemoved( false );
1489 if ( EventData::SELECTING == mImpl->mEventData->mState ||
1490 EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
1492 std::string removedString;
1493 mImpl->RetrieveSelection( removedString, true );
1495 if( !removedString.empty() )
1498 mImpl->ChangeState( EventData::EDITING );
1505 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1507 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1509 if( NULL != mImpl->mEventData )
1511 if( 1u == tapCount )
1513 // This is to avoid unnecessary relayouts when tapping an empty text-field
1514 bool relayoutNeeded( false );
1516 if( mImpl->IsShowingRealText() &&
1517 EventData::EDITING == mImpl->mEventData->mState )
1519 // Show grab handle on second tap
1520 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
1521 relayoutNeeded = true;
1523 else if( EventData::EDITING != mImpl->mEventData->mState &&
1524 EventData::EDITING_WITH_GRAB_HANDLE != mImpl->mEventData->mState )
1526 // Show cursor on first tap
1527 mImpl->ChangeState( EventData::EDITING );
1528 relayoutNeeded = true;
1530 else if( mImpl->IsShowingRealText() )
1533 relayoutNeeded = true;
1536 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
1537 if( relayoutNeeded )
1539 Event event( Event::TAP_EVENT );
1540 event.p1.mUint = tapCount;
1541 event.p2.mFloat = x;
1542 event.p3.mFloat = y;
1543 mImpl->mEventData->mEventQueue.push_back( event );
1545 mImpl->RequestRelayout();
1548 else if( 2u == tapCount )
1550 if( mImpl->mEventData->mSelectionEnabled &&
1551 mImpl->IsShowingRealText() )
1553 SelectEvent( x, y, false );
1558 // Reset keyboard as tap event has occurred.
1559 mImpl->ResetImfManager();
1562 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
1564 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1566 if( mImpl->mEventData )
1568 Event event( Event::PAN_EVENT );
1569 event.p1.mInt = state;
1570 event.p2.mFloat = displacement.x;
1571 event.p3.mFloat = displacement.y;
1572 mImpl->mEventData->mEventQueue.push_back( event );
1574 mImpl->RequestRelayout();
1578 void Controller::LongPressEvent( Gesture::State state, float x, float y )
1580 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
1582 if( state == Gesture::Started &&
1585 if( ! mImpl->IsShowingRealText() )
1587 Event event( Event::LONG_PRESS_EVENT );
1588 event.p1.mInt = state;
1589 mImpl->mEventData->mEventQueue.push_back( event );
1590 mImpl->RequestRelayout();
1594 // The 1st long-press on inactive text-field is treated as tap
1595 if( EventData::INACTIVE == mImpl->mEventData->mState )
1597 mImpl->ChangeState( EventData::EDITING );
1599 Event event( Event::TAP_EVENT );
1601 event.p2.mFloat = x;
1602 event.p3.mFloat = y;
1603 mImpl->mEventData->mEventQueue.push_back( event );
1605 mImpl->RequestRelayout();
1609 SelectEvent( x, y, false );
1615 void Controller::SelectEvent( float x, float y, bool selectAll )
1617 if( mImpl->mEventData )
1619 if ( mImpl->mEventData->mState == EventData::SELECTING )
1621 mImpl->ChangeState( EventData::SELECTION_CHANGED );
1625 mImpl->ChangeState( EventData::SELECTING );
1630 Event event( Event::SELECT_ALL );
1631 mImpl->mEventData->mEventQueue.push_back( event );
1635 Event event( Event::SELECT );
1636 event.p2.mFloat = x;
1637 event.p3.mFloat = y;
1638 mImpl->mEventData->mEventQueue.push_back( event );
1641 mImpl->RequestRelayout();
1645 void Controller::GetTargetSize( Vector2& targetSize )
1647 targetSize = mImpl->mVisualModel->mControlSize;
1650 void Controller::AddDecoration( Actor& actor, bool needsClipping )
1652 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
1655 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
1657 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
1659 if( mImpl->mEventData )
1661 switch( handleType )
1665 Event event( Event::GRAB_HANDLE_EVENT );
1666 event.p1.mUint = state;
1667 event.p2.mFloat = x;
1668 event.p3.mFloat = y;
1670 mImpl->mEventData->mEventQueue.push_back( event );
1673 case LEFT_SELECTION_HANDLE:
1675 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
1676 event.p1.mUint = state;
1677 event.p2.mFloat = x;
1678 event.p3.mFloat = y;
1680 mImpl->mEventData->mEventQueue.push_back( event );
1683 case RIGHT_SELECTION_HANDLE:
1685 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
1686 event.p1.mUint = state;
1687 event.p2.mFloat = x;
1688 event.p3.mFloat = y;
1690 mImpl->mEventData->mEventQueue.push_back( event );
1693 case LEFT_SELECTION_HANDLE_MARKER:
1694 case RIGHT_SELECTION_HANDLE_MARKER:
1696 // Markers do not move the handles.
1699 case HANDLE_TYPE_COUNT:
1701 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
1705 mImpl->RequestRelayout();
1709 void Controller::PasteText( const std::string& stringToPaste )
1711 InsertText( stringToPaste, Text::Controller::COMMIT );
1712 mImpl->ChangeState( EventData::EDITING );
1713 mImpl->RequestRelayout();
1716 void Controller::PasteClipboardItemEvent()
1718 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
1719 std::string stringToPaste( notifier.GetContent() );
1720 PasteText( stringToPaste );
1723 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
1725 if( NULL == mImpl->mEventData )
1732 case Toolkit::TextSelectionPopup::CUT:
1734 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
1735 mImpl->mOperationsPending = ALL_OPERATIONS;
1737 // This is to reset the virtual keyboard to Upper-case
1738 if( 0u == mImpl->mLogicalModel->mText.Count() )
1743 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1744 !mImpl->IsPlaceholderAvailable() )
1746 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1750 ShowPlaceholderText();
1751 mImpl->mEventData->mUpdateCursorPosition = true;
1753 mImpl->RequestRelayout();
1754 mImpl->mControlInterface.TextChanged();
1757 case Toolkit::TextSelectionPopup::COPY:
1759 mImpl->SendSelectionToClipboard( false ); // Text not modified
1760 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
1763 case Toolkit::TextSelectionPopup::PASTE:
1765 std::string stringToPaste("");
1766 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
1767 PasteText( stringToPaste );
1770 case Toolkit::TextSelectionPopup::SELECT:
1772 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
1774 if( mImpl->mEventData->mSelectionEnabled )
1776 // Creates a SELECT event.
1777 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
1781 case Toolkit::TextSelectionPopup::SELECT_ALL:
1783 // Creates a SELECT_ALL event
1784 SelectEvent( 0.f, 0.f, true );
1787 case Toolkit::TextSelectionPopup::CLIPBOARD:
1789 mImpl->ShowClipboard();
1792 case Toolkit::TextSelectionPopup::NONE:
1800 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
1802 bool update( false );
1803 bool requestRelayout = false;
1806 unsigned int cursorPosition( 0 );
1808 switch ( imfEvent.eventName )
1810 case ImfManager::COMMIT:
1812 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
1813 requestRelayout = true;
1816 case ImfManager::PREEDIT:
1818 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
1820 requestRelayout = true;
1823 case ImfManager::DELETESURROUNDING:
1825 update = RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
1829 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1830 !mImpl->IsPlaceholderAvailable() )
1832 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1836 ShowPlaceholderText();
1837 mImpl->mEventData->mUpdateCursorPosition = true;
1840 requestRelayout = true;
1843 case ImfManager::GETSURROUNDING:
1846 cursorPosition = GetLogicalCursorPosition();
1848 imfManager.SetSurroundingText( text );
1849 imfManager.SetCursorPosition( cursorPosition );
1852 case ImfManager::VOID:
1859 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
1862 cursorPosition = GetLogicalCursorPosition();
1865 if( requestRelayout )
1867 mImpl->mOperationsPending = ALL_OPERATIONS;
1868 mImpl->RequestRelayout();
1870 // Do this last since it provides callbacks into application code
1871 mImpl->mControlInterface.TextChanged();
1874 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
1876 return callbackData;
1879 Controller::~Controller()
1884 bool Controller::BackspaceKeyEvent()
1886 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
1888 // IMF manager is no longer handling key-events
1889 mImpl->ClearPreEditFlag();
1891 bool removed( false );
1893 if ( EventData::SELECTING == mImpl->mEventData->mState ||
1894 EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
1896 removed = RemoveSelectedText();
1898 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
1900 // Remove the character before the current cursor position
1901 removed = RemoveText( -1, 1 );
1906 // This is to reset the virtual keyboard to Upper-case
1907 if( 0u == mImpl->mLogicalModel->mText.Count() )
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