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 // Remove the previously set text
83 CharacterIndex lastCursorIndex = 0u;
85 if( mImpl->mEventData )
87 // If popup shown then hide it by switching to Editing state
88 if( ( EventData::SELECTING == mImpl->mEventData->mState ) ||
89 ( EventData::SELECTION_CHANGED == mImpl->mEventData->mState ) ||
90 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
91 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
93 mImpl->ChangeState( EventData::EDITING );
99 // Convert text into UTF-32
100 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
101 utf32Characters.Resize( text.size() );
103 // This is a bit horrible but std::string returns a (signed) char*
104 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
106 // Transform a text array encoded in utf8 into an array encoded in utf32.
107 // It returns the actual number of characters.
108 Length characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
109 utf32Characters.Resize( characterCount );
111 DALI_ASSERT_DEBUG( text.size() >= characterCount && "Invalid UTF32 conversion length" );
112 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, text.size(), mImpl->mLogicalModel->mText.Count() );
114 // To reset the cursor position
115 lastCursorIndex = characterCount;
117 // Update the rest of the model during size negotiation
118 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
120 // The natural size needs to be re-calculated.
121 mImpl->mRecalculateNaturalSize = true;
123 // Apply modifications to the model
124 mImpl->mOperationsPending = ALL_OPERATIONS;
128 ShowPlaceholderText();
131 // Resets the cursor position.
132 ResetCursorPosition( lastCursorIndex );
134 // Scrolls the text to make the cursor visible.
135 ResetScrollPosition();
137 mImpl->RequestRelayout();
139 if( mImpl->mEventData )
141 // Cancel previously queued events
142 mImpl->mEventData->mEventQueue.clear();
145 // Reset keyboard as text changed
146 mImpl->ResetImfManager();
148 // Do this last since it provides callbacks into application code
149 mImpl->mControlInterface.TextChanged();
152 void Controller::GetText( std::string& text ) const
154 if( ! mImpl->IsShowingPlaceholderText() )
156 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
158 if( 0u != utf32Characters.Count() )
160 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
165 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
169 unsigned int Controller::GetLogicalCursorPosition() const
171 if( mImpl->mEventData )
173 return mImpl->mEventData->mPrimaryCursorPosition;
179 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
181 if( mImpl->mEventData )
183 if( PLACEHOLDER_TYPE_INACTIVE == type )
185 mImpl->mEventData->mPlaceholderTextInactive = text;
189 mImpl->mEventData->mPlaceholderTextActive = text;
192 // Update placeholder if there is no text
193 if( mImpl->IsShowingPlaceholderText() ||
194 0u == mImpl->mLogicalModel->mText.Count() )
196 ShowPlaceholderText();
201 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
203 if( mImpl->mEventData )
205 if( PLACEHOLDER_TYPE_INACTIVE == type )
207 text = mImpl->mEventData->mPlaceholderTextInactive;
211 text = mImpl->mEventData->mPlaceholderTextActive;
216 void Controller::SetMaximumNumberOfCharacters( int maxCharacters )
218 if ( maxCharacters >= 0 )
220 mImpl->mMaximumNumberOfCharacters = maxCharacters;
224 int Controller::GetMaximumNumberOfCharacters()
226 return mImpl->mMaximumNumberOfCharacters;
229 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily, bool userDefined )
231 if( !mImpl->mFontDefaults )
233 mImpl->mFontDefaults = new FontDefaults();
236 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
237 mImpl->mUserDefinedFontFamily = userDefined;
239 // Clear the font-specific data
242 mImpl->mOperationsPending = ALL_OPERATIONS;
243 mImpl->mRecalculateNaturalSize = true;
245 mImpl->RequestRelayout();
248 const std::string& Controller::GetDefaultFontFamily() const
250 if( mImpl->mFontDefaults )
252 return mImpl->mFontDefaults->mFontDescription.family;
258 void Controller::SetDefaultFontStyle( const std::string& style )
260 if( !mImpl->mFontDefaults )
262 mImpl->mFontDefaults = new FontDefaults();
265 mImpl->mFontDefaults->mFontStyle = style;
268 const std::string& Controller::GetDefaultFontStyle() const
270 if( mImpl->mFontDefaults )
272 return mImpl->mFontDefaults->mFontStyle;
278 void Controller::SetDefaultFontWidth( FontWidth width )
280 if( !mImpl->mFontDefaults )
282 mImpl->mFontDefaults = new FontDefaults();
285 mImpl->mFontDefaults->mFontDescription.width = width;
287 // Clear the font-specific data
290 mImpl->mOperationsPending = ALL_OPERATIONS;
291 mImpl->mRecalculateNaturalSize = true;
293 mImpl->RequestRelayout();
296 FontWidth Controller::GetDefaultFontWidth() const
298 if( mImpl->mFontDefaults )
300 return mImpl->mFontDefaults->mFontDescription.width;
303 return TextAbstraction::FontWidth::NORMAL;
306 void Controller::SetDefaultFontWeight( FontWeight weight )
308 if( !mImpl->mFontDefaults )
310 mImpl->mFontDefaults = new FontDefaults();
313 mImpl->mFontDefaults->mFontDescription.weight = weight;
315 // Clear the font-specific data
318 mImpl->mOperationsPending = ALL_OPERATIONS;
319 mImpl->mRecalculateNaturalSize = true;
321 mImpl->RequestRelayout();
324 FontWeight Controller::GetDefaultFontWeight() const
326 if( mImpl->mFontDefaults )
328 return mImpl->mFontDefaults->mFontDescription.weight;
331 return TextAbstraction::FontWeight::NORMAL;
334 void Controller::SetDefaultFontSlant( FontSlant slant )
336 if( !mImpl->mFontDefaults )
338 mImpl->mFontDefaults = new FontDefaults();
341 mImpl->mFontDefaults->mFontDescription.slant = slant;
343 // Clear the font-specific data
346 mImpl->mOperationsPending = ALL_OPERATIONS;
347 mImpl->mRecalculateNaturalSize = true;
349 mImpl->RequestRelayout();
352 FontSlant Controller::GetDefaultFontSlant() const
354 if( mImpl->mFontDefaults )
356 return mImpl->mFontDefaults->mFontDescription.slant;
359 return TextAbstraction::FontSlant::NORMAL;
362 void Controller::SetDefaultPointSize( float pointSize )
364 if( !mImpl->mFontDefaults )
366 mImpl->mFontDefaults = new FontDefaults();
369 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
371 unsigned int horizontalDpi( 0u );
372 unsigned int verticalDpi( 0u );
373 mImpl->mFontClient.GetDpi( horizontalDpi, verticalDpi );
375 // Adjust the metrics if the fixed-size font should be down-scaled
376 int maxEmojiSize( pointSize/POINTS_PER_INCH * verticalDpi );
377 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultPointSize %p setting MaxEmojiSize %d\n", this, maxEmojiSize );
378 mImpl->mMetrics->SetMaxEmojiSize( maxEmojiSize );
380 // Clear the font-specific data
383 mImpl->mOperationsPending = ALL_OPERATIONS;
384 mImpl->mRecalculateNaturalSize = true;
386 mImpl->RequestRelayout();
389 float Controller::GetDefaultPointSize() const
391 if( mImpl->mFontDefaults )
393 return mImpl->mFontDefaults->mDefaultPointSize;
399 void Controller::UpdateAfterFontChange( std::string& newDefaultFont )
401 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange");
405 if ( !mImpl->mUserDefinedFontFamily ) // If user defined font then should not update when system font changes
407 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
408 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
409 mImpl->UpdateModel( ALL_OPERATIONS );
410 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
411 mImpl->mRecalculateNaturalSize = true;
412 mImpl->RequestRelayout();
416 void Controller::SetTextColor( const Vector4& textColor )
418 mImpl->mTextColor = textColor;
420 if( !mImpl->IsShowingPlaceholderText() )
422 mImpl->mVisualModel->SetTextColor( textColor );
424 mImpl->RequestRelayout();
428 const Vector4& Controller::GetTextColor() const
430 return mImpl->mTextColor;
433 bool Controller::RemoveText( int cursorOffset, int numberOfChars )
435 bool removed( false );
437 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfChars %d\n",
438 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfChars );
440 if( !mImpl->IsShowingPlaceholderText() )
442 // Delete at current cursor position
443 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
444 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
446 CharacterIndex cursorIndex = oldCursorIndex;
448 // Validate the cursor position & number of characters
449 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
451 cursorIndex = oldCursorIndex + cursorOffset;
454 if( (cursorIndex + numberOfChars) > currentText.Count() )
456 numberOfChars = currentText.Count() - cursorIndex;
459 if( (cursorIndex + numberOfChars) <= currentText.Count() )
461 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
462 Vector<Character>::Iterator last = first + numberOfChars;
464 currentText.Erase( first, last );
466 // Cursor position retreat
467 oldCursorIndex = cursorIndex;
469 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfChars );
477 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
479 if( mImpl->mEventData )
481 mImpl->mEventData->mPlaceholderTextColor = textColor;
484 if( mImpl->IsShowingPlaceholderText() )
486 mImpl->mVisualModel->SetTextColor( textColor );
487 mImpl->RequestRelayout();
491 const Vector4& Controller::GetPlaceholderTextColor() const
493 if( mImpl->mEventData )
495 return mImpl->mEventData->mPlaceholderTextColor;
501 void Controller::SetShadowOffset( const Vector2& shadowOffset )
503 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
505 mImpl->RequestRelayout();
508 const Vector2& Controller::GetShadowOffset() const
510 return mImpl->mVisualModel->GetShadowOffset();
513 void Controller::SetShadowColor( const Vector4& shadowColor )
515 mImpl->mVisualModel->SetShadowColor( shadowColor );
517 mImpl->RequestRelayout();
520 const Vector4& Controller::GetShadowColor() const
522 return mImpl->mVisualModel->GetShadowColor();
525 void Controller::SetUnderlineColor( const Vector4& color )
527 mImpl->mVisualModel->SetUnderlineColor( color );
529 mImpl->RequestRelayout();
532 const Vector4& Controller::GetUnderlineColor() const
534 return mImpl->mVisualModel->GetUnderlineColor();
537 void Controller::SetUnderlineEnabled( bool enabled )
539 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
541 mImpl->RequestRelayout();
544 bool Controller::IsUnderlineEnabled() const
546 return mImpl->mVisualModel->IsUnderlineEnabled();
549 void Controller::SetUnderlineHeight( float height )
551 mImpl->mVisualModel->SetUnderlineHeight( height );
553 mImpl->RequestRelayout();
556 float Controller::GetUnderlineHeight() const
558 return mImpl->mVisualModel->GetUnderlineHeight();
561 void Controller::SetEnableCursorBlink( bool enable )
563 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
565 if( mImpl->mEventData )
567 mImpl->mEventData->mCursorBlinkEnabled = enable;
570 mImpl->mEventData->mDecorator )
572 mImpl->mEventData->mDecorator->StopCursorBlink();
577 bool Controller::GetEnableCursorBlink() const
579 if( mImpl->mEventData )
581 return mImpl->mEventData->mCursorBlinkEnabled;
587 const Vector2& Controller::GetScrollPosition() const
589 if( mImpl->mEventData )
591 return mImpl->mEventData->mScrollPosition;
594 return Vector2::ZERO;
597 const Vector2& Controller::GetAlignmentOffset() const
599 return mImpl->mAlignmentOffset;
602 Vector3 Controller::GetNaturalSize()
604 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
607 // Make sure the model is up-to-date before layouting
608 ProcessModifyEvents();
610 if( mImpl->mRecalculateNaturalSize )
612 // Operations that can be done only once until the text changes.
613 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
621 // Make sure the model is up-to-date before layouting
622 mImpl->UpdateModel( onlyOnceOperations );
624 // Operations that need to be done if the size changes.
625 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
629 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
630 static_cast<OperationsMask>( onlyOnceOperations |
632 naturalSize.GetVectorXY() );
634 // Do not do again the only once operations.
635 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
637 // Do the size related operations again.
638 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
640 // Stores the natural size to avoid recalculate it again
641 // unless the text/style changes.
642 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
644 mImpl->mRecalculateNaturalSize = false;
646 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
650 naturalSize = mImpl->mVisualModel->GetNaturalSize();
652 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
655 naturalSize.x = ConvertToEven( naturalSize.x );
656 naturalSize.y = ConvertToEven( naturalSize.y );
661 float Controller::GetHeightForWidth( float width )
663 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
664 // Make sure the model is up-to-date before layouting
665 ProcessModifyEvents();
668 if( width != mImpl->mVisualModel->mControlSize.width )
670 // Operations that can be done only once until the text changes.
671 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
679 // Make sure the model is up-to-date before layouting
680 mImpl->UpdateModel( onlyOnceOperations );
682 // Operations that need to be done if the size changes.
683 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
687 DoRelayout( Size( width, MAX_FLOAT ),
688 static_cast<OperationsMask>( onlyOnceOperations |
692 // Do not do again the only once operations.
693 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
695 // Do the size related operations again.
696 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
697 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
701 layoutSize = mImpl->mVisualModel->GetActualSize();
702 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
705 return layoutSize.height;
708 bool Controller::Relayout( const Size& size )
710 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
712 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
714 bool glyphsRemoved( false );
715 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
717 mImpl->mVisualModel->mGlyphPositions.Clear();
718 glyphsRemoved = true;
720 // Not worth to relayout if width or height is equal to zero.
721 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
722 return glyphsRemoved;
725 if( size != mImpl->mVisualModel->mControlSize )
727 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
729 // Operations that need to be done if the size changes.
730 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
736 mImpl->mVisualModel->mControlSize = size;
739 // Make sure the model is up-to-date before layouting
740 ProcessModifyEvents();
741 mImpl->UpdateModel( mImpl->mOperationsPending );
744 bool updated = DoRelayout( mImpl->mVisualModel->mControlSize,
745 mImpl->mOperationsPending,
748 // Do not re-do any operation until something changes.
749 mImpl->mOperationsPending = NO_OPERATION;
751 // Keep the current offset and alignment as it will be used to update the decorator's positions.
753 if( mImpl->mEventData )
755 offset = mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition;
758 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
759 CalculateTextAlignment( size );
761 if( mImpl->mEventData )
763 // If there is a nex size, the scroll position needs to be clamped.
764 mImpl->ClampHorizontalScroll( layoutSize );
766 // Update the decorator's positions.
767 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
769 // Move the cursor, grab handle etc.
770 updated = mImpl->ProcessInputEvents() || updated;
773 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
777 void Controller::ProcessModifyEvents()
779 std::vector<ModifyEvent>& events = mImpl->mModifyEvents;
781 for( unsigned int i=0; i<events.size(); ++i )
783 if( ModifyEvent::TEXT_REPLACED == events[0].type )
785 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
786 DALI_ASSERT_DEBUG( 0 == i && "Unexpected TEXT_REPLACED event" );
790 else if( ModifyEvent::TEXT_INSERTED == events[0].type )
794 else if( ModifyEvent::TEXT_DELETED == events[0].type )
796 // Placeholder-text cannot be deleted
797 if( !mImpl->IsShowingPlaceholderText() )
804 if( mImpl->mEventData &&
807 // When the text is being modified, delay cursor blinking
808 mImpl->mEventData->mDecorator->DelayCursorBlink();
811 // Discard temporary text
815 void Controller::ResetText()
818 mImpl->mLogicalModel->mText.Clear();
821 // We have cleared everything including the placeholder-text
822 mImpl->PlaceholderCleared();
824 // The natural size needs to be re-calculated.
825 mImpl->mRecalculateNaturalSize = true;
827 // Apply modifications to the model
828 mImpl->mOperationsPending = ALL_OPERATIONS;
831 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
833 // Reset the cursor position
834 if( NULL != mImpl->mEventData )
836 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
838 // Update the cursor if it's in editing mode.
839 if( ( EventData::EDITING == mImpl->mEventData->mState ) ||
840 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
841 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
843 mImpl->mEventData->mUpdateCursorPosition = true;
848 void Controller::ResetScrollPosition()
850 if( NULL != mImpl->mEventData )
852 // Reset the scroll position.
853 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
854 mImpl->mEventData->mScrollAfterUpdatePosition = true;
858 void Controller::TextReplacedEvent()
863 // The natural size needs to be re-calculated.
864 mImpl->mRecalculateNaturalSize = true;
866 // Apply modifications to the model
867 mImpl->mOperationsPending = ALL_OPERATIONS;
868 mImpl->UpdateModel( ALL_OPERATIONS );
869 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
875 void Controller::TextInsertedEvent()
877 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
879 // TODO - Optimize this
882 // The natural size needs to be re-calculated.
883 mImpl->mRecalculateNaturalSize = true;
885 // Apply modifications to the model; TODO - Optimize this
886 mImpl->mOperationsPending = ALL_OPERATIONS;
887 mImpl->UpdateModel( ALL_OPERATIONS );
888 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
893 // Queue a cursor reposition event; this must wait until after DoRelayout()
894 if( ( EventData::EDITING == mImpl->mEventData->mState ) ||
895 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
896 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
898 mImpl->mEventData->mUpdateCursorPosition = true;
899 mImpl->mEventData->mScrollAfterUpdatePosition = true;
903 void Controller::TextDeletedEvent()
905 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
907 // TODO - Optimize this
910 // The natural size needs to be re-calculated.
911 mImpl->mRecalculateNaturalSize = true;
913 // Apply modifications to the model; TODO - Optimize this
914 mImpl->mOperationsPending = ALL_OPERATIONS;
915 mImpl->UpdateModel( ALL_OPERATIONS );
916 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
921 // Queue a cursor reposition event; this must wait until after DoRelayout()
922 if( 0u == mImpl->mLogicalModel->mText.Count() )
924 mImpl->mEventData->mUpdateCursorPosition = true;
928 mImpl->mEventData->mScrollAfterDelete = true;
932 bool Controller::DoRelayout( const Size& size,
933 OperationsMask operationsRequired,
936 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
937 bool viewUpdated( false );
939 // Calculate the operations to be done.
940 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
942 if( LAYOUT & operations )
944 // Some vectors with data needed to layout and reorder may be void
945 // after the first time the text has been laid out.
946 // Fill the vectors again.
948 const Length numberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
950 if( 0u == numberOfGlyphs )
952 // Nothing else to do if there is no glyphs.
953 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
957 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
958 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
959 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
960 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
961 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
962 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
963 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
965 // Set the layout parameters.
966 LayoutParameters layoutParameters( size,
968 lineBreakInfo.Begin(),
969 wordBreakInfo.Begin(),
970 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
973 glyphsToCharactersMap.Begin(),
974 charactersPerGlyph.Begin() );
976 // The laid-out lines.
977 // It's not possible to know in how many lines the text is going to be laid-out,
978 // but it can be resized at least with the number of 'paragraphs' to avoid
979 // some re-allocations.
980 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
982 // Delete any previous laid out lines before setting the new ones.
985 // The capacity of the bidirectional paragraph info is the number of paragraphs.
986 lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
988 // Resize the vector of positions to have the same size than the vector of glyphs.
989 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
990 glyphPositions.Resize( numberOfGlyphs );
992 // Whether the last character is a new paragraph character.
993 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
995 // Update the visual model.
996 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1003 // Reorder the lines
1004 if( REORDER & operations )
1006 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1008 // Check first if there are paragraphs with bidirectional info.
1009 if( 0u != bidirectionalInfo.Count() )
1012 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1014 // Reorder the lines.
1015 Vector<BidirectionalLineInfoRun> lineBidirectionalInfoRuns;
1016 lineBidirectionalInfoRuns.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1017 ReorderLines( bidirectionalInfo,
1019 lineBidirectionalInfoRuns );
1021 // Set the bidirectional info into the model.
1022 const Length numberOfBidirectionalInfoRuns = lineBidirectionalInfoRuns.Count();
1023 mImpl->mLogicalModel->SetVisualToLogicalMap( lineBidirectionalInfoRuns.Begin(),
1024 numberOfBidirectionalInfoRuns );
1026 // Set the bidirectional info per line into the layout parameters.
1027 layoutParameters.lineBidirectionalInfoRunsBuffer = lineBidirectionalInfoRuns.Begin();
1028 layoutParameters.numberOfBidirectionalInfoRuns = numberOfBidirectionalInfoRuns;
1030 // Get the character to glyph conversion table and set into the layout.
1031 layoutParameters.charactersToGlyphsBuffer = mImpl->mVisualModel->mCharactersToGlyph.Begin();
1033 // Get the glyphs per character table and set into the layout.
1034 layoutParameters.glyphsPerCharacterBuffer = mImpl->mVisualModel->mGlyphsPerCharacter.Begin();
1036 // Re-layout the text. Reorder those lines with right to left characters.
1037 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1040 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
1041 for( Vector<BidirectionalLineInfoRun>::Iterator it = lineBidirectionalInfoRuns.Begin(),
1042 endIt = lineBidirectionalInfoRuns.End();
1046 BidirectionalLineInfoRun& bidiLineInfo = *it;
1048 free( bidiLineInfo.visualToLogicalMap );
1053 // Sets the actual size.
1054 if( UPDATE_ACTUAL_SIZE & operations )
1056 mImpl->mVisualModel->SetActualSize( layoutSize );
1062 layoutSize = mImpl->mVisualModel->GetActualSize();
1065 if( ALIGN & operations )
1067 // The laid-out lines.
1068 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1070 mImpl->mLayoutEngine.Align( layoutSize,
1076 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1080 void Controller::SetMultiLineEnabled( bool enable )
1082 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1084 if( layout != mImpl->mLayoutEngine.GetLayout() )
1086 // Set the layout type.
1087 mImpl->mLayoutEngine.SetLayout( layout );
1089 // Set the flags to redo the layout operations
1090 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1091 UPDATE_ACTUAL_SIZE |
1095 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1097 mImpl->RequestRelayout();
1101 bool Controller::IsMultiLineEnabled() const
1103 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1106 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1108 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1110 // Set the alignment.
1111 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1113 // Set the flag to redo the alignment operation.
1114 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1116 mImpl->RequestRelayout();
1120 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1122 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1125 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1127 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1129 // Set the alignment.
1130 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1132 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1134 mImpl->RequestRelayout();
1138 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1140 return mImpl->mLayoutEngine.GetVerticalAlignment();
1143 void Controller::CalculateTextAlignment( const Size& size )
1145 // Get the direction of the first character.
1146 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1148 Size actualSize = mImpl->mVisualModel->GetActualSize();
1149 if( fabsf( actualSize.height ) < Math::MACHINE_EPSILON_1000 )
1151 // Get the line height of the default font.
1152 actualSize.height = mImpl->GetDefaultFontLineHeight();
1155 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1156 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1157 if( firstParagraphDirection &&
1158 ( LayoutEngine::HORIZONTAL_ALIGN_CENTER != horizontalAlignment ) )
1160 if( LayoutEngine::HORIZONTAL_ALIGN_BEGIN == horizontalAlignment )
1162 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1166 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1170 switch( horizontalAlignment )
1172 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1174 mImpl->mAlignmentOffset.x = 0.f;
1177 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1179 const int intOffset = static_cast<int>( 0.5f * ( size.width - actualSize.width ) ); // try to avoid pixel alignment.
1180 mImpl->mAlignmentOffset.x = static_cast<float>( intOffset );
1183 case LayoutEngine::HORIZONTAL_ALIGN_END:
1185 mImpl->mAlignmentOffset.x = size.width - actualSize.width;
1190 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1191 switch( verticalAlignment )
1193 case LayoutEngine::VERTICAL_ALIGN_TOP:
1195 mImpl->mAlignmentOffset.y = 0.f;
1198 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1200 const int intOffset = static_cast<int>( 0.5f * ( size.height - actualSize.height ) ); // try to avoid pixel alignment.
1201 mImpl->mAlignmentOffset.y = static_cast<float>( intOffset );
1204 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1206 mImpl->mAlignmentOffset.y = size.height - actualSize.height;
1212 LayoutEngine& Controller::GetLayoutEngine()
1214 return mImpl->mLayoutEngine;
1217 View& Controller::GetView()
1219 return mImpl->mView;
1222 void Controller::KeyboardFocusGainEvent()
1224 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1226 if( mImpl->mEventData )
1228 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1229 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1231 mImpl->ChangeState( EventData::EDITING );
1232 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1235 if( mImpl->IsShowingPlaceholderText() )
1237 // Show alternative placeholder-text when editing
1238 ShowPlaceholderText();
1241 mImpl->RequestRelayout();
1245 void Controller::KeyboardFocusLostEvent()
1247 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1249 if( mImpl->mEventData )
1251 if ( EventData::INTERRUPTED != mImpl->mEventData->mState )
1253 mImpl->ChangeState( EventData::INACTIVE );
1255 if( mImpl->IsShowingPlaceholderText() )
1257 // Revert to regular placeholder-text when not editing
1258 ShowPlaceholderText();
1262 mImpl->RequestRelayout();
1265 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1267 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1269 bool textChanged( false );
1271 if( mImpl->mEventData &&
1272 keyEvent.state == KeyEvent::Down )
1274 int keyCode = keyEvent.keyCode;
1275 const std::string& keyString = keyEvent.keyPressed;
1277 // Pre-process to separate modifying events from non-modifying input events.
1278 if( Dali::DALI_KEY_ESCAPE == keyCode )
1280 // Escape key is a special case which causes focus loss
1281 KeyboardFocusLostEvent();
1283 else if( Dali::DALI_KEY_CURSOR_LEFT == keyCode ||
1284 Dali::DALI_KEY_CURSOR_RIGHT == keyCode ||
1285 Dali::DALI_KEY_CURSOR_UP == keyCode ||
1286 Dali::DALI_KEY_CURSOR_DOWN == keyCode )
1288 Event event( Event::CURSOR_KEY_EVENT );
1289 event.p1.mInt = keyCode;
1290 mImpl->mEventData->mEventQueue.push_back( event );
1292 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1294 textChanged = BackspaceKeyEvent();
1296 else if ( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1298 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1299 // Avoids calling the InsertText() method which can delete selected text
1301 else if ( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1302 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1304 mImpl->ChangeState( EventData::INACTIVE );
1305 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1306 // Avoids calling the InsertText() method which can delete selected text
1308 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1310 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1311 // and a character is typed after the type of a upper case latin character.
1317 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1319 // IMF manager is no longer handling key-events
1320 mImpl->ClearPreEditFlag();
1322 InsertText( keyString, COMMIT );
1326 if ( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1327 ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1329 mImpl->ChangeState( EventData::EDITING );
1332 mImpl->RequestRelayout();
1337 // Do this last since it provides callbacks into application code
1338 mImpl->mControlInterface.TextChanged();
1344 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1346 bool removedPrevious( false );
1347 bool maxLengthReached( false );
1349 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1350 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1351 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1352 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1354 // TODO: At the moment the underline runs are only for pre-edit.
1355 mImpl->mVisualModel->mUnderlineRuns.Clear();
1357 Vector<Character> utf32Characters;
1358 Length characterCount( 0u );
1360 // Remove the previous IMF pre-edit (predicitive text)
1361 if( mImpl->mEventData &&
1362 mImpl->mEventData->mPreEditFlag &&
1363 0 != mImpl->mEventData->mPreEditLength )
1365 CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1367 removedPrevious = RemoveText( -static_cast<int>(offset), mImpl->mEventData->mPreEditLength );
1369 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1370 mImpl->mEventData->mPreEditLength = 0;
1374 // Remove the previous Selection
1375 removedPrevious = RemoveSelectedText();
1380 // Convert text into UTF-32
1381 utf32Characters.Resize( text.size() );
1383 // This is a bit horrible but std::string returns a (signed) char*
1384 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1386 // Transform a text array encoded in utf8 into an array encoded in utf32.
1387 // It returns the actual number of characters.
1388 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1389 utf32Characters.Resize( characterCount );
1391 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1392 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1395 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1397 // The placeholder text is no longer needed
1398 if( mImpl->IsShowingPlaceholderText() )
1403 mImpl->ChangeState( EventData::EDITING );
1405 // Handle the IMF (predicitive text) state changes
1406 if( mImpl->mEventData )
1408 if( COMMIT == type )
1410 // IMF manager is no longer handling key-events
1411 mImpl->ClearPreEditFlag();
1415 if( !mImpl->mEventData->mPreEditFlag )
1417 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1419 // Record the start of the pre-edit text
1420 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1423 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1424 mImpl->mEventData->mPreEditFlag = true;
1426 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1430 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1432 // Restrict new text to fit within Maximum characters setting
1433 Length maxSizeOfNewText = std::min ( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1434 maxLengthReached = ( characterCount > maxSizeOfNewText );
1436 // Insert at current cursor position
1437 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1439 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1441 if( cursorIndex < numberOfCharactersInModel )
1443 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1447 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1450 cursorIndex += maxSizeOfNewText;
1452 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1455 if( 0u == mImpl->mLogicalModel->mText.Count() &&
1456 mImpl->IsPlaceholderAvailable() )
1458 // Show place-holder if empty after removing the pre-edit text
1459 ShowPlaceholderText();
1460 mImpl->mEventData->mUpdateCursorPosition = true;
1461 mImpl->ClearPreEditFlag();
1463 else if( removedPrevious ||
1464 0 != utf32Characters.Count() )
1466 // Queue an inserted event
1467 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1470 if( maxLengthReached )
1472 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1474 mImpl->ResetImfManager();
1476 // Do this last since it provides callbacks into application code
1477 mImpl->mControlInterface.MaxLengthReached();
1481 bool Controller::RemoveSelectedText()
1483 bool textRemoved( false );
1485 if ( EventData::SELECTING == mImpl->mEventData->mState ||
1486 EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
1488 std::string removedString;
1489 mImpl->RetrieveSelection( removedString, true );
1491 if( !removedString.empty() )
1494 mImpl->ChangeState( EventData::EDITING );
1501 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1503 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1505 if( NULL != mImpl->mEventData )
1507 if( 1u == tapCount )
1509 // This is to avoid unnecessary relayouts when tapping an empty text-field
1510 bool relayoutNeeded( false );
1512 if( mImpl->IsShowingRealText() &&
1513 EventData::EDITING == mImpl->mEventData->mState )
1515 // Show grab handle on second tap
1516 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
1517 relayoutNeeded = true;
1519 else if( EventData::EDITING != mImpl->mEventData->mState &&
1520 EventData::EDITING_WITH_GRAB_HANDLE != mImpl->mEventData->mState )
1522 // Show cursor on first tap
1523 mImpl->ChangeState( EventData::EDITING );
1524 relayoutNeeded = true;
1526 else if( mImpl->IsShowingRealText() )
1529 relayoutNeeded = true;
1532 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
1533 if( relayoutNeeded )
1535 Event event( Event::TAP_EVENT );
1536 event.p1.mUint = tapCount;
1537 event.p2.mFloat = x;
1538 event.p3.mFloat = y;
1539 mImpl->mEventData->mEventQueue.push_back( event );
1541 mImpl->RequestRelayout();
1544 else if( 2u == tapCount )
1546 if( mImpl->mEventData->mSelectionEnabled &&
1547 mImpl->IsShowingRealText() )
1549 SelectEvent( x, y, false );
1554 // Reset keyboard as tap event has occurred.
1555 mImpl->ResetImfManager();
1558 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
1560 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1562 if( mImpl->mEventData )
1564 Event event( Event::PAN_EVENT );
1565 event.p1.mInt = state;
1566 event.p2.mFloat = displacement.x;
1567 event.p3.mFloat = displacement.y;
1568 mImpl->mEventData->mEventQueue.push_back( event );
1570 mImpl->RequestRelayout();
1574 void Controller::LongPressEvent( Gesture::State state, float x, float y )
1576 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1578 if ( mImpl->IsShowingPlaceholderText() || mImpl->mLogicalModel->mText.Count() == 0u )
1580 if ( mImpl->mEventData )
1582 Event event( Event::LONG_PRESS_EVENT );
1583 event.p1.mInt = state;
1584 mImpl->mEventData->mEventQueue.push_back( event );
1585 mImpl->RequestRelayout();
1588 else if( mImpl->mEventData )
1590 SelectEvent( x, y, false );
1594 void Controller::SelectEvent( float x, float y, bool selectAll )
1596 if( mImpl->mEventData )
1598 if ( mImpl->mEventData->mState == EventData::SELECTING )
1600 mImpl->ChangeState( EventData::SELECTION_CHANGED );
1604 mImpl->ChangeState( EventData::SELECTING );
1609 Event event( Event::SELECT_ALL );
1610 mImpl->mEventData->mEventQueue.push_back( event );
1614 Event event( Event::SELECT );
1615 event.p2.mFloat = x;
1616 event.p3.mFloat = y;
1617 mImpl->mEventData->mEventQueue.push_back( event );
1620 mImpl->RequestRelayout();
1624 void Controller::GetTargetSize( Vector2& targetSize )
1626 targetSize = mImpl->mVisualModel->mControlSize;
1629 void Controller::AddDecoration( Actor& actor, bool needsClipping )
1631 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
1634 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
1636 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
1638 if( mImpl->mEventData )
1640 switch( handleType )
1644 Event event( Event::GRAB_HANDLE_EVENT );
1645 event.p1.mUint = state;
1646 event.p2.mFloat = x;
1647 event.p3.mFloat = y;
1649 mImpl->mEventData->mEventQueue.push_back( event );
1652 case LEFT_SELECTION_HANDLE:
1654 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
1655 event.p1.mUint = state;
1656 event.p2.mFloat = x;
1657 event.p3.mFloat = y;
1659 mImpl->mEventData->mEventQueue.push_back( event );
1662 case RIGHT_SELECTION_HANDLE:
1664 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
1665 event.p1.mUint = state;
1666 event.p2.mFloat = x;
1667 event.p3.mFloat = y;
1669 mImpl->mEventData->mEventQueue.push_back( event );
1672 case LEFT_SELECTION_HANDLE_MARKER:
1673 case RIGHT_SELECTION_HANDLE_MARKER:
1675 // Markers do not move the handles.
1678 case HANDLE_TYPE_COUNT:
1680 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
1684 mImpl->RequestRelayout();
1688 void Controller::PasteText( const std::string& stringToPaste )
1690 InsertText( stringToPaste, Text::Controller::COMMIT );
1691 mImpl->ChangeState( EventData::EDITING );
1692 mImpl->RequestRelayout();
1695 void Controller::PasteClipboardItemEvent()
1697 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
1698 std::string stringToPaste( notifier.GetContent() );
1699 PasteText( stringToPaste );
1702 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
1704 if( NULL == mImpl->mEventData )
1711 case Toolkit::TextSelectionPopup::CUT:
1713 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
1714 mImpl->mOperationsPending = ALL_OPERATIONS;
1715 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1716 !mImpl->IsPlaceholderAvailable() )
1718 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1722 ShowPlaceholderText();
1723 mImpl->mEventData->mUpdateCursorPosition = true;
1725 mImpl->RequestRelayout();
1726 mImpl->mControlInterface.TextChanged();
1729 case Toolkit::TextSelectionPopup::COPY:
1731 mImpl->SendSelectionToClipboard( false ); // Text not modified
1732 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
1735 case Toolkit::TextSelectionPopup::PASTE:
1737 std::string stringToPaste("");
1738 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
1739 PasteText( stringToPaste );
1742 case Toolkit::TextSelectionPopup::SELECT:
1744 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
1746 if( mImpl->mEventData->mSelectionEnabled )
1748 // Creates a SELECT event.
1749 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
1753 case Toolkit::TextSelectionPopup::SELECT_ALL:
1755 // Creates a SELECT_ALL event
1756 SelectEvent( 0.f, 0.f, true );
1759 case Toolkit::TextSelectionPopup::CLIPBOARD:
1761 mImpl->ShowClipboard();
1764 case Toolkit::TextSelectionPopup::NONE:
1772 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
1774 bool update( false );
1775 bool requestRelayout = false;
1778 unsigned int cursorPosition( 0 );
1780 switch ( imfEvent.eventName )
1782 case ImfManager::COMMIT:
1784 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
1785 requestRelayout = true;
1788 case ImfManager::PREEDIT:
1790 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
1792 requestRelayout = true;
1795 case ImfManager::DELETESURROUNDING:
1797 update = RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
1801 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1802 !mImpl->IsPlaceholderAvailable() )
1804 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1808 ShowPlaceholderText();
1809 mImpl->mEventData->mUpdateCursorPosition = true;
1812 requestRelayout = true;
1815 case ImfManager::GETSURROUNDING:
1818 cursorPosition = GetLogicalCursorPosition();
1820 imfManager.SetSurroundingText( text );
1821 imfManager.SetCursorPosition( cursorPosition );
1824 case ImfManager::VOID:
1831 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
1834 cursorPosition = GetLogicalCursorPosition();
1837 if( requestRelayout )
1839 mImpl->mOperationsPending = ALL_OPERATIONS;
1840 mImpl->RequestRelayout();
1842 // Do this last since it provides callbacks into application code
1843 mImpl->mControlInterface.TextChanged();
1846 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
1848 return callbackData;
1851 Controller::~Controller()
1856 bool Controller::BackspaceKeyEvent()
1858 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
1860 // IMF manager is no longer handling key-events
1861 mImpl->ClearPreEditFlag();
1863 bool removed( false );
1865 if ( EventData::SELECTING == mImpl->mEventData->mState ||
1866 EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
1868 removed = RemoveSelectedText();
1870 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
1872 // Remove the character before the current cursor position
1873 removed = RemoveText( -1, 1 );
1878 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1879 !mImpl->IsPlaceholderAvailable() )
1881 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1885 ShowPlaceholderText();
1886 mImpl->mEventData->mUpdateCursorPosition = true;
1893 void Controller::ShowPlaceholderText()
1895 if( mImpl->IsPlaceholderAvailable() )
1897 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
1899 mImpl->mEventData->mIsShowingPlaceholderText = true;
1901 // Disable handles when showing place-holder text
1902 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
1903 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
1904 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
1906 const char* text( NULL );
1909 // TODO - Switch placeholder text styles when changing state
1910 if( EventData::INACTIVE != mImpl->mEventData->mState &&
1911 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() )
1913 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
1914 size = mImpl->mEventData->mPlaceholderTextActive.size();
1918 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
1919 size = mImpl->mEventData->mPlaceholderTextInactive.size();
1922 // Reset model for showing placeholder.
1923 mImpl->mLogicalModel->mText.Clear();
1925 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
1927 // Convert text into UTF-32
1928 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
1929 utf32Characters.Resize( size );
1931 // This is a bit horrible but std::string returns a (signed) char*
1932 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
1934 // Transform a text array encoded in utf8 into an array encoded in utf32.
1935 // It returns the actual number of characters.
1936 Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
1937 utf32Characters.Resize( characterCount );
1939 // Reset the cursor position
1940 mImpl->mEventData->mPrimaryCursorPosition = 0;
1942 // The natural size needs to be re-calculated.
1943 mImpl->mRecalculateNaturalSize = true;
1945 // Apply modifications to the model
1946 mImpl->mOperationsPending = ALL_OPERATIONS;
1948 // Update the rest of the model during size negotiation
1949 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
1953 void Controller::ClearModelData()
1955 // n.b. This does not Clear the mText from mLogicalModel
1956 mImpl->mLogicalModel->mScriptRuns.Clear();
1957 mImpl->mLogicalModel->mFontRuns.Clear();
1958 mImpl->mLogicalModel->mLineBreakInfo.Clear();
1959 mImpl->mLogicalModel->mWordBreakInfo.Clear();
1960 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
1961 mImpl->mLogicalModel->mCharacterDirections.Clear();
1962 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
1963 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
1964 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
1965 mImpl->mVisualModel->mGlyphs.Clear();
1966 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
1967 mImpl->mVisualModel->mCharactersToGlyph.Clear();
1968 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
1969 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
1970 mImpl->mVisualModel->mGlyphPositions.Clear();
1971 mImpl->mVisualModel->mLines.Clear();
1972 mImpl->mVisualModel->ClearCaches();
1975 void Controller::ClearFontData()
1977 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
1978 mImpl->mLogicalModel->mFontRuns.Clear();
1979 mImpl->mVisualModel->mGlyphs.Clear();
1980 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
1981 mImpl->mVisualModel->mCharactersToGlyph.Clear();
1982 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
1983 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
1984 mImpl->mVisualModel->mGlyphPositions.Clear();
1985 mImpl->mVisualModel->mLines.Clear();
1986 mImpl->mVisualModel->ClearCaches();
1989 Controller::Controller( ControlInterface& controlInterface )
1992 mImpl = new Controller::Impl( controlInterface );
1997 } // namespace Toolkit