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 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s userDefined: %s\n", defaultFontFamily.c_str(), userDefined ? "true":"false" );
238 mImpl->mUserDefinedFontFamily = userDefined;
240 // Clear the font-specific data
243 mImpl->mOperationsPending = ALL_OPERATIONS;
244 mImpl->mRecalculateNaturalSize = true;
246 mImpl->RequestRelayout();
249 const std::string& Controller::GetDefaultFontFamily() const
251 if( mImpl->mFontDefaults )
253 return mImpl->mFontDefaults->mFontDescription.family;
259 void Controller::SetDefaultFontStyle( const std::string& style )
261 if( !mImpl->mFontDefaults )
263 mImpl->mFontDefaults = new FontDefaults();
266 mImpl->mFontDefaults->mFontStyle = style;
269 const std::string& Controller::GetDefaultFontStyle() const
271 if( mImpl->mFontDefaults )
273 return mImpl->mFontDefaults->mFontStyle;
279 void Controller::SetDefaultFontWidth( FontWidth width )
281 if( !mImpl->mFontDefaults )
283 mImpl->mFontDefaults = new FontDefaults();
286 mImpl->mFontDefaults->mFontDescription.width = width;
288 // Clear the font-specific data
291 mImpl->mOperationsPending = ALL_OPERATIONS;
292 mImpl->mRecalculateNaturalSize = true;
294 mImpl->RequestRelayout();
297 FontWidth Controller::GetDefaultFontWidth() const
299 if( mImpl->mFontDefaults )
301 return mImpl->mFontDefaults->mFontDescription.width;
304 return TextAbstraction::FontWidth::NORMAL;
307 void Controller::SetDefaultFontWeight( FontWeight weight )
309 if( !mImpl->mFontDefaults )
311 mImpl->mFontDefaults = new FontDefaults();
314 mImpl->mFontDefaults->mFontDescription.weight = weight;
316 // Clear the font-specific data
319 mImpl->mOperationsPending = ALL_OPERATIONS;
320 mImpl->mRecalculateNaturalSize = true;
322 mImpl->RequestRelayout();
325 FontWeight Controller::GetDefaultFontWeight() const
327 if( mImpl->mFontDefaults )
329 return mImpl->mFontDefaults->mFontDescription.weight;
332 return TextAbstraction::FontWeight::NORMAL;
335 void Controller::SetDefaultFontSlant( FontSlant slant )
337 if( !mImpl->mFontDefaults )
339 mImpl->mFontDefaults = new FontDefaults();
342 mImpl->mFontDefaults->mFontDescription.slant = slant;
344 // Clear the font-specific data
347 mImpl->mOperationsPending = ALL_OPERATIONS;
348 mImpl->mRecalculateNaturalSize = true;
350 mImpl->RequestRelayout();
353 FontSlant Controller::GetDefaultFontSlant() const
355 if( mImpl->mFontDefaults )
357 return mImpl->mFontDefaults->mFontDescription.slant;
360 return TextAbstraction::FontSlant::NORMAL;
363 void Controller::SetDefaultPointSize( float pointSize )
365 if( !mImpl->mFontDefaults )
367 mImpl->mFontDefaults = new FontDefaults();
370 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
372 unsigned int horizontalDpi( 0u );
373 unsigned int verticalDpi( 0u );
374 mImpl->mFontClient.GetDpi( horizontalDpi, verticalDpi );
376 // Adjust the metrics if the fixed-size font should be down-scaled
377 int maxEmojiSize( pointSize/POINTS_PER_INCH * verticalDpi );
378 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultPointSize %p setting MaxEmojiSize %d\n", this, maxEmojiSize );
379 mImpl->mMetrics->SetMaxEmojiSize( maxEmojiSize );
381 // Clear the font-specific data
384 mImpl->mOperationsPending = ALL_OPERATIONS;
385 mImpl->mRecalculateNaturalSize = true;
387 mImpl->RequestRelayout();
390 float Controller::GetDefaultPointSize() const
392 if( mImpl->mFontDefaults )
394 return mImpl->mFontDefaults->mDefaultPointSize;
400 void Controller::UpdateAfterFontChange( std::string& newDefaultFont )
402 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange");
406 if ( !mImpl->mUserDefinedFontFamily ) // If user defined font then should not update when system font changes
408 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
409 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
410 mImpl->UpdateModel( ALL_OPERATIONS );
411 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
412 mImpl->mRecalculateNaturalSize = true;
413 mImpl->RequestRelayout();
417 void Controller::SetTextColor( const Vector4& textColor )
419 mImpl->mTextColor = textColor;
421 if( !mImpl->IsShowingPlaceholderText() )
423 mImpl->mVisualModel->SetTextColor( textColor );
425 mImpl->RequestRelayout();
429 const Vector4& Controller::GetTextColor() const
431 return mImpl->mTextColor;
434 bool Controller::RemoveText( int cursorOffset, int numberOfChars )
436 bool removed( false );
438 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfChars %d\n",
439 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfChars );
441 if( !mImpl->IsShowingPlaceholderText() )
443 // Delete at current cursor position
444 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
445 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
447 CharacterIndex cursorIndex = oldCursorIndex;
449 // Validate the cursor position & number of characters
450 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
452 cursorIndex = oldCursorIndex + cursorOffset;
455 if( (cursorIndex + numberOfChars) > currentText.Count() )
457 numberOfChars = currentText.Count() - cursorIndex;
460 if( (cursorIndex + numberOfChars) <= currentText.Count() )
462 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
463 Vector<Character>::Iterator last = first + numberOfChars;
465 currentText.Erase( first, last );
467 // Cursor position retreat
468 oldCursorIndex = cursorIndex;
470 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfChars );
478 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
480 if( mImpl->mEventData )
482 mImpl->mEventData->mPlaceholderTextColor = textColor;
485 if( mImpl->IsShowingPlaceholderText() )
487 mImpl->mVisualModel->SetTextColor( textColor );
488 mImpl->RequestRelayout();
492 const Vector4& Controller::GetPlaceholderTextColor() const
494 if( mImpl->mEventData )
496 return mImpl->mEventData->mPlaceholderTextColor;
502 void Controller::SetShadowOffset( const Vector2& shadowOffset )
504 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
506 mImpl->RequestRelayout();
509 const Vector2& Controller::GetShadowOffset() const
511 return mImpl->mVisualModel->GetShadowOffset();
514 void Controller::SetShadowColor( const Vector4& shadowColor )
516 mImpl->mVisualModel->SetShadowColor( shadowColor );
518 mImpl->RequestRelayout();
521 const Vector4& Controller::GetShadowColor() const
523 return mImpl->mVisualModel->GetShadowColor();
526 void Controller::SetUnderlineColor( const Vector4& color )
528 mImpl->mVisualModel->SetUnderlineColor( color );
530 mImpl->RequestRelayout();
533 const Vector4& Controller::GetUnderlineColor() const
535 return mImpl->mVisualModel->GetUnderlineColor();
538 void Controller::SetUnderlineEnabled( bool enabled )
540 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
542 mImpl->RequestRelayout();
545 bool Controller::IsUnderlineEnabled() const
547 return mImpl->mVisualModel->IsUnderlineEnabled();
550 void Controller::SetUnderlineHeight( float height )
552 mImpl->mVisualModel->SetUnderlineHeight( height );
554 mImpl->RequestRelayout();
557 float Controller::GetUnderlineHeight() const
559 return mImpl->mVisualModel->GetUnderlineHeight();
562 void Controller::SetEnableCursorBlink( bool enable )
564 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
566 if( mImpl->mEventData )
568 mImpl->mEventData->mCursorBlinkEnabled = enable;
571 mImpl->mEventData->mDecorator )
573 mImpl->mEventData->mDecorator->StopCursorBlink();
578 bool Controller::GetEnableCursorBlink() const
580 if( mImpl->mEventData )
582 return mImpl->mEventData->mCursorBlinkEnabled;
588 const Vector2& Controller::GetScrollPosition() const
590 if( mImpl->mEventData )
592 return mImpl->mEventData->mScrollPosition;
595 return Vector2::ZERO;
598 const Vector2& Controller::GetAlignmentOffset() const
600 return mImpl->mAlignmentOffset;
603 Vector3 Controller::GetNaturalSize()
605 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
608 // Make sure the model is up-to-date before layouting
609 ProcessModifyEvents();
611 if( mImpl->mRecalculateNaturalSize )
613 // Operations that can be done only once until the text changes.
614 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
622 // Make sure the model is up-to-date before layouting
623 mImpl->UpdateModel( onlyOnceOperations );
625 // Operations that need to be done if the size changes.
626 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
630 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
631 static_cast<OperationsMask>( onlyOnceOperations |
633 naturalSize.GetVectorXY() );
635 // Do not do again the only once operations.
636 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
638 // Do the size related operations again.
639 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
641 // Stores the natural size to avoid recalculate it again
642 // unless the text/style changes.
643 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
645 mImpl->mRecalculateNaturalSize = false;
647 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
651 naturalSize = mImpl->mVisualModel->GetNaturalSize();
653 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
656 naturalSize.x = ConvertToEven( naturalSize.x );
657 naturalSize.y = ConvertToEven( naturalSize.y );
662 float Controller::GetHeightForWidth( float width )
664 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
665 // Make sure the model is up-to-date before layouting
666 ProcessModifyEvents();
669 if( width != mImpl->mVisualModel->mControlSize.width )
671 // Operations that can be done only once until the text changes.
672 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
680 // Make sure the model is up-to-date before layouting
681 mImpl->UpdateModel( onlyOnceOperations );
683 // Operations that need to be done if the size changes.
684 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
688 DoRelayout( Size( width, MAX_FLOAT ),
689 static_cast<OperationsMask>( onlyOnceOperations |
693 // Do not do again the only once operations.
694 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
696 // Do the size related operations again.
697 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
698 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
702 layoutSize = mImpl->mVisualModel->GetActualSize();
703 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
706 return layoutSize.height;
709 bool Controller::Relayout( const Size& size )
711 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
713 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
715 bool glyphsRemoved( false );
716 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
718 mImpl->mVisualModel->mGlyphPositions.Clear();
719 glyphsRemoved = true;
721 // Not worth to relayout if width or height is equal to zero.
722 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
723 return glyphsRemoved;
726 if( size != mImpl->mVisualModel->mControlSize )
728 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
730 // Operations that need to be done if the size changes.
731 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
737 mImpl->mVisualModel->mControlSize = size;
740 // Make sure the model is up-to-date before layouting
741 ProcessModifyEvents();
742 mImpl->UpdateModel( mImpl->mOperationsPending );
745 bool updated = DoRelayout( mImpl->mVisualModel->mControlSize,
746 mImpl->mOperationsPending,
749 // Do not re-do any operation until something changes.
750 mImpl->mOperationsPending = NO_OPERATION;
752 // Keep the current offset and alignment as it will be used to update the decorator's positions.
754 if( mImpl->mEventData )
756 offset = mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition;
759 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
760 CalculateTextAlignment( size );
762 if( mImpl->mEventData )
764 // If there is a nex size, the scroll position needs to be clamped.
765 mImpl->ClampHorizontalScroll( layoutSize );
767 // Update the decorator's positions.
768 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
770 // Move the cursor, grab handle etc.
771 updated = mImpl->ProcessInputEvents() || updated;
774 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
778 void Controller::ProcessModifyEvents()
780 std::vector<ModifyEvent>& events = mImpl->mModifyEvents;
782 for( unsigned int i=0; i<events.size(); ++i )
784 if( ModifyEvent::TEXT_REPLACED == events[0].type )
786 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
787 DALI_ASSERT_DEBUG( 0 == i && "Unexpected TEXT_REPLACED event" );
791 else if( ModifyEvent::TEXT_INSERTED == events[0].type )
795 else if( ModifyEvent::TEXT_DELETED == events[0].type )
797 // Placeholder-text cannot be deleted
798 if( !mImpl->IsShowingPlaceholderText() )
805 if( mImpl->mEventData &&
808 // When the text is being modified, delay cursor blinking
809 mImpl->mEventData->mDecorator->DelayCursorBlink();
812 // Discard temporary text
816 void Controller::ResetText()
819 mImpl->mLogicalModel->mText.Clear();
822 // We have cleared everything including the placeholder-text
823 mImpl->PlaceholderCleared();
825 // The natural size needs to be re-calculated.
826 mImpl->mRecalculateNaturalSize = true;
828 // Apply modifications to the model
829 mImpl->mOperationsPending = ALL_OPERATIONS;
832 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
834 // Reset the cursor position
835 if( NULL != mImpl->mEventData )
837 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
839 // Update the cursor if it's in editing mode.
840 if( ( EventData::EDITING == mImpl->mEventData->mState ) ||
841 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
842 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
844 mImpl->mEventData->mUpdateCursorPosition = true;
849 void Controller::ResetScrollPosition()
851 if( NULL != mImpl->mEventData )
853 // Reset the scroll position.
854 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
855 mImpl->mEventData->mScrollAfterUpdatePosition = true;
859 void Controller::TextReplacedEvent()
864 // The natural size needs to be re-calculated.
865 mImpl->mRecalculateNaturalSize = true;
867 // Apply modifications to the model
868 mImpl->mOperationsPending = ALL_OPERATIONS;
869 mImpl->UpdateModel( ALL_OPERATIONS );
870 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
876 void Controller::TextInsertedEvent()
878 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
880 // TODO - Optimize this
883 // The natural size needs to be re-calculated.
884 mImpl->mRecalculateNaturalSize = true;
886 // Apply modifications to the model; TODO - Optimize this
887 mImpl->mOperationsPending = ALL_OPERATIONS;
888 mImpl->UpdateModel( ALL_OPERATIONS );
889 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
894 // Queue a cursor reposition event; this must wait until after DoRelayout()
895 if( ( EventData::EDITING == mImpl->mEventData->mState ) ||
896 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
897 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
899 mImpl->mEventData->mUpdateCursorPosition = true;
900 mImpl->mEventData->mScrollAfterUpdatePosition = true;
904 void Controller::TextDeletedEvent()
906 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
908 // TODO - Optimize this
911 // The natural size needs to be re-calculated.
912 mImpl->mRecalculateNaturalSize = true;
914 // Apply modifications to the model; TODO - Optimize this
915 mImpl->mOperationsPending = ALL_OPERATIONS;
916 mImpl->UpdateModel( ALL_OPERATIONS );
917 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
922 // Queue a cursor reposition event; this must wait until after DoRelayout()
923 if( 0u == mImpl->mLogicalModel->mText.Count() )
925 mImpl->mEventData->mUpdateCursorPosition = true;
929 mImpl->mEventData->mScrollAfterDelete = true;
933 bool Controller::DoRelayout( const Size& size,
934 OperationsMask operationsRequired,
937 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
938 bool viewUpdated( false );
940 // Calculate the operations to be done.
941 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
943 if( LAYOUT & operations )
945 // Some vectors with data needed to layout and reorder may be void
946 // after the first time the text has been laid out.
947 // Fill the vectors again.
949 const Length numberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
951 if( 0u == numberOfGlyphs )
953 // Nothing else to do if there is no glyphs.
954 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
958 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
959 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
960 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
961 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
962 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
963 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
964 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
966 // Set the layout parameters.
967 LayoutParameters layoutParameters( size,
969 lineBreakInfo.Begin(),
970 wordBreakInfo.Begin(),
971 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
974 glyphsToCharactersMap.Begin(),
975 charactersPerGlyph.Begin() );
977 // The laid-out lines.
978 // It's not possible to know in how many lines the text is going to be laid-out,
979 // but it can be resized at least with the number of 'paragraphs' to avoid
980 // some re-allocations.
981 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
983 // Delete any previous laid out lines before setting the new ones.
986 // The capacity of the bidirectional paragraph info is the number of paragraphs.
987 lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
989 // Resize the vector of positions to have the same size than the vector of glyphs.
990 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
991 glyphPositions.Resize( numberOfGlyphs );
993 // Whether the last character is a new paragraph character.
994 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
996 // Update the visual model.
997 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1004 // Reorder the lines
1005 if( REORDER & operations )
1007 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1009 // Check first if there are paragraphs with bidirectional info.
1010 if( 0u != bidirectionalInfo.Count() )
1013 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1015 // Reorder the lines.
1016 Vector<BidirectionalLineInfoRun> lineBidirectionalInfoRuns;
1017 lineBidirectionalInfoRuns.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1018 ReorderLines( bidirectionalInfo,
1020 lineBidirectionalInfoRuns );
1022 // Set the bidirectional info into the model.
1023 const Length numberOfBidirectionalInfoRuns = lineBidirectionalInfoRuns.Count();
1024 mImpl->mLogicalModel->SetVisualToLogicalMap( lineBidirectionalInfoRuns.Begin(),
1025 numberOfBidirectionalInfoRuns );
1027 // Set the bidirectional info per line into the layout parameters.
1028 layoutParameters.lineBidirectionalInfoRunsBuffer = lineBidirectionalInfoRuns.Begin();
1029 layoutParameters.numberOfBidirectionalInfoRuns = numberOfBidirectionalInfoRuns;
1031 // Get the character to glyph conversion table and set into the layout.
1032 layoutParameters.charactersToGlyphsBuffer = mImpl->mVisualModel->mCharactersToGlyph.Begin();
1034 // Get the glyphs per character table and set into the layout.
1035 layoutParameters.glyphsPerCharacterBuffer = mImpl->mVisualModel->mGlyphsPerCharacter.Begin();
1037 // Re-layout the text. Reorder those lines with right to left characters.
1038 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1041 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
1042 for( Vector<BidirectionalLineInfoRun>::Iterator it = lineBidirectionalInfoRuns.Begin(),
1043 endIt = lineBidirectionalInfoRuns.End();
1047 BidirectionalLineInfoRun& bidiLineInfo = *it;
1049 free( bidiLineInfo.visualToLogicalMap );
1054 // Sets the actual size.
1055 if( UPDATE_ACTUAL_SIZE & operations )
1057 mImpl->mVisualModel->SetActualSize( layoutSize );
1063 layoutSize = mImpl->mVisualModel->GetActualSize();
1066 if( ALIGN & operations )
1068 // The laid-out lines.
1069 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1071 mImpl->mLayoutEngine.Align( layoutSize,
1077 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1081 void Controller::SetMultiLineEnabled( bool enable )
1083 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1085 if( layout != mImpl->mLayoutEngine.GetLayout() )
1087 // Set the layout type.
1088 mImpl->mLayoutEngine.SetLayout( layout );
1090 // Set the flags to redo the layout operations
1091 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1092 UPDATE_ACTUAL_SIZE |
1096 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1098 mImpl->RequestRelayout();
1102 bool Controller::IsMultiLineEnabled() const
1104 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1107 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1109 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1111 // Set the alignment.
1112 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1114 // Set the flag to redo the alignment operation.
1115 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1117 mImpl->RequestRelayout();
1121 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1123 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1126 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1128 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1130 // Set the alignment.
1131 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1133 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1135 mImpl->RequestRelayout();
1139 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1141 return mImpl->mLayoutEngine.GetVerticalAlignment();
1144 void Controller::CalculateTextAlignment( const Size& size )
1146 // Get the direction of the first character.
1147 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1149 Size actualSize = mImpl->mVisualModel->GetActualSize();
1150 if( fabsf( actualSize.height ) < Math::MACHINE_EPSILON_1000 )
1152 // Get the line height of the default font.
1153 actualSize.height = mImpl->GetDefaultFontLineHeight();
1156 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1157 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1158 if( firstParagraphDirection &&
1159 ( LayoutEngine::HORIZONTAL_ALIGN_CENTER != horizontalAlignment ) )
1161 if( LayoutEngine::HORIZONTAL_ALIGN_BEGIN == horizontalAlignment )
1163 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1167 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1171 switch( horizontalAlignment )
1173 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1175 mImpl->mAlignmentOffset.x = 0.f;
1178 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1180 const int intOffset = static_cast<int>( 0.5f * ( size.width - actualSize.width ) ); // try to avoid pixel alignment.
1181 mImpl->mAlignmentOffset.x = static_cast<float>( intOffset );
1184 case LayoutEngine::HORIZONTAL_ALIGN_END:
1186 mImpl->mAlignmentOffset.x = size.width - actualSize.width;
1191 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1192 switch( verticalAlignment )
1194 case LayoutEngine::VERTICAL_ALIGN_TOP:
1196 mImpl->mAlignmentOffset.y = 0.f;
1199 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1201 const int intOffset = static_cast<int>( 0.5f * ( size.height - actualSize.height ) ); // try to avoid pixel alignment.
1202 mImpl->mAlignmentOffset.y = static_cast<float>( intOffset );
1205 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1207 mImpl->mAlignmentOffset.y = size.height - actualSize.height;
1213 LayoutEngine& Controller::GetLayoutEngine()
1215 return mImpl->mLayoutEngine;
1218 View& Controller::GetView()
1220 return mImpl->mView;
1223 void Controller::KeyboardFocusGainEvent()
1225 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1227 if( mImpl->mEventData )
1229 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1230 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1232 mImpl->ChangeState( EventData::EDITING );
1233 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1236 if( mImpl->IsShowingPlaceholderText() )
1238 // Show alternative placeholder-text when editing
1239 ShowPlaceholderText();
1242 mImpl->RequestRelayout();
1246 void Controller::KeyboardFocusLostEvent()
1248 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1250 if( mImpl->mEventData )
1252 if ( EventData::INTERRUPTED != mImpl->mEventData->mState )
1254 mImpl->ChangeState( EventData::INACTIVE );
1256 if( mImpl->IsShowingPlaceholderText() )
1258 // Revert to regular placeholder-text when not editing
1259 ShowPlaceholderText();
1263 mImpl->RequestRelayout();
1266 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1268 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1270 bool textChanged( false );
1272 if( mImpl->mEventData &&
1273 keyEvent.state == KeyEvent::Down )
1275 int keyCode = keyEvent.keyCode;
1276 const std::string& keyString = keyEvent.keyPressed;
1278 // Pre-process to separate modifying events from non-modifying input events.
1279 if( Dali::DALI_KEY_ESCAPE == keyCode )
1281 // Escape key is a special case which causes focus loss
1282 KeyboardFocusLostEvent();
1284 else if( Dali::DALI_KEY_CURSOR_LEFT == keyCode ||
1285 Dali::DALI_KEY_CURSOR_RIGHT == keyCode ||
1286 Dali::DALI_KEY_CURSOR_UP == keyCode ||
1287 Dali::DALI_KEY_CURSOR_DOWN == keyCode )
1289 Event event( Event::CURSOR_KEY_EVENT );
1290 event.p1.mInt = keyCode;
1291 mImpl->mEventData->mEventQueue.push_back( event );
1293 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1295 textChanged = BackspaceKeyEvent();
1297 else if ( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1299 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1300 // Avoids calling the InsertText() method which can delete selected text
1302 else if ( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1303 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1305 mImpl->ChangeState( EventData::INACTIVE );
1306 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1307 // Avoids calling the InsertText() method which can delete selected text
1309 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1311 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1312 // and a character is typed after the type of a upper case latin character.
1318 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1320 // IMF manager is no longer handling key-events
1321 mImpl->ClearPreEditFlag();
1323 InsertText( keyString, COMMIT );
1327 if ( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1328 ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1330 mImpl->ChangeState( EventData::EDITING );
1333 mImpl->RequestRelayout();
1338 // Do this last since it provides callbacks into application code
1339 mImpl->mControlInterface.TextChanged();
1345 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1347 bool removedPrevious( false );
1348 bool maxLengthReached( false );
1350 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1351 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1352 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1353 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1355 // TODO: At the moment the underline runs are only for pre-edit.
1356 mImpl->mVisualModel->mUnderlineRuns.Clear();
1358 Vector<Character> utf32Characters;
1359 Length characterCount( 0u );
1361 // Remove the previous IMF pre-edit (predicitive text)
1362 if( mImpl->mEventData &&
1363 mImpl->mEventData->mPreEditFlag &&
1364 0 != mImpl->mEventData->mPreEditLength )
1366 CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1368 removedPrevious = RemoveText( -static_cast<int>(offset), mImpl->mEventData->mPreEditLength );
1370 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1371 mImpl->mEventData->mPreEditLength = 0;
1375 // Remove the previous Selection
1376 removedPrevious = RemoveSelectedText();
1381 // Convert text into UTF-32
1382 utf32Characters.Resize( text.size() );
1384 // This is a bit horrible but std::string returns a (signed) char*
1385 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1387 // Transform a text array encoded in utf8 into an array encoded in utf32.
1388 // It returns the actual number of characters.
1389 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1390 utf32Characters.Resize( characterCount );
1392 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1393 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1396 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1398 // The placeholder text is no longer needed
1399 if( mImpl->IsShowingPlaceholderText() )
1404 mImpl->ChangeState( EventData::EDITING );
1406 // Handle the IMF (predicitive text) state changes
1407 if( mImpl->mEventData )
1409 if( COMMIT == type )
1411 // IMF manager is no longer handling key-events
1412 mImpl->ClearPreEditFlag();
1416 if( !mImpl->mEventData->mPreEditFlag )
1418 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1420 // Record the start of the pre-edit text
1421 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1424 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1425 mImpl->mEventData->mPreEditFlag = true;
1427 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1431 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1433 // Restrict new text to fit within Maximum characters setting
1434 Length maxSizeOfNewText = std::min ( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1435 maxLengthReached = ( characterCount > maxSizeOfNewText );
1437 // Insert at current cursor position
1438 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1440 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1442 if( cursorIndex < numberOfCharactersInModel )
1444 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1448 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1451 cursorIndex += maxSizeOfNewText;
1453 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1456 if( 0u == mImpl->mLogicalModel->mText.Count() &&
1457 mImpl->IsPlaceholderAvailable() )
1459 // Show place-holder if empty after removing the pre-edit text
1460 ShowPlaceholderText();
1461 mImpl->mEventData->mUpdateCursorPosition = true;
1462 mImpl->ClearPreEditFlag();
1464 else if( removedPrevious ||
1465 0 != utf32Characters.Count() )
1467 // Queue an inserted event
1468 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1471 if( maxLengthReached )
1473 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1475 mImpl->ResetImfManager();
1477 // Do this last since it provides callbacks into application code
1478 mImpl->mControlInterface.MaxLengthReached();
1482 bool Controller::RemoveSelectedText()
1484 bool textRemoved( false );
1486 if ( EventData::SELECTING == mImpl->mEventData->mState ||
1487 EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
1489 std::string removedString;
1490 mImpl->RetrieveSelection( removedString, true );
1492 if( !removedString.empty() )
1495 mImpl->ChangeState( EventData::EDITING );
1502 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1504 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1506 if( NULL != mImpl->mEventData )
1508 if( 1u == tapCount )
1510 // This is to avoid unnecessary relayouts when tapping an empty text-field
1511 bool relayoutNeeded( false );
1513 if( mImpl->IsShowingRealText() &&
1514 EventData::EDITING == mImpl->mEventData->mState )
1516 // Show grab handle on second tap
1517 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
1518 relayoutNeeded = true;
1520 else if( EventData::EDITING != mImpl->mEventData->mState &&
1521 EventData::EDITING_WITH_GRAB_HANDLE != mImpl->mEventData->mState )
1523 // Show cursor on first tap
1524 mImpl->ChangeState( EventData::EDITING );
1525 relayoutNeeded = true;
1527 else if( mImpl->IsShowingRealText() )
1530 relayoutNeeded = true;
1533 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
1534 if( relayoutNeeded )
1536 Event event( Event::TAP_EVENT );
1537 event.p1.mUint = tapCount;
1538 event.p2.mFloat = x;
1539 event.p3.mFloat = y;
1540 mImpl->mEventData->mEventQueue.push_back( event );
1542 mImpl->RequestRelayout();
1545 else if( 2u == tapCount )
1547 if( mImpl->mEventData->mSelectionEnabled &&
1548 mImpl->IsShowingRealText() )
1550 SelectEvent( x, y, false );
1555 // Reset keyboard as tap event has occurred.
1556 mImpl->ResetImfManager();
1559 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
1561 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1563 if( mImpl->mEventData )
1565 Event event( Event::PAN_EVENT );
1566 event.p1.mInt = state;
1567 event.p2.mFloat = displacement.x;
1568 event.p3.mFloat = displacement.y;
1569 mImpl->mEventData->mEventQueue.push_back( event );
1571 mImpl->RequestRelayout();
1575 void Controller::LongPressEvent( Gesture::State state, float x, float y )
1577 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
1579 if( state == Gesture::Started &&
1582 if( ! mImpl->IsShowingRealText() )
1584 Event event( Event::LONG_PRESS_EVENT );
1585 event.p1.mInt = state;
1586 mImpl->mEventData->mEventQueue.push_back( event );
1587 mImpl->RequestRelayout();
1591 // The 1st long-press on inactive text-field is treated as tap
1592 if( EventData::INACTIVE == mImpl->mEventData->mState )
1594 mImpl->ChangeState( EventData::EDITING );
1596 Event event( Event::TAP_EVENT );
1598 event.p2.mFloat = x;
1599 event.p3.mFloat = y;
1600 mImpl->mEventData->mEventQueue.push_back( event );
1602 mImpl->RequestRelayout();
1606 SelectEvent( x, y, false );
1612 void Controller::SelectEvent( float x, float y, bool selectAll )
1614 if( mImpl->mEventData )
1616 if ( mImpl->mEventData->mState == EventData::SELECTING )
1618 mImpl->ChangeState( EventData::SELECTION_CHANGED );
1622 mImpl->ChangeState( EventData::SELECTING );
1627 Event event( Event::SELECT_ALL );
1628 mImpl->mEventData->mEventQueue.push_back( event );
1632 Event event( Event::SELECT );
1633 event.p2.mFloat = x;
1634 event.p3.mFloat = y;
1635 mImpl->mEventData->mEventQueue.push_back( event );
1638 mImpl->RequestRelayout();
1642 void Controller::GetTargetSize( Vector2& targetSize )
1644 targetSize = mImpl->mVisualModel->mControlSize;
1647 void Controller::AddDecoration( Actor& actor, bool needsClipping )
1649 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
1652 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
1654 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
1656 if( mImpl->mEventData )
1658 switch( handleType )
1662 Event event( Event::GRAB_HANDLE_EVENT );
1663 event.p1.mUint = state;
1664 event.p2.mFloat = x;
1665 event.p3.mFloat = y;
1667 mImpl->mEventData->mEventQueue.push_back( event );
1670 case LEFT_SELECTION_HANDLE:
1672 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
1673 event.p1.mUint = state;
1674 event.p2.mFloat = x;
1675 event.p3.mFloat = y;
1677 mImpl->mEventData->mEventQueue.push_back( event );
1680 case RIGHT_SELECTION_HANDLE:
1682 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
1683 event.p1.mUint = state;
1684 event.p2.mFloat = x;
1685 event.p3.mFloat = y;
1687 mImpl->mEventData->mEventQueue.push_back( event );
1690 case LEFT_SELECTION_HANDLE_MARKER:
1691 case RIGHT_SELECTION_HANDLE_MARKER:
1693 // Markers do not move the handles.
1696 case HANDLE_TYPE_COUNT:
1698 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
1702 mImpl->RequestRelayout();
1706 void Controller::PasteText( const std::string& stringToPaste )
1708 InsertText( stringToPaste, Text::Controller::COMMIT );
1709 mImpl->ChangeState( EventData::EDITING );
1710 mImpl->RequestRelayout();
1713 void Controller::PasteClipboardItemEvent()
1715 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
1716 std::string stringToPaste( notifier.GetContent() );
1717 PasteText( stringToPaste );
1720 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
1722 if( NULL == mImpl->mEventData )
1729 case Toolkit::TextSelectionPopup::CUT:
1731 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
1732 mImpl->mOperationsPending = ALL_OPERATIONS;
1734 // This is to reset the virtual keyboard to Upper-case
1735 if( 0u == mImpl->mLogicalModel->mText.Count() )
1740 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1741 !mImpl->IsPlaceholderAvailable() )
1743 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1747 ShowPlaceholderText();
1748 mImpl->mEventData->mUpdateCursorPosition = true;
1750 mImpl->RequestRelayout();
1751 mImpl->mControlInterface.TextChanged();
1754 case Toolkit::TextSelectionPopup::COPY:
1756 mImpl->SendSelectionToClipboard( false ); // Text not modified
1757 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
1760 case Toolkit::TextSelectionPopup::PASTE:
1762 std::string stringToPaste("");
1763 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
1764 PasteText( stringToPaste );
1767 case Toolkit::TextSelectionPopup::SELECT:
1769 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
1771 if( mImpl->mEventData->mSelectionEnabled )
1773 // Creates a SELECT event.
1774 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
1778 case Toolkit::TextSelectionPopup::SELECT_ALL:
1780 // Creates a SELECT_ALL event
1781 SelectEvent( 0.f, 0.f, true );
1784 case Toolkit::TextSelectionPopup::CLIPBOARD:
1786 mImpl->ShowClipboard();
1789 case Toolkit::TextSelectionPopup::NONE:
1797 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
1799 bool update( false );
1800 bool requestRelayout = false;
1803 unsigned int cursorPosition( 0 );
1805 switch ( imfEvent.eventName )
1807 case ImfManager::COMMIT:
1809 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
1810 requestRelayout = true;
1813 case ImfManager::PREEDIT:
1815 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
1817 requestRelayout = true;
1820 case ImfManager::DELETESURROUNDING:
1822 update = RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
1826 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1827 !mImpl->IsPlaceholderAvailable() )
1829 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1833 ShowPlaceholderText();
1834 mImpl->mEventData->mUpdateCursorPosition = true;
1837 requestRelayout = true;
1840 case ImfManager::GETSURROUNDING:
1843 cursorPosition = GetLogicalCursorPosition();
1845 imfManager.SetSurroundingText( text );
1846 imfManager.SetCursorPosition( cursorPosition );
1849 case ImfManager::VOID:
1856 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
1859 cursorPosition = GetLogicalCursorPosition();
1862 if( requestRelayout )
1864 mImpl->mOperationsPending = ALL_OPERATIONS;
1865 mImpl->RequestRelayout();
1867 // Do this last since it provides callbacks into application code
1868 mImpl->mControlInterface.TextChanged();
1871 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
1873 return callbackData;
1876 Controller::~Controller()
1881 bool Controller::BackspaceKeyEvent()
1883 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
1885 // IMF manager is no longer handling key-events
1886 mImpl->ClearPreEditFlag();
1888 bool removed( false );
1890 if ( EventData::SELECTING == mImpl->mEventData->mState ||
1891 EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
1893 removed = RemoveSelectedText();
1895 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
1897 // Remove the character before the current cursor position
1898 removed = RemoveText( -1, 1 );
1903 // This is to reset the virtual keyboard to Upper-case
1904 if( 0u == mImpl->mLogicalModel->mText.Count() )
1909 if( 0u != mImpl->mLogicalModel->mText.Count() ||
1910 !mImpl->IsPlaceholderAvailable() )
1912 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1916 ShowPlaceholderText();
1917 mImpl->mEventData->mUpdateCursorPosition = true;
1924 void Controller::NotifyImfManager()
1926 ImfManager imfManager = ImfManager::Get();
1930 // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
1933 imfManager.SetSurroundingText( text );
1935 imfManager.SetCursorPosition( GetLogicalCursorPosition() );
1936 imfManager.NotifyCursorPosition();
1940 void Controller::ShowPlaceholderText()
1942 if( mImpl->IsPlaceholderAvailable() )
1944 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
1946 mImpl->mEventData->mIsShowingPlaceholderText = true;
1948 // Disable handles when showing place-holder text
1949 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
1950 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
1951 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
1953 const char* text( NULL );
1956 // TODO - Switch placeholder text styles when changing state
1957 if( EventData::INACTIVE != mImpl->mEventData->mState &&
1958 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() )
1960 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
1961 size = mImpl->mEventData->mPlaceholderTextActive.size();
1965 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
1966 size = mImpl->mEventData->mPlaceholderTextInactive.size();
1969 // Reset model for showing placeholder.
1970 mImpl->mLogicalModel->mText.Clear();
1972 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
1974 // Convert text into UTF-32
1975 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
1976 utf32Characters.Resize( size );
1978 // This is a bit horrible but std::string returns a (signed) char*
1979 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
1981 // Transform a text array encoded in utf8 into an array encoded in utf32.
1982 // It returns the actual number of characters.
1983 Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
1984 utf32Characters.Resize( characterCount );
1986 // Reset the cursor position
1987 mImpl->mEventData->mPrimaryCursorPosition = 0;
1989 // The natural size needs to be re-calculated.
1990 mImpl->mRecalculateNaturalSize = true;
1992 // Apply modifications to the model
1993 mImpl->mOperationsPending = ALL_OPERATIONS;
1995 // Update the rest of the model during size negotiation
1996 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2000 void Controller::ClearModelData()
2002 // n.b. This does not Clear the mText from mLogicalModel
2003 mImpl->mLogicalModel->mScriptRuns.Clear();
2004 mImpl->mLogicalModel->mFontRuns.Clear();
2005 mImpl->mLogicalModel->mLineBreakInfo.Clear();
2006 mImpl->mLogicalModel->mWordBreakInfo.Clear();
2007 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
2008 mImpl->mLogicalModel->mCharacterDirections.Clear();
2009 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
2010 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
2011 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
2012 mImpl->mVisualModel->mGlyphs.Clear();
2013 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2014 mImpl->mVisualModel->mCharactersToGlyph.Clear();
2015 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2016 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2017 mImpl->mVisualModel->mGlyphPositions.Clear();
2018 mImpl->mVisualModel->mLines.Clear();
2019 mImpl->mVisualModel->ClearCaches();
2022 void Controller::ClearFontData()
2024 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2025 mImpl->mLogicalModel->mFontRuns.Clear();
2026 mImpl->mVisualModel->mGlyphs.Clear();
2027 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2028 mImpl->mVisualModel->mCharactersToGlyph.Clear();
2029 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2030 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2031 mImpl->mVisualModel->mGlyphPositions.Clear();
2032 mImpl->mVisualModel->mLines.Clear();
2033 mImpl->mVisualModel->ClearCaches();
2036 Controller::Controller( ControlInterface& controlInterface )
2039 mImpl = new Controller::Impl( controlInterface );
2044 } // namespace Toolkit