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>
24 #include <dali/public-api/adaptor-framework/key.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/devel-api/adaptor-framework/clipboard-event-notifier.h>
29 #include <dali-toolkit/internal/text/bidirectional-support.h>
30 #include <dali-toolkit/internal/text/character-set-conversion.h>
31 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
32 #include <dali-toolkit/internal/text/markup-processor.h>
33 #include <dali-toolkit/internal/text/text-controller-impl.h>
38 #if defined(DEBUG_ENABLED)
39 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
42 const float MAX_FLOAT = std::numeric_limits<float>::max();
43 const unsigned int POINTS_PER_INCH = 72;
45 const std::string EMPTY_STRING("");
46 const unsigned int ZERO = 0u;
48 float ConvertToEven( float value )
50 int intValue(static_cast<int>( value ));
51 return static_cast<float>(intValue % 2 == 0) ? intValue : (intValue + 1);
66 * @brief Adds a new font description run for the selected text.
68 * The new font parameters are added after the call to this method.
70 * @param[in] eventData The event data pointer.
71 * @param[in] logicalModel The logical model where to add the new font description run.
73 FontDescriptionRun& UpdateSelectionFontStyleRun( EventData* eventData,
74 LogicalModelPtr logicalModel )
76 const bool handlesCrossed = eventData->mLeftSelectionPosition > eventData->mRightSelectionPosition;
78 // Get start and end position of selection
79 const CharacterIndex startOfSelectedText = handlesCrossed ? eventData->mRightSelectionPosition : eventData->mLeftSelectionPosition;
80 const Length lengthOfSelectedText = ( handlesCrossed ? eventData->mLeftSelectionPosition : eventData->mRightSelectionPosition ) - startOfSelectedText;
83 const VectorBase::SizeType numberOfRuns = logicalModel->mFontDescriptionRuns.Count();
84 logicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
86 FontDescriptionRun& fontDescriptionRun = *( logicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
88 fontDescriptionRun.characterRun.characterIndex = startOfSelectedText;
89 fontDescriptionRun.characterRun.numberOfCharacters = lengthOfSelectedText;
91 // Recalculate the selection highlight as the metrics may have changed.
92 eventData->mUpdateLeftSelectionPosition = true;
93 eventData->mUpdateRightSelectionPosition = true;
95 return fontDescriptionRun;
98 ControllerPtr Controller::New( ControlInterface& controlInterface )
100 return ControllerPtr( new Controller( controlInterface ) );
103 void Controller::EnableTextInput( DecoratorPtr decorator )
105 if( NULL == mImpl->mEventData )
107 mImpl->mEventData = new EventData( decorator );
111 void Controller::SetMarkupProcessorEnabled( bool enable )
113 mImpl->mMarkupProcessorEnabled = enable;
116 bool Controller::IsMarkupProcessorEnabled() const
118 return mImpl->mMarkupProcessorEnabled;
121 void Controller::SetText( const std::string& text )
123 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
125 // Reset keyboard as text changed
126 mImpl->ResetImfManager();
128 // Remove the previously set text and style.
134 CharacterIndex lastCursorIndex = 0u;
136 if( NULL != mImpl->mEventData )
138 // If popup shown then hide it by switching to Editing state
139 if( ( EventData::SELECTING == mImpl->mEventData->mState ) ||
140 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
141 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
142 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
144 mImpl->ChangeState( EventData::EDITING );
150 MarkupProcessData markupProcessData( mImpl->mLogicalModel->mColorRuns,
151 mImpl->mLogicalModel->mFontDescriptionRuns );
153 Length textSize = 0u;
154 const uint8_t* utf8 = NULL;
155 if( mImpl->mMarkupProcessorEnabled )
157 ProcessMarkupString( text, markupProcessData );
158 textSize = markupProcessData.markupProcessedText.size();
160 // This is a bit horrible but std::string returns a (signed) char*
161 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
165 textSize = text.size();
167 // This is a bit horrible but std::string returns a (signed) char*
168 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
171 // Convert text into UTF-32
172 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
173 utf32Characters.Resize( textSize );
175 // Transform a text array encoded in utf8 into an array encoded in utf32.
176 // It returns the actual number of characters.
177 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
178 utf32Characters.Resize( characterCount );
180 DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
181 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mLogicalModel->mText.Count() );
183 // To reset the cursor position
184 lastCursorIndex = characterCount;
186 // Update the rest of the model during size negotiation
187 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
189 // The natural size needs to be re-calculated.
190 mImpl->mRecalculateNaturalSize = true;
192 // Apply modifications to the model
193 mImpl->mOperationsPending = ALL_OPERATIONS;
197 ShowPlaceholderText();
200 // Resets the cursor position.
201 ResetCursorPosition( lastCursorIndex );
203 // Scrolls the text to make the cursor visible.
204 ResetScrollPosition();
206 mImpl->RequestRelayout();
208 if( NULL != mImpl->mEventData )
210 // Cancel previously queued events
211 mImpl->mEventData->mEventQueue.clear();
214 // Notify IMF as text changed
217 // Do this last since it provides callbacks into application code
218 mImpl->mControlInterface.TextChanged();
221 void Controller::GetText( std::string& text ) const
223 if( !mImpl->IsShowingPlaceholderText() )
225 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
227 if( 0u != utf32Characters.Count() )
229 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
234 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
238 unsigned int Controller::GetLogicalCursorPosition() const
240 if( NULL != mImpl->mEventData )
242 return mImpl->mEventData->mPrimaryCursorPosition;
248 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
250 if( NULL != mImpl->mEventData )
252 if( PLACEHOLDER_TYPE_INACTIVE == type )
254 mImpl->mEventData->mPlaceholderTextInactive = text;
258 mImpl->mEventData->mPlaceholderTextActive = text;
261 // Update placeholder if there is no text
262 if( mImpl->IsShowingPlaceholderText() ||
263 ( 0u == mImpl->mLogicalModel->mText.Count() ) )
265 ShowPlaceholderText();
270 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
272 if( NULL != mImpl->mEventData )
274 if( PLACEHOLDER_TYPE_INACTIVE == type )
276 text = mImpl->mEventData->mPlaceholderTextInactive;
280 text = mImpl->mEventData->mPlaceholderTextActive;
285 void Controller::SetMaximumNumberOfCharacters( int maxCharacters )
287 if( maxCharacters >= 0 )
289 mImpl->mMaximumNumberOfCharacters = maxCharacters;
293 int Controller::GetMaximumNumberOfCharacters()
295 return mImpl->mMaximumNumberOfCharacters;
298 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
300 if( NULL == mImpl->mFontDefaults )
302 mImpl->mFontDefaults = new FontDefaults();
305 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
306 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
307 mImpl->mFontDefaults->familyDefined = true;
309 // Clear the font-specific data
312 mImpl->mOperationsPending = ALL_OPERATIONS;
313 mImpl->mRecalculateNaturalSize = true;
315 mImpl->RequestRelayout();
318 const std::string& Controller::GetDefaultFontFamily() const
320 if( NULL != mImpl->mFontDefaults )
322 return mImpl->mFontDefaults->mFontDescription.family;
328 void Controller::SetDefaultFontStyle( const std::string& style )
330 if( NULL == mImpl->mFontDefaults )
332 mImpl->mFontDefaults = new FontDefaults();
335 mImpl->mFontDefaults->mFontStyle = style;
338 const std::string& Controller::GetDefaultFontStyle() const
340 if( NULL != mImpl->mFontDefaults )
342 return mImpl->mFontDefaults->mFontStyle;
348 void Controller::SetDefaultFontWeight( FontWeight weight )
350 if( NULL == mImpl->mFontDefaults )
352 mImpl->mFontDefaults = new FontDefaults();
355 mImpl->mFontDefaults->mFontDescription.weight = weight;
356 mImpl->mFontDefaults->weightDefined = true;
358 // Clear the font-specific data
361 mImpl->mOperationsPending = ALL_OPERATIONS;
362 mImpl->mRecalculateNaturalSize = true;
364 mImpl->RequestRelayout();
367 FontWeight Controller::GetDefaultFontWeight() const
369 if( NULL != mImpl->mFontDefaults )
371 return mImpl->mFontDefaults->mFontDescription.weight;
374 return TextAbstraction::FontWeight::NORMAL;
377 void Controller::SetDefaultFontWidth( FontWidth width )
379 if( NULL == mImpl->mFontDefaults )
381 mImpl->mFontDefaults = new FontDefaults();
384 mImpl->mFontDefaults->mFontDescription.width = width;
385 mImpl->mFontDefaults->widthDefined = true;
387 // Clear the font-specific data
390 mImpl->mOperationsPending = ALL_OPERATIONS;
391 mImpl->mRecalculateNaturalSize = true;
393 mImpl->RequestRelayout();
396 FontWidth Controller::GetDefaultFontWidth() const
398 if( NULL != mImpl->mFontDefaults )
400 return mImpl->mFontDefaults->mFontDescription.width;
403 return TextAbstraction::FontWidth::NORMAL;
406 void Controller::SetDefaultFontSlant( FontSlant slant )
408 if( NULL == mImpl->mFontDefaults )
410 mImpl->mFontDefaults = new FontDefaults();
413 mImpl->mFontDefaults->mFontDescription.slant = slant;
414 mImpl->mFontDefaults->slantDefined = true;
416 // Clear the font-specific data
419 mImpl->mOperationsPending = ALL_OPERATIONS;
420 mImpl->mRecalculateNaturalSize = true;
422 mImpl->RequestRelayout();
425 FontSlant Controller::GetDefaultFontSlant() const
427 if( NULL != mImpl->mFontDefaults )
429 return mImpl->mFontDefaults->mFontDescription.slant;
432 return TextAbstraction::FontSlant::NORMAL;
435 void Controller::SetDefaultPointSize( float pointSize )
437 if( NULL == mImpl->mFontDefaults )
439 mImpl->mFontDefaults = new FontDefaults();
442 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
443 mImpl->mFontDefaults->sizeDefined = true;
445 unsigned int horizontalDpi( 0u );
446 unsigned int verticalDpi( 0u );
447 mImpl->mFontClient.GetDpi( horizontalDpi, verticalDpi );
449 // Adjust the metrics if the fixed-size font should be down-scaled
450 int maxEmojiSize( pointSize/POINTS_PER_INCH * verticalDpi );
451 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultPointSize %p setting MaxEmojiSize %d\n", this, maxEmojiSize );
452 mImpl->mMetrics->SetMaxEmojiSize( maxEmojiSize );
454 // Clear the font-specific data
457 mImpl->mOperationsPending = ALL_OPERATIONS;
458 mImpl->mRecalculateNaturalSize = true;
460 mImpl->RequestRelayout();
463 float Controller::GetDefaultPointSize() const
465 if( NULL != mImpl->mFontDefaults )
467 return mImpl->mFontDefaults->mDefaultPointSize;
473 void Controller::UpdateAfterFontChange( std::string& newDefaultFont )
475 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange");
477 if( !mImpl->mFontDefaults->familyDefined ) // If user defined font then should not update when system font changes
479 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
481 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
482 mImpl->UpdateModel( ALL_OPERATIONS );
483 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
484 mImpl->mRecalculateNaturalSize = true;
485 mImpl->RequestRelayout();
489 void Controller::SetTextColor( const Vector4& textColor )
491 mImpl->mTextColor = textColor;
493 if( !mImpl->IsShowingPlaceholderText() )
495 mImpl->mVisualModel->SetTextColor( textColor );
497 mImpl->RequestRelayout();
501 const Vector4& Controller::GetTextColor() const
503 return mImpl->mTextColor;
506 bool Controller::RemoveText( int cursorOffset, int numberOfChars )
508 bool removed = false;
510 if( NULL == mImpl->mEventData )
515 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfChars %d\n",
516 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfChars );
518 if( !mImpl->IsShowingPlaceholderText() )
520 // Delete at current cursor position
521 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
522 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
524 CharacterIndex cursorIndex = oldCursorIndex;
526 // Validate the cursor position & number of characters
527 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
529 cursorIndex = oldCursorIndex + cursorOffset;
532 if( ( cursorIndex + numberOfChars ) > currentText.Count() )
534 numberOfChars = currentText.Count() - cursorIndex;
537 if( ( cursorIndex + numberOfChars ) <= currentText.Count() )
539 // Update the input style and remove the text's style before removing the text.
541 // Set first the default input style.
542 mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
544 // Update the input style.
545 mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
547 // Remove the text's style before removing the text.
548 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfChars );
550 // Remove the characters.
551 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
552 Vector<Character>::Iterator last = first + numberOfChars;
554 currentText.Erase( first, last );
556 // Cursor position retreat
557 oldCursorIndex = cursorIndex;
559 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfChars );
567 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
569 if( NULL != mImpl->mEventData )
571 mImpl->mEventData->mPlaceholderTextColor = textColor;
574 if( mImpl->IsShowingPlaceholderText() )
576 mImpl->mVisualModel->SetTextColor( textColor );
577 mImpl->RequestRelayout();
581 const Vector4& Controller::GetPlaceholderTextColor() const
583 if( NULL != mImpl->mEventData )
585 return mImpl->mEventData->mPlaceholderTextColor;
591 void Controller::SetShadowOffset( const Vector2& shadowOffset )
593 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
595 mImpl->RequestRelayout();
598 const Vector2& Controller::GetShadowOffset() const
600 return mImpl->mVisualModel->GetShadowOffset();
603 void Controller::SetShadowColor( const Vector4& shadowColor )
605 mImpl->mVisualModel->SetShadowColor( shadowColor );
607 mImpl->RequestRelayout();
610 const Vector4& Controller::GetShadowColor() const
612 return mImpl->mVisualModel->GetShadowColor();
615 void Controller::SetUnderlineColor( const Vector4& color )
617 mImpl->mVisualModel->SetUnderlineColor( color );
619 mImpl->RequestRelayout();
622 const Vector4& Controller::GetUnderlineColor() const
624 return mImpl->mVisualModel->GetUnderlineColor();
627 void Controller::SetUnderlineEnabled( bool enabled )
629 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
631 mImpl->RequestRelayout();
634 bool Controller::IsUnderlineEnabled() const
636 return mImpl->mVisualModel->IsUnderlineEnabled();
639 void Controller::SetUnderlineHeight( float height )
641 mImpl->mVisualModel->SetUnderlineHeight( height );
643 mImpl->RequestRelayout();
646 float Controller::GetUnderlineHeight() const
648 return mImpl->mVisualModel->GetUnderlineHeight();
651 void Controller::SetInputColor( const Vector4& color )
653 if( NULL != mImpl->mEventData )
655 mImpl->mEventData->mInputStyle.textColor = color;
657 if( EventData::SELECTING == mImpl->mEventData->mState )
659 const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
661 // Get start and end position of selection
662 const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
663 const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
665 // Add the color run.
666 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
667 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
669 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
670 colorRun.color = color;
671 colorRun.characterRun.characterIndex = startOfSelectedText;
672 colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
674 // Request to relayout.
675 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
676 mImpl->RequestRelayout();
681 const Vector4& Controller::GetInputColor() const
683 if( NULL != mImpl->mEventData )
685 return mImpl->mEventData->mInputStyle.textColor;
688 // Return the default text's color if there is no EventData.
689 return mImpl->mTextColor;
693 void Controller::SetInputFontFamily( const std::string& fontFamily )
695 if( NULL != mImpl->mEventData )
697 mImpl->mEventData->mInputStyle.familyName = fontFamily;
698 mImpl->mEventData->mInputStyle.familyDefined = true;
700 if( EventData::SELECTING == mImpl->mEventData->mState )
702 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
703 mImpl->mLogicalModel );
705 fontDescriptionRun.familyLength = fontFamily.size();
706 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
707 memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
708 fontDescriptionRun.familyDefined = true;
710 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
712 // Request to relayout.
713 mImpl->mOperationsPending = ALL_OPERATIONS;
714 mImpl->mRecalculateNaturalSize = true;
715 mImpl->RequestRelayout();
717 // As the font changes, recalculate the handle positions is needed.
718 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
719 mImpl->mEventData->mUpdateRightSelectionPosition = true;
720 mImpl->mEventData->mScrollAfterUpdatePosition = true;
725 const std::string& Controller::GetInputFontFamily() const
727 if( NULL != mImpl->mEventData )
729 return mImpl->mEventData->mInputStyle.familyName;
732 // Return the default font's family if there is no EventData.
733 return GetDefaultFontFamily();
736 void Controller::SetInputFontStyle( const std::string& fontStyle )
738 if( NULL != mImpl->mEventData )
740 mImpl->mEventData->mInputStyle.fontStyle = fontStyle;
744 const std::string& Controller::GetInputFontStyle() const
746 if( NULL != mImpl->mEventData )
748 return mImpl->mEventData->mInputStyle.fontStyle;
751 // Return the default font's style if there is no EventData.
752 return GetDefaultFontStyle();
755 void Controller::SetInputFontWeight( FontWeight weight )
757 if( NULL != mImpl->mEventData )
759 mImpl->mEventData->mInputStyle.weight = weight;
760 mImpl->mEventData->mInputStyle.weightDefined = true;
762 if( EventData::SELECTING == mImpl->mEventData->mState )
764 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
765 mImpl->mLogicalModel );
767 fontDescriptionRun.weight = weight;
768 fontDescriptionRun.weightDefined = true;
770 // Request to relayout.
771 mImpl->mOperationsPending = ALL_OPERATIONS;
772 mImpl->mRecalculateNaturalSize = true;
773 mImpl->RequestRelayout();
775 // As the font might change, recalculate the handle positions is needed.
776 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
777 mImpl->mEventData->mUpdateRightSelectionPosition = true;
778 mImpl->mEventData->mScrollAfterUpdatePosition = true;
783 FontWeight Controller::GetInputFontWeight() const
785 if( NULL != mImpl->mEventData )
787 return mImpl->mEventData->mInputStyle.weight;
790 return GetDefaultFontWeight();
793 void Controller::SetInputFontWidth( FontWidth width )
795 if( NULL != mImpl->mEventData )
797 mImpl->mEventData->mInputStyle.width = width;
798 mImpl->mEventData->mInputStyle.widthDefined = true;
800 if( EventData::SELECTING == mImpl->mEventData->mState )
802 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
803 mImpl->mLogicalModel );
805 fontDescriptionRun.width = width;
806 fontDescriptionRun.widthDefined = true;
808 // Request to relayout.
809 mImpl->mOperationsPending = ALL_OPERATIONS;
810 mImpl->mRecalculateNaturalSize = true;
811 mImpl->RequestRelayout();
813 // As the font might change, recalculate the handle positions is needed.
814 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
815 mImpl->mEventData->mUpdateRightSelectionPosition = true;
816 mImpl->mEventData->mScrollAfterUpdatePosition = true;
821 FontWidth Controller::GetInputFontWidth() const
823 if( NULL != mImpl->mEventData )
825 return mImpl->mEventData->mInputStyle.width;
828 return GetDefaultFontWidth();
831 void Controller::SetInputFontSlant( FontSlant slant )
833 if( NULL != mImpl->mEventData )
835 mImpl->mEventData->mInputStyle.slant = slant;
836 mImpl->mEventData->mInputStyle.slantDefined = true;
838 if( EventData::SELECTING == mImpl->mEventData->mState )
840 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
841 mImpl->mLogicalModel );
843 fontDescriptionRun.slant = slant;
844 fontDescriptionRun.slantDefined = true;
846 // Request to relayout.
847 mImpl->mOperationsPending = ALL_OPERATIONS;
848 mImpl->mRecalculateNaturalSize = true;
849 mImpl->RequestRelayout();
851 // As the font might change, recalculate the handle positions is needed.
852 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
853 mImpl->mEventData->mUpdateRightSelectionPosition = true;
854 mImpl->mEventData->mScrollAfterUpdatePosition = true;
859 FontSlant Controller::GetInputFontSlant() const
861 if( NULL != mImpl->mEventData )
863 return mImpl->mEventData->mInputStyle.slant;
866 return GetDefaultFontSlant();
869 void Controller::SetInputFontPointSize( float size )
871 if( NULL != mImpl->mEventData )
873 mImpl->mEventData->mInputStyle.size = size;
875 if( EventData::SELECTING == mImpl->mEventData->mState )
877 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
878 mImpl->mLogicalModel );
880 fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
881 fontDescriptionRun.sizeDefined = true;
883 // Request to relayout.
884 mImpl->mOperationsPending = ALL_OPERATIONS;
885 mImpl->mRecalculateNaturalSize = true;
886 mImpl->RequestRelayout();
888 // As the font might change, recalculate the handle positions is needed.
889 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
890 mImpl->mEventData->mUpdateRightSelectionPosition = true;
891 mImpl->mEventData->mScrollAfterUpdatePosition = true;
896 float Controller::GetInputFontPointSize() const
898 if( NULL != mImpl->mEventData )
900 return mImpl->mEventData->mInputStyle.size;
903 // Return the default font's point size if there is no EventData.
904 return GetDefaultPointSize();
907 void Controller::SetEnableCursorBlink( bool enable )
909 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
911 if( NULL != mImpl->mEventData )
913 mImpl->mEventData->mCursorBlinkEnabled = enable;
916 mImpl->mEventData->mDecorator )
918 mImpl->mEventData->mDecorator->StopCursorBlink();
923 bool Controller::GetEnableCursorBlink() const
925 if( NULL != mImpl->mEventData )
927 return mImpl->mEventData->mCursorBlinkEnabled;
933 const Vector2& Controller::GetScrollPosition() const
935 if( NULL != mImpl->mEventData )
937 return mImpl->mEventData->mScrollPosition;
940 return Vector2::ZERO;
943 const Vector2& Controller::GetAlignmentOffset() const
945 return mImpl->mAlignmentOffset;
948 Vector3 Controller::GetNaturalSize()
950 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
953 // Make sure the model is up-to-date before layouting
954 ProcessModifyEvents();
956 if( mImpl->mRecalculateNaturalSize )
958 // Operations that can be done only once until the text changes.
959 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
967 // Make sure the model is up-to-date before layouting
968 mImpl->UpdateModel( onlyOnceOperations );
970 // Operations that need to be done if the size changes.
971 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
975 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
976 static_cast<OperationsMask>( onlyOnceOperations |
978 naturalSize.GetVectorXY() );
980 // Do not do again the only once operations.
981 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
983 // Do the size related operations again.
984 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
986 // Stores the natural size to avoid recalculate it again
987 // unless the text/style changes.
988 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
990 mImpl->mRecalculateNaturalSize = false;
992 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
996 naturalSize = mImpl->mVisualModel->GetNaturalSize();
998 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1001 naturalSize.x = ConvertToEven( naturalSize.x );
1002 naturalSize.y = ConvertToEven( naturalSize.y );
1007 float Controller::GetHeightForWidth( float width )
1009 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1010 // Make sure the model is up-to-date before layouting
1011 ProcessModifyEvents();
1014 if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
1016 // Operations that can be done only once until the text changes.
1017 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1024 GET_GLYPH_METRICS );
1025 // Make sure the model is up-to-date before layouting
1026 mImpl->UpdateModel( onlyOnceOperations );
1028 // Operations that need to be done if the size changes.
1029 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1033 DoRelayout( Size( width, MAX_FLOAT ),
1034 static_cast<OperationsMask>( onlyOnceOperations |
1038 // Do not do again the only once operations.
1039 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1041 // Do the size related operations again.
1042 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1043 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1047 layoutSize = mImpl->mVisualModel->GetActualSize();
1048 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1051 return layoutSize.height;
1054 bool Controller::Relayout( const Size& size )
1056 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
1058 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1060 bool glyphsRemoved( false );
1061 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1063 mImpl->mVisualModel->mGlyphPositions.Clear();
1064 glyphsRemoved = true;
1066 // Not worth to relayout if width or height is equal to zero.
1067 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1068 return glyphsRemoved;
1071 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1075 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1077 // Layout operations that need to be done if the size changes.
1078 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1081 UPDATE_ACTUAL_SIZE |
1084 mImpl->mVisualModel->mControlSize = size;
1087 // Whether there are modify events.
1088 const bool isModifyEventsEmpty = 0u == mImpl->mModifyEvents.Count();
1090 // Make sure the model is up-to-date before layouting.
1091 ProcessModifyEvents();
1092 mImpl->UpdateModel( mImpl->mOperationsPending );
1094 // Style operations that need to be done if the text is modified.
1095 if( !isModifyEventsEmpty )
1097 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1101 // Apply the style runs if text is modified.
1102 bool updated = mImpl->UpdateModelStyle( mImpl->mOperationsPending );
1106 updated = DoRelayout( mImpl->mVisualModel->mControlSize,
1107 mImpl->mOperationsPending,
1108 layoutSize ) || updated;
1110 // Do not re-do any operation until something changes.
1111 mImpl->mOperationsPending = NO_OPERATION;
1113 // Whether the text control is editable
1114 const bool isEditable = NULL != mImpl->mEventData;
1116 // Keep the current offset and alignment as it will be used to update the decorator's positions (if the size changes).
1118 if( newSize && isEditable )
1120 offset = mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition;
1123 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
1124 CalculateTextAlignment( size );
1130 // If there is a new size, the scroll position needs to be clamped.
1131 mImpl->ClampHorizontalScroll( layoutSize );
1133 // Update the decorator's positions is needed if there is a new size.
1134 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
1137 // Move the cursor, grab handle etc.
1138 updated = mImpl->ProcessInputEvents() || updated;
1141 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1145 void Controller::ProcessModifyEvents()
1147 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
1149 if( 0u == events.Count() )
1155 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
1156 endIt = events.End();
1160 const ModifyEvent& event = *it;
1162 if( ModifyEvent::TEXT_REPLACED == event.type )
1164 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
1165 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
1167 TextReplacedEvent();
1169 else if( ModifyEvent::TEXT_INSERTED == event.type )
1171 TextInsertedEvent();
1173 else if( ModifyEvent::TEXT_DELETED == event.type )
1175 // Placeholder-text cannot be deleted
1176 if( !mImpl->IsShowingPlaceholderText() )
1183 if( NULL != mImpl->mEventData )
1185 // When the text is being modified, delay cursor blinking
1186 mImpl->mEventData->mDecorator->DelayCursorBlink();
1189 // Discard temporary text
1193 void Controller::ResetText()
1196 mImpl->mLogicalModel->mText.Clear();
1199 // We have cleared everything including the placeholder-text
1200 mImpl->PlaceholderCleared();
1202 // The natural size needs to be re-calculated.
1203 mImpl->mRecalculateNaturalSize = true;
1205 // Apply modifications to the model
1206 mImpl->mOperationsPending = ALL_OPERATIONS;
1209 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
1211 // Reset the cursor position
1212 if( NULL != mImpl->mEventData )
1214 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
1216 // Update the cursor if it's in editing mode.
1217 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1219 mImpl->mEventData->mUpdateCursorPosition = true;
1224 void Controller::ResetScrollPosition()
1226 if( NULL != mImpl->mEventData )
1228 // Reset the scroll position.
1229 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
1230 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1234 void Controller::TextReplacedEvent()
1239 // The natural size needs to be re-calculated.
1240 mImpl->mRecalculateNaturalSize = true;
1242 // Apply modifications to the model
1243 mImpl->mOperationsPending = ALL_OPERATIONS;
1244 mImpl->UpdateModel( ALL_OPERATIONS );
1245 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
1247 UPDATE_ACTUAL_SIZE |
1251 void Controller::TextInsertedEvent()
1253 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
1255 if( NULL == mImpl->mEventData )
1260 // TODO - Optimize this
1263 // The natural size needs to be re-calculated.
1264 mImpl->mRecalculateNaturalSize = true;
1266 // Apply modifications to the model; TODO - Optimize this
1267 mImpl->mOperationsPending = ALL_OPERATIONS;
1268 mImpl->UpdateModel( ALL_OPERATIONS );
1269 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
1271 UPDATE_ACTUAL_SIZE |
1274 // Queue a cursor reposition event; this must wait until after DoRelayout()
1275 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1277 mImpl->mEventData->mUpdateCursorPosition = true;
1278 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1282 void Controller::TextDeletedEvent()
1284 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
1286 if( NULL == mImpl->mEventData )
1291 // TODO - Optimize this
1294 // The natural size needs to be re-calculated.
1295 mImpl->mRecalculateNaturalSize = true;
1297 // Apply modifications to the model; TODO - Optimize this
1298 mImpl->mOperationsPending = ALL_OPERATIONS;
1299 mImpl->UpdateModel( ALL_OPERATIONS );
1300 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
1302 UPDATE_ACTUAL_SIZE |
1305 // Queue a cursor reposition event; this must wait until after DoRelayout()
1306 mImpl->mEventData->mUpdateCursorPosition = true;
1307 if( 0u != mImpl->mLogicalModel->mText.Count() )
1309 mImpl->mEventData->mScrollAfterDelete = true;
1313 bool Controller::DoRelayout( const Size& size,
1314 OperationsMask operationsRequired,
1317 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
1318 bool viewUpdated( false );
1320 // Calculate the operations to be done.
1321 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
1323 if( LAYOUT & operations )
1325 // Some vectors with data needed to layout and reorder may be void
1326 // after the first time the text has been laid out.
1327 // Fill the vectors again.
1329 const Length numberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
1331 if( 0u == numberOfGlyphs )
1333 // Nothing else to do if there is no glyphs.
1334 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1338 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1339 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1340 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1341 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1342 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1343 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1344 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1346 // Set the layout parameters.
1347 LayoutParameters layoutParameters( size,
1349 lineBreakInfo.Begin(),
1350 wordBreakInfo.Begin(),
1351 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1354 glyphsToCharactersMap.Begin(),
1355 charactersPerGlyph.Begin() );
1357 // The laid-out lines.
1358 // It's not possible to know in how many lines the text is going to be laid-out,
1359 // but it can be resized at least with the number of 'paragraphs' to avoid
1360 // some re-allocations.
1361 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1363 // Delete any previous laid out lines before setting the new ones.
1366 // The capacity of the bidirectional paragraph info is the number of paragraphs.
1367 lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
1369 // Resize the vector of positions to have the same size than the vector of glyphs.
1370 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1371 glyphPositions.Resize( numberOfGlyphs );
1373 // Whether the last character is a new paragraph character.
1374 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1376 // Update the visual model.
1377 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1384 // Reorder the lines
1385 if( REORDER & operations )
1387 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1389 // Check first if there are paragraphs with bidirectional info.
1390 if( 0u != bidirectionalInfo.Count() )
1393 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1395 // Reorder the lines.
1396 Vector<BidirectionalLineInfoRun> lineBidirectionalInfoRuns;
1397 lineBidirectionalInfoRuns.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1398 ReorderLines( bidirectionalInfo,
1400 lineBidirectionalInfoRuns );
1402 // Set the bidirectional info into the model.
1403 const Length numberOfBidirectionalInfoRuns = lineBidirectionalInfoRuns.Count();
1404 mImpl->mLogicalModel->SetVisualToLogicalMap( lineBidirectionalInfoRuns.Begin(),
1405 numberOfBidirectionalInfoRuns );
1407 // Set the bidirectional info per line into the layout parameters.
1408 layoutParameters.lineBidirectionalInfoRunsBuffer = lineBidirectionalInfoRuns.Begin();
1409 layoutParameters.numberOfBidirectionalInfoRuns = numberOfBidirectionalInfoRuns;
1411 // Get the character to glyph conversion table and set into the layout.
1412 layoutParameters.charactersToGlyphsBuffer = mImpl->mVisualModel->mCharactersToGlyph.Begin();
1414 // Get the glyphs per character table and set into the layout.
1415 layoutParameters.glyphsPerCharacterBuffer = mImpl->mVisualModel->mGlyphsPerCharacter.Begin();
1417 // Re-layout the text. Reorder those lines with right to left characters.
1418 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1421 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
1422 for( Vector<BidirectionalLineInfoRun>::Iterator it = lineBidirectionalInfoRuns.Begin(),
1423 endIt = lineBidirectionalInfoRuns.End();
1427 BidirectionalLineInfoRun& bidiLineInfo = *it;
1429 free( bidiLineInfo.visualToLogicalMap );
1434 // Sets the actual size.
1435 if( UPDATE_ACTUAL_SIZE & operations )
1437 mImpl->mVisualModel->SetActualSize( layoutSize );
1443 layoutSize = mImpl->mVisualModel->GetActualSize();
1446 if( ALIGN & operations )
1448 // The laid-out lines.
1449 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1451 mImpl->mLayoutEngine.Align( layoutSize,
1457 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1461 void Controller::SetMultiLineEnabled( bool enable )
1463 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1465 if( layout != mImpl->mLayoutEngine.GetLayout() )
1467 // Set the layout type.
1468 mImpl->mLayoutEngine.SetLayout( layout );
1470 // Set the flags to redo the layout operations
1471 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1472 UPDATE_ACTUAL_SIZE |
1476 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1478 mImpl->RequestRelayout();
1482 bool Controller::IsMultiLineEnabled() const
1484 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1487 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1489 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1491 // Set the alignment.
1492 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1494 // Set the flag to redo the alignment operation.
1495 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1497 mImpl->RequestRelayout();
1501 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1503 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1506 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1508 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1510 // Set the alignment.
1511 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1513 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1515 mImpl->RequestRelayout();
1519 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1521 return mImpl->mLayoutEngine.GetVerticalAlignment();
1524 void Controller::CalculateTextAlignment( const Size& size )
1526 // Get the direction of the first character.
1527 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1529 Size actualSize = mImpl->mVisualModel->GetActualSize();
1530 if( fabsf( actualSize.height ) < Math::MACHINE_EPSILON_1000 )
1532 // Get the line height of the default font.
1533 actualSize.height = mImpl->GetDefaultFontLineHeight();
1536 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1537 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1538 if( firstParagraphDirection &&
1539 ( LayoutEngine::HORIZONTAL_ALIGN_CENTER != horizontalAlignment ) )
1541 if( LayoutEngine::HORIZONTAL_ALIGN_BEGIN == horizontalAlignment )
1543 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1547 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1551 switch( horizontalAlignment )
1553 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1555 mImpl->mAlignmentOffset.x = 0.f;
1558 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1560 mImpl->mAlignmentOffset.x = floorf( 0.5f * ( size.width - actualSize.width ) ); // try to avoid pixel alignment.
1563 case LayoutEngine::HORIZONTAL_ALIGN_END:
1565 mImpl->mAlignmentOffset.x = size.width - actualSize.width;
1570 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1571 switch( verticalAlignment )
1573 case LayoutEngine::VERTICAL_ALIGN_TOP:
1575 mImpl->mAlignmentOffset.y = 0.f;
1578 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1580 mImpl->mAlignmentOffset.y = floorf( 0.5f * ( size.height - actualSize.height ) ); // try to avoid pixel alignment.
1583 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1585 mImpl->mAlignmentOffset.y = size.height - actualSize.height;
1591 LayoutEngine& Controller::GetLayoutEngine()
1593 return mImpl->mLayoutEngine;
1596 View& Controller::GetView()
1598 return mImpl->mView;
1601 void Controller::KeyboardFocusGainEvent()
1603 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1605 if( NULL != mImpl->mEventData )
1607 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1608 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1610 mImpl->ChangeState( EventData::EDITING );
1611 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1614 if( mImpl->IsShowingPlaceholderText() )
1616 // Show alternative placeholder-text when editing
1617 ShowPlaceholderText();
1620 mImpl->RequestRelayout();
1624 void Controller::KeyboardFocusLostEvent()
1626 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1628 if( NULL != mImpl->mEventData )
1630 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1632 mImpl->ChangeState( EventData::INACTIVE );
1634 if( !mImpl->IsShowingRealText() )
1636 // Revert to regular placeholder-text when not editing
1637 ShowPlaceholderText();
1641 mImpl->RequestRelayout();
1644 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1646 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1648 bool textChanged( false );
1650 if( ( NULL != mImpl->mEventData ) &&
1651 ( keyEvent.state == KeyEvent::Down ) )
1653 int keyCode = keyEvent.keyCode;
1654 const std::string& keyString = keyEvent.keyPressed;
1656 // Pre-process to separate modifying events from non-modifying input events.
1657 if( Dali::DALI_KEY_ESCAPE == keyCode )
1659 // Escape key is a special case which causes focus loss
1660 KeyboardFocusLostEvent();
1662 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
1663 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1664 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
1665 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
1667 Event event( Event::CURSOR_KEY_EVENT );
1668 event.p1.mInt = keyCode;
1669 mImpl->mEventData->mEventQueue.push_back( event );
1671 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1673 textChanged = BackspaceKeyEvent();
1675 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1677 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1678 // Avoids calling the InsertText() method which can delete selected text
1680 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1681 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1683 mImpl->ChangeState( EventData::INACTIVE );
1684 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1685 // Avoids calling the InsertText() method which can delete selected text
1687 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1689 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1690 // and a character is typed after the type of a upper case latin character.
1696 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1698 // IMF manager is no longer handling key-events
1699 mImpl->ClearPreEditFlag();
1701 InsertText( keyString, COMMIT );
1705 if( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1706 ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1708 mImpl->ChangeState( EventData::EDITING );
1711 mImpl->RequestRelayout();
1716 // Do this last since it provides callbacks into application code
1717 mImpl->mControlInterface.TextChanged();
1723 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1725 bool removedPrevious( false );
1726 bool maxLengthReached( false );
1728 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1730 if( NULL == mImpl->mEventData )
1735 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1736 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1737 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1739 // TODO: At the moment the underline runs are only for pre-edit.
1740 mImpl->mVisualModel->mUnderlineRuns.Clear();
1742 Vector<Character> utf32Characters;
1743 Length characterCount( 0u );
1745 // Remove the previous IMF pre-edit (predicitive text)
1746 if( mImpl->mEventData->mPreEditFlag &&
1747 ( 0 != mImpl->mEventData->mPreEditLength ) )
1749 CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1751 removedPrevious = RemoveText( -static_cast<int>(offset), mImpl->mEventData->mPreEditLength );
1753 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1754 mImpl->mEventData->mPreEditLength = 0;
1758 // Remove the previous Selection
1759 removedPrevious = RemoveSelectedText();
1764 // Convert text into UTF-32
1765 utf32Characters.Resize( text.size() );
1767 // This is a bit horrible but std::string returns a (signed) char*
1768 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1770 // Transform a text array encoded in utf8 into an array encoded in utf32.
1771 // It returns the actual number of characters.
1772 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1773 utf32Characters.Resize( characterCount );
1775 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1776 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1779 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1781 // The placeholder text is no longer needed
1782 if( mImpl->IsShowingPlaceholderText() )
1787 mImpl->ChangeState( EventData::EDITING );
1789 // Handle the IMF (predicitive text) state changes
1790 if( COMMIT == type )
1792 // IMF manager is no longer handling key-events
1793 mImpl->ClearPreEditFlag();
1797 if( !mImpl->mEventData->mPreEditFlag )
1799 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1801 // Record the start of the pre-edit text
1802 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1805 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1806 mImpl->mEventData->mPreEditFlag = true;
1808 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1811 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1813 // Restrict new text to fit within Maximum characters setting
1814 Length maxSizeOfNewText = std::min ( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1815 maxLengthReached = ( characterCount > maxSizeOfNewText );
1817 // The cursor position.
1818 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1820 // Update the text's style.
1822 // Updates the text style runs by adding characters.
1823 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
1825 // Get the character index from the cursor index.
1826 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
1828 // Retrieve the text's style for the given index.
1830 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
1832 // Whether to add a new text color run.
1833 const bool addColorRun = style.textColor != mImpl->mEventData->mInputStyle.textColor;
1835 // Whether to add a new font run.
1836 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
1837 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
1838 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
1839 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
1840 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
1845 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
1846 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
1848 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
1849 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
1850 colorRun.characterRun.characterIndex = cursorIndex;
1851 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1854 if( addFontNameRun ||
1860 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
1861 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
1863 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
1865 if( addFontNameRun )
1867 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
1868 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
1869 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
1870 fontDescriptionRun.familyDefined = true;
1872 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
1875 if( addFontWeightRun )
1877 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
1878 fontDescriptionRun.weightDefined = true;
1881 if( addFontWidthRun )
1883 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
1884 fontDescriptionRun.widthDefined = true;
1887 if( addFontSlantRun )
1889 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
1890 fontDescriptionRun.slantDefined = true;
1893 if( addFontSizeRun )
1895 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
1896 fontDescriptionRun.sizeDefined = true;
1899 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
1900 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1903 // Insert at current cursor position.
1904 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1906 if( cursorIndex < numberOfCharactersInModel )
1908 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1912 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1915 cursorIndex += maxSizeOfNewText;
1917 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1920 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
1921 mImpl->IsPlaceholderAvailable() )
1923 // Show place-holder if empty after removing the pre-edit text
1924 ShowPlaceholderText();
1925 mImpl->mEventData->mUpdateCursorPosition = true;
1926 mImpl->ClearPreEditFlag();
1928 else if( removedPrevious ||
1929 ( 0 != utf32Characters.Count() ) )
1931 // Queue an inserted event
1932 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1935 if( maxLengthReached )
1937 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1939 mImpl->ResetImfManager();
1941 // Do this last since it provides callbacks into application code
1942 mImpl->mControlInterface.MaxLengthReached();
1946 bool Controller::RemoveSelectedText()
1948 bool textRemoved( false );
1950 if( EventData::SELECTING == mImpl->mEventData->mState )
1952 std::string removedString;
1953 mImpl->RetrieveSelection( removedString, true );
1955 if( !removedString.empty() )
1958 mImpl->ChangeState( EventData::EDITING );
1965 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1967 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1969 if( NULL != mImpl->mEventData )
1971 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
1973 if( 1u == tapCount )
1975 // This is to avoid unnecessary relayouts when tapping an empty text-field
1976 bool relayoutNeeded( false );
1978 if( ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) ||
1979 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
1981 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE); // If Popup shown hide it here so can be shown again if required.
1984 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
1986 // Already in an active state so show a popup
1987 if( !mImpl->IsClipboardEmpty() )
1989 // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
1990 mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
1994 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
1996 relayoutNeeded = true;
2000 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2002 // Hide placeholder text
2006 if( EventData::INACTIVE == mImpl->mEventData->mState )
2008 mImpl->ChangeState( EventData::EDITING );
2010 else if( !mImpl->IsClipboardEmpty() )
2012 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2014 relayoutNeeded = true;
2017 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2018 if( relayoutNeeded )
2020 Event event( Event::TAP_EVENT );
2021 event.p1.mUint = tapCount;
2022 event.p2.mFloat = x;
2023 event.p3.mFloat = y;
2024 mImpl->mEventData->mEventQueue.push_back( event );
2026 mImpl->RequestRelayout();
2029 else if( 2u == tapCount )
2031 if( mImpl->mEventData->mSelectionEnabled &&
2032 mImpl->IsShowingRealText() )
2034 SelectEvent( x, y, false );
2039 // Reset keyboard as tap event has occurred.
2040 mImpl->ResetImfManager();
2043 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2044 // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2046 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2048 if( NULL != mImpl->mEventData )
2050 Event event( Event::PAN_EVENT );
2051 event.p1.mInt = state;
2052 event.p2.mFloat = displacement.x;
2053 event.p3.mFloat = displacement.y;
2054 mImpl->mEventData->mEventQueue.push_back( event );
2056 mImpl->RequestRelayout();
2060 void Controller::LongPressEvent( Gesture::State state, float x, float y )
2062 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2064 if( ( state == Gesture::Started ) &&
2065 ( NULL != mImpl->mEventData ) )
2067 if( !mImpl->IsShowingRealText() )
2069 Event event( Event::LONG_PRESS_EVENT );
2070 event.p1.mInt = state;
2071 mImpl->mEventData->mEventQueue.push_back( event );
2072 mImpl->RequestRelayout();
2076 // The 1st long-press on inactive text-field is treated as tap
2077 if( EventData::INACTIVE == mImpl->mEventData->mState )
2079 mImpl->ChangeState( EventData::EDITING );
2081 Event event( Event::TAP_EVENT );
2083 event.p2.mFloat = x;
2084 event.p3.mFloat = y;
2085 mImpl->mEventData->mEventQueue.push_back( event );
2087 mImpl->RequestRelayout();
2091 // Reset the imf manger to commit the pre-edit before selecting the text.
2092 mImpl->ResetImfManager();
2094 SelectEvent( x, y, false );
2100 void Controller::SelectEvent( float x, float y, bool selectAll )
2102 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2104 if( NULL != mImpl->mEventData )
2106 mImpl->ChangeState( EventData::SELECTING );
2110 Event event( Event::SELECT_ALL );
2111 mImpl->mEventData->mEventQueue.push_back( event );
2115 Event event( Event::SELECT );
2116 event.p2.mFloat = x;
2117 event.p3.mFloat = y;
2118 mImpl->mEventData->mEventQueue.push_back( event );
2121 mImpl->RequestRelayout();
2125 void Controller::GetTargetSize( Vector2& targetSize )
2127 targetSize = mImpl->mVisualModel->mControlSize;
2130 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2132 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2135 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2137 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2139 if( NULL != mImpl->mEventData )
2141 switch( handleType )
2145 Event event( Event::GRAB_HANDLE_EVENT );
2146 event.p1.mUint = state;
2147 event.p2.mFloat = x;
2148 event.p3.mFloat = y;
2150 mImpl->mEventData->mEventQueue.push_back( event );
2153 case LEFT_SELECTION_HANDLE:
2155 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2156 event.p1.mUint = state;
2157 event.p2.mFloat = x;
2158 event.p3.mFloat = y;
2160 mImpl->mEventData->mEventQueue.push_back( event );
2163 case RIGHT_SELECTION_HANDLE:
2165 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2166 event.p1.mUint = state;
2167 event.p2.mFloat = x;
2168 event.p3.mFloat = y;
2170 mImpl->mEventData->mEventQueue.push_back( event );
2173 case LEFT_SELECTION_HANDLE_MARKER:
2174 case RIGHT_SELECTION_HANDLE_MARKER:
2176 // Markers do not move the handles.
2179 case HANDLE_TYPE_COUNT:
2181 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2185 mImpl->RequestRelayout();
2189 void Controller::PasteText( const std::string& stringToPaste )
2191 InsertText( stringToPaste, Text::Controller::COMMIT );
2192 mImpl->ChangeState( EventData::EDITING );
2193 mImpl->RequestRelayout();
2195 // Do this last since it provides callbacks into application code
2196 mImpl->mControlInterface.TextChanged();
2199 void Controller::PasteClipboardItemEvent()
2201 // Retrieve the clipboard contents first
2202 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2203 std::string stringToPaste( notifier.GetContent() );
2205 // Commit the current pre-edit text; the contents of the clipboard should be appended
2206 mImpl->ResetImfManager();
2209 PasteText( stringToPaste );
2212 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2214 if( NULL == mImpl->mEventData )
2221 case Toolkit::TextSelectionPopup::CUT:
2223 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2224 mImpl->mOperationsPending = ALL_OPERATIONS;
2226 // This is to reset the virtual keyboard to Upper-case
2227 if( 0u == mImpl->mLogicalModel->mText.Count() )
2232 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2233 !mImpl->IsPlaceholderAvailable() )
2235 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2239 ShowPlaceholderText();
2240 mImpl->mEventData->mUpdateCursorPosition = true;
2242 mImpl->RequestRelayout();
2243 mImpl->mControlInterface.TextChanged();
2246 case Toolkit::TextSelectionPopup::COPY:
2248 mImpl->SendSelectionToClipboard( false ); // Text not modified
2249 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
2252 case Toolkit::TextSelectionPopup::PASTE:
2254 std::string stringToPaste("");
2255 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2256 PasteText( stringToPaste );
2259 case Toolkit::TextSelectionPopup::SELECT:
2261 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2263 if( mImpl->mEventData->mSelectionEnabled )
2265 // Creates a SELECT event.
2266 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2270 case Toolkit::TextSelectionPopup::SELECT_ALL:
2272 // Creates a SELECT_ALL event
2273 SelectEvent( 0.f, 0.f, true );
2276 case Toolkit::TextSelectionPopup::CLIPBOARD:
2278 mImpl->ShowClipboard();
2281 case Toolkit::TextSelectionPopup::NONE:
2289 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2291 bool update = false;
2292 bool requestRelayout = false;
2295 unsigned int cursorPosition = 0u;
2297 switch( imfEvent.eventName )
2299 case ImfManager::COMMIT:
2301 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2303 requestRelayout = true;
2306 case ImfManager::PREEDIT:
2308 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2310 requestRelayout = true;
2313 case ImfManager::DELETESURROUNDING:
2315 update = RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
2319 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2320 !mImpl->IsPlaceholderAvailable() )
2322 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2326 ShowPlaceholderText();
2327 mImpl->mEventData->mUpdateCursorPosition = true;
2330 requestRelayout = true;
2333 case ImfManager::GETSURROUNDING:
2336 cursorPosition = GetLogicalCursorPosition();
2338 imfManager.SetSurroundingText( text );
2339 imfManager.SetCursorPosition( cursorPosition );
2342 case ImfManager::VOID:
2349 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
2352 cursorPosition = GetLogicalCursorPosition();
2355 if( requestRelayout )
2357 mImpl->mOperationsPending = ALL_OPERATIONS;
2358 mImpl->RequestRelayout();
2360 // Do this last since it provides callbacks into application code
2361 mImpl->mControlInterface.TextChanged();
2364 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
2366 return callbackData;
2369 Controller::~Controller()
2374 bool Controller::BackspaceKeyEvent()
2376 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2378 bool removed = false;
2380 if( NULL == mImpl->mEventData )
2385 // IMF manager is no longer handling key-events
2386 mImpl->ClearPreEditFlag();
2388 if( EventData::SELECTING == mImpl->mEventData->mState )
2390 removed = RemoveSelectedText();
2392 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2394 // Remove the character before the current cursor position
2395 removed = RemoveText( -1, 1 );
2400 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE RemovedText\n", this );
2401 // Notifiy the IMF manager after text changed
2402 // Automatic Upper-case and restarting prediction on an existing word require this.
2405 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2406 !mImpl->IsPlaceholderAvailable() )
2408 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2412 ShowPlaceholderText();
2413 mImpl->mEventData->mUpdateCursorPosition = true;
2420 void Controller::NotifyImfManager()
2422 if( NULL != mImpl->mEventData )
2424 if( mImpl->mEventData->mImfManager )
2426 // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
2429 mImpl->mEventData->mImfManager.SetSurroundingText( text );
2431 mImpl->mEventData->mImfManager.SetCursorPosition( GetLogicalCursorPosition() );
2432 mImpl->mEventData->mImfManager.NotifyCursorPosition();
2437 void Controller::ShowPlaceholderText()
2439 if( mImpl->IsPlaceholderAvailable() )
2441 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2443 if( NULL == mImpl->mEventData )
2448 mImpl->mEventData->mIsShowingPlaceholderText = true;
2450 // Disable handles when showing place-holder text
2451 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2452 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2453 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2455 const char* text( NULL );
2458 // TODO - Switch placeholder text styles when changing state
2459 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2460 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2462 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2463 size = mImpl->mEventData->mPlaceholderTextActive.size();
2467 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2468 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2471 // Reset model for showing placeholder.
2472 mImpl->mLogicalModel->mText.Clear();
2474 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2476 // Convert text into UTF-32
2477 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2478 utf32Characters.Resize( size );
2480 // This is a bit horrible but std::string returns a (signed) char*
2481 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2483 // Transform a text array encoded in utf8 into an array encoded in utf32.
2484 // It returns the actual number of characters.
2485 Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2486 utf32Characters.Resize( characterCount );
2488 // Reset the cursor position
2489 mImpl->mEventData->mPrimaryCursorPosition = 0;
2491 // The natural size needs to be re-calculated.
2492 mImpl->mRecalculateNaturalSize = true;
2494 // Apply modifications to the model
2495 mImpl->mOperationsPending = ALL_OPERATIONS;
2497 // Update the rest of the model during size negotiation
2498 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2502 void Controller::ClearModelData()
2504 // n.b. This does not Clear the mText from mLogicalModel
2505 mImpl->mLogicalModel->mScriptRuns.Clear();
2506 mImpl->mLogicalModel->mFontRuns.Clear();
2507 mImpl->mLogicalModel->mLineBreakInfo.Clear();
2508 mImpl->mLogicalModel->mWordBreakInfo.Clear();
2509 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
2510 mImpl->mLogicalModel->mCharacterDirections.Clear();
2511 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
2512 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
2513 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
2514 mImpl->mVisualModel->mGlyphs.Clear();
2515 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2516 mImpl->mVisualModel->mCharactersToGlyph.Clear();
2517 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2518 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2519 mImpl->mVisualModel->mGlyphPositions.Clear();
2520 mImpl->mVisualModel->mLines.Clear();
2521 mImpl->mVisualModel->mColorRuns.Clear();
2522 mImpl->mVisualModel->ClearCaches();
2525 void Controller::ClearFontData()
2527 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2528 mImpl->mLogicalModel->mFontRuns.Clear();
2529 mImpl->mVisualModel->mGlyphs.Clear();
2530 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2531 mImpl->mVisualModel->mCharactersToGlyph.Clear();
2532 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2533 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2534 mImpl->mVisualModel->mGlyphPositions.Clear();
2535 mImpl->mVisualModel->mLines.Clear();
2536 mImpl->mVisualModel->ClearCaches();
2539 void Controller::ClearStyleData()
2541 mImpl->mLogicalModel->mColorRuns.Clear();
2542 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2545 Controller::Controller( ControlInterface& controlInterface )
2548 mImpl = new Controller::Impl( controlInterface );
2553 } // namespace Toolkit