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("");
47 float ConvertToEven( float value )
49 int intValue(static_cast<int>( value ));
50 return static_cast<float>(intValue % 2 == 0) ? intValue : (intValue + 1);
65 * @brief Adds a new font description run for the selected text.
67 * The new font parameters are added after the call to this method.
69 * @param[in] eventData The event data pointer.
70 * @param[in] logicalModel The logical model where to add the new font description run.
72 FontDescriptionRun& UpdateSelectionFontStyleRun( EventData* eventData,
73 LogicalModelPtr logicalModel )
75 const bool handlesCrossed = eventData->mLeftSelectionPosition > eventData->mRightSelectionPosition;
77 // Get start and end position of selection
78 const CharacterIndex startOfSelectedText = handlesCrossed ? eventData->mRightSelectionPosition : eventData->mLeftSelectionPosition;
79 const Length lengthOfSelectedText = ( handlesCrossed ? eventData->mLeftSelectionPosition : eventData->mRightSelectionPosition ) - startOfSelectedText;
82 const VectorBase::SizeType numberOfRuns = logicalModel->mFontDescriptionRuns.Count();
83 logicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
85 FontDescriptionRun& fontDescriptionRun = *( logicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
87 fontDescriptionRun.characterRun.characterIndex = startOfSelectedText;
88 fontDescriptionRun.characterRun.numberOfCharacters = lengthOfSelectedText;
90 // Recalculate the selection highlight as the metrics may have changed.
91 eventData->mUpdateLeftSelectionPosition = true;
92 eventData->mUpdateRightSelectionPosition = true;
94 return fontDescriptionRun;
97 ControllerPtr Controller::New( ControlInterface& controlInterface )
99 return ControllerPtr( new Controller( controlInterface ) );
102 void Controller::EnableTextInput( DecoratorPtr decorator )
104 if( NULL == mImpl->mEventData )
106 mImpl->mEventData = new EventData( decorator );
110 void Controller::SetGlyphType( TextAbstraction::GlyphType glyphType )
112 // Metrics for bitmap & vector based glyphs are different
113 mImpl->mMetrics->SetGlyphType( glyphType );
115 // Clear the font-specific data
118 mImpl->mOperationsPending = ALL_OPERATIONS;
119 mImpl->mRecalculateNaturalSize = true;
121 mImpl->RequestRelayout();
124 void Controller::SetMarkupProcessorEnabled( bool enable )
126 mImpl->mMarkupProcessorEnabled = enable;
129 bool Controller::IsMarkupProcessorEnabled() const
131 return mImpl->mMarkupProcessorEnabled;
134 void Controller::SetText( const std::string& text )
136 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
138 // Reset keyboard as text changed
139 mImpl->ResetImfManager();
141 // Remove the previously set text and style.
147 CharacterIndex lastCursorIndex = 0u;
149 if( NULL != mImpl->mEventData )
151 // If popup shown then hide it by switching to Editing state
152 if( ( EventData::SELECTING == mImpl->mEventData->mState ) ||
153 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
154 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
155 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
157 mImpl->ChangeState( EventData::EDITING );
163 MarkupProcessData markupProcessData( mImpl->mLogicalModel->mColorRuns,
164 mImpl->mLogicalModel->mFontDescriptionRuns );
166 Length textSize = 0u;
167 const uint8_t* utf8 = NULL;
168 if( mImpl->mMarkupProcessorEnabled )
170 ProcessMarkupString( text, markupProcessData );
171 textSize = markupProcessData.markupProcessedText.size();
173 // This is a bit horrible but std::string returns a (signed) char*
174 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
178 textSize = text.size();
180 // This is a bit horrible but std::string returns a (signed) char*
181 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
184 // Convert text into UTF-32
185 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
186 utf32Characters.Resize( textSize );
188 // Transform a text array encoded in utf8 into an array encoded in utf32.
189 // It returns the actual number of characters.
190 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
191 utf32Characters.Resize( characterCount );
193 DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
194 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mLogicalModel->mText.Count() );
196 // To reset the cursor position
197 lastCursorIndex = characterCount;
199 // Update the rest of the model during size negotiation
200 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
202 // The natural size needs to be re-calculated.
203 mImpl->mRecalculateNaturalSize = true;
205 // Apply modifications to the model
206 mImpl->mOperationsPending = ALL_OPERATIONS;
210 ShowPlaceholderText();
213 // Resets the cursor position.
214 ResetCursorPosition( lastCursorIndex );
216 // Scrolls the text to make the cursor visible.
217 ResetScrollPosition();
219 mImpl->RequestRelayout();
221 if( NULL != mImpl->mEventData )
223 // Cancel previously queued events
224 mImpl->mEventData->mEventQueue.clear();
227 // Notify IMF as text changed
230 // Do this last since it provides callbacks into application code
231 mImpl->mControlInterface.TextChanged();
234 void Controller::GetText( std::string& text ) const
236 if( !mImpl->IsShowingPlaceholderText() )
238 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
240 if( 0u != utf32Characters.Count() )
242 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
247 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
251 unsigned int Controller::GetLogicalCursorPosition() const
253 if( NULL != mImpl->mEventData )
255 return mImpl->mEventData->mPrimaryCursorPosition;
261 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
263 if( NULL != mImpl->mEventData )
265 if( PLACEHOLDER_TYPE_INACTIVE == type )
267 mImpl->mEventData->mPlaceholderTextInactive = text;
271 mImpl->mEventData->mPlaceholderTextActive = text;
274 // Update placeholder if there is no text
275 if( mImpl->IsShowingPlaceholderText() ||
276 ( 0u == mImpl->mLogicalModel->mText.Count() ) )
278 ShowPlaceholderText();
283 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
285 if( NULL != mImpl->mEventData )
287 if( PLACEHOLDER_TYPE_INACTIVE == type )
289 text = mImpl->mEventData->mPlaceholderTextInactive;
293 text = mImpl->mEventData->mPlaceholderTextActive;
298 void Controller::SetMaximumNumberOfCharacters( Length maxCharacters )
300 mImpl->mMaximumNumberOfCharacters = maxCharacters;
303 int Controller::GetMaximumNumberOfCharacters()
305 return mImpl->mMaximumNumberOfCharacters;
308 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
310 if( NULL == mImpl->mFontDefaults )
312 mImpl->mFontDefaults = new FontDefaults();
315 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
316 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
317 mImpl->mFontDefaults->familyDefined = true;
319 // Clear the font-specific data
322 mImpl->mOperationsPending = ALL_OPERATIONS;
323 mImpl->mRecalculateNaturalSize = true;
325 mImpl->RequestRelayout();
328 const std::string& Controller::GetDefaultFontFamily() const
330 if( NULL != mImpl->mFontDefaults )
332 return mImpl->mFontDefaults->mFontDescription.family;
338 void Controller::SetDefaultFontStyle( const std::string& style )
340 if( NULL == mImpl->mFontDefaults )
342 mImpl->mFontDefaults = new FontDefaults();
345 mImpl->mFontDefaults->mFontStyle = style;
348 const std::string& Controller::GetDefaultFontStyle() const
350 if( NULL != mImpl->mFontDefaults )
352 return mImpl->mFontDefaults->mFontStyle;
358 void Controller::SetDefaultFontWeight( FontWeight weight )
360 if( NULL == mImpl->mFontDefaults )
362 mImpl->mFontDefaults = new FontDefaults();
365 mImpl->mFontDefaults->mFontDescription.weight = weight;
366 mImpl->mFontDefaults->weightDefined = true;
368 // Clear the font-specific data
371 mImpl->mOperationsPending = ALL_OPERATIONS;
372 mImpl->mRecalculateNaturalSize = true;
374 mImpl->RequestRelayout();
377 FontWeight Controller::GetDefaultFontWeight() const
379 if( NULL != mImpl->mFontDefaults )
381 return mImpl->mFontDefaults->mFontDescription.weight;
384 return TextAbstraction::FontWeight::NORMAL;
387 void Controller::SetDefaultFontWidth( FontWidth width )
389 if( NULL == mImpl->mFontDefaults )
391 mImpl->mFontDefaults = new FontDefaults();
394 mImpl->mFontDefaults->mFontDescription.width = width;
395 mImpl->mFontDefaults->widthDefined = true;
397 // Clear the font-specific data
400 mImpl->mOperationsPending = ALL_OPERATIONS;
401 mImpl->mRecalculateNaturalSize = true;
403 mImpl->RequestRelayout();
406 FontWidth Controller::GetDefaultFontWidth() const
408 if( NULL != mImpl->mFontDefaults )
410 return mImpl->mFontDefaults->mFontDescription.width;
413 return TextAbstraction::FontWidth::NORMAL;
416 void Controller::SetDefaultFontSlant( FontSlant slant )
418 if( NULL == mImpl->mFontDefaults )
420 mImpl->mFontDefaults = new FontDefaults();
423 mImpl->mFontDefaults->mFontDescription.slant = slant;
424 mImpl->mFontDefaults->slantDefined = true;
426 // Clear the font-specific data
429 mImpl->mOperationsPending = ALL_OPERATIONS;
430 mImpl->mRecalculateNaturalSize = true;
432 mImpl->RequestRelayout();
435 FontSlant Controller::GetDefaultFontSlant() const
437 if( NULL != mImpl->mFontDefaults )
439 return mImpl->mFontDefaults->mFontDescription.slant;
442 return TextAbstraction::FontSlant::NORMAL;
445 void Controller::SetDefaultPointSize( float pointSize )
447 if( NULL == mImpl->mFontDefaults )
449 mImpl->mFontDefaults = new FontDefaults();
452 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
453 mImpl->mFontDefaults->sizeDefined = true;
455 unsigned int horizontalDpi( 0u );
456 unsigned int verticalDpi( 0u );
457 mImpl->mFontClient.GetDpi( horizontalDpi, verticalDpi );
459 // Adjust the metrics if the fixed-size font should be down-scaled
460 int maxEmojiSize( pointSize/POINTS_PER_INCH * verticalDpi );
461 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultPointSize %p setting MaxEmojiSize %d\n", this, maxEmojiSize );
462 mImpl->mMetrics->SetMaxEmojiSize( maxEmojiSize );
464 // Clear the font-specific data
467 mImpl->mOperationsPending = ALL_OPERATIONS;
468 mImpl->mRecalculateNaturalSize = true;
470 mImpl->RequestRelayout();
473 float Controller::GetDefaultPointSize() const
475 if( NULL != mImpl->mFontDefaults )
477 return mImpl->mFontDefaults->mDefaultPointSize;
483 void Controller::UpdateAfterFontChange( const std::string& newDefaultFont )
485 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange");
487 if( !mImpl->mFontDefaults->familyDefined ) // If user defined font then should not update when system font changes
489 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
491 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
492 mImpl->UpdateModel( ALL_OPERATIONS );
493 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
494 mImpl->mRecalculateNaturalSize = true;
495 mImpl->RequestRelayout();
499 void Controller::SetTextColor( const Vector4& textColor )
501 mImpl->mTextColor = textColor;
503 if( !mImpl->IsShowingPlaceholderText() )
505 mImpl->mVisualModel->SetTextColor( textColor );
507 mImpl->RequestRelayout();
511 const Vector4& Controller::GetTextColor() const
513 return mImpl->mTextColor;
516 bool Controller::RemoveText( int cursorOffset, int numberOfCharacters )
518 bool removed = false;
520 if( NULL == mImpl->mEventData )
525 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfCharacters %d\n",
526 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters );
528 if( !mImpl->IsShowingPlaceholderText() )
530 // Delete at current cursor position
531 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
532 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
534 CharacterIndex cursorIndex = oldCursorIndex;
536 // Validate the cursor position & number of characters
537 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
539 cursorIndex = oldCursorIndex + cursorOffset;
542 if( ( cursorIndex + numberOfCharacters ) > currentText.Count() )
544 numberOfCharacters = currentText.Count() - cursorIndex;
547 if( ( cursorIndex + numberOfCharacters ) <= currentText.Count() )
549 // Update the input style and remove the text's style before removing the text.
551 // Set first the default input style.
552 mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
554 // Update the input style.
555 mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
557 // Remove the text's style before removing the text.
558 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfCharacters );
560 // Remove the characters.
561 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
562 Vector<Character>::Iterator last = first + numberOfCharacters;
564 currentText.Erase( first, last );
566 // Cursor position retreat
567 oldCursorIndex = cursorIndex;
569 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfCharacters );
577 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
579 if( NULL != mImpl->mEventData )
581 mImpl->mEventData->mPlaceholderTextColor = textColor;
584 if( mImpl->IsShowingPlaceholderText() )
586 mImpl->mVisualModel->SetTextColor( textColor );
587 mImpl->RequestRelayout();
591 const Vector4& Controller::GetPlaceholderTextColor() const
593 if( NULL != mImpl->mEventData )
595 return mImpl->mEventData->mPlaceholderTextColor;
601 void Controller::SetShadowOffset( const Vector2& shadowOffset )
603 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
605 mImpl->RequestRelayout();
608 const Vector2& Controller::GetShadowOffset() const
610 return mImpl->mVisualModel->GetShadowOffset();
613 void Controller::SetShadowColor( const Vector4& shadowColor )
615 mImpl->mVisualModel->SetShadowColor( shadowColor );
617 mImpl->RequestRelayout();
620 const Vector4& Controller::GetShadowColor() const
622 return mImpl->mVisualModel->GetShadowColor();
625 void Controller::SetUnderlineColor( const Vector4& color )
627 mImpl->mVisualModel->SetUnderlineColor( color );
629 mImpl->RequestRelayout();
632 const Vector4& Controller::GetUnderlineColor() const
634 return mImpl->mVisualModel->GetUnderlineColor();
637 void Controller::SetUnderlineEnabled( bool enabled )
639 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
641 mImpl->RequestRelayout();
644 bool Controller::IsUnderlineEnabled() const
646 return mImpl->mVisualModel->IsUnderlineEnabled();
649 void Controller::SetUnderlineHeight( float height )
651 mImpl->mVisualModel->SetUnderlineHeight( height );
653 mImpl->RequestRelayout();
656 float Controller::GetUnderlineHeight() const
658 return mImpl->mVisualModel->GetUnderlineHeight();
661 void Controller::SetInputColor( const Vector4& color )
663 if( NULL != mImpl->mEventData )
665 mImpl->mEventData->mInputStyle.textColor = color;
667 if( EventData::SELECTING == mImpl->mEventData->mState )
669 const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
671 // Get start and end position of selection
672 const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
673 const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
675 // Add the color run.
676 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
677 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
679 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
680 colorRun.color = color;
681 colorRun.characterRun.characterIndex = startOfSelectedText;
682 colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
684 // Request to relayout.
685 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
686 mImpl->RequestRelayout();
691 const Vector4& Controller::GetInputColor() const
693 if( NULL != mImpl->mEventData )
695 return mImpl->mEventData->mInputStyle.textColor;
698 // Return the default text's color if there is no EventData.
699 return mImpl->mTextColor;
703 void Controller::SetInputFontFamily( const std::string& fontFamily )
705 if( NULL != mImpl->mEventData )
707 mImpl->mEventData->mInputStyle.familyName = fontFamily;
708 mImpl->mEventData->mInputStyle.familyDefined = true;
710 if( EventData::SELECTING == mImpl->mEventData->mState )
712 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
713 mImpl->mLogicalModel );
715 fontDescriptionRun.familyLength = fontFamily.size();
716 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
717 memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
718 fontDescriptionRun.familyDefined = true;
720 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
722 // Request to relayout.
723 mImpl->mOperationsPending = ALL_OPERATIONS;
724 mImpl->mRecalculateNaturalSize = true;
725 mImpl->RequestRelayout();
727 // As the font changes, recalculate the handle positions is needed.
728 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
729 mImpl->mEventData->mUpdateRightSelectionPosition = true;
730 mImpl->mEventData->mScrollAfterUpdatePosition = true;
735 const std::string& Controller::GetInputFontFamily() const
737 if( NULL != mImpl->mEventData )
739 return mImpl->mEventData->mInputStyle.familyName;
742 // Return the default font's family if there is no EventData.
743 return GetDefaultFontFamily();
746 void Controller::SetInputFontStyle( const std::string& fontStyle )
748 if( NULL != mImpl->mEventData )
750 mImpl->mEventData->mInputStyle.fontStyle = fontStyle;
754 const std::string& Controller::GetInputFontStyle() const
756 if( NULL != mImpl->mEventData )
758 return mImpl->mEventData->mInputStyle.fontStyle;
761 // Return the default font's style if there is no EventData.
762 return GetDefaultFontStyle();
765 void Controller::SetInputFontWeight( FontWeight weight )
767 if( NULL != mImpl->mEventData )
769 mImpl->mEventData->mInputStyle.weight = weight;
770 mImpl->mEventData->mInputStyle.weightDefined = true;
772 if( EventData::SELECTING == mImpl->mEventData->mState )
774 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
775 mImpl->mLogicalModel );
777 fontDescriptionRun.weight = weight;
778 fontDescriptionRun.weightDefined = true;
780 // Request to relayout.
781 mImpl->mOperationsPending = ALL_OPERATIONS;
782 mImpl->mRecalculateNaturalSize = true;
783 mImpl->RequestRelayout();
785 // As the font might change, recalculate the handle positions is needed.
786 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
787 mImpl->mEventData->mUpdateRightSelectionPosition = true;
788 mImpl->mEventData->mScrollAfterUpdatePosition = true;
793 FontWeight Controller::GetInputFontWeight() const
795 if( NULL != mImpl->mEventData )
797 return mImpl->mEventData->mInputStyle.weight;
800 return GetDefaultFontWeight();
803 void Controller::SetInputFontWidth( FontWidth width )
805 if( NULL != mImpl->mEventData )
807 mImpl->mEventData->mInputStyle.width = width;
808 mImpl->mEventData->mInputStyle.widthDefined = true;
810 if( EventData::SELECTING == mImpl->mEventData->mState )
812 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
813 mImpl->mLogicalModel );
815 fontDescriptionRun.width = width;
816 fontDescriptionRun.widthDefined = true;
818 // Request to relayout.
819 mImpl->mOperationsPending = ALL_OPERATIONS;
820 mImpl->mRecalculateNaturalSize = true;
821 mImpl->RequestRelayout();
823 // As the font might change, recalculate the handle positions is needed.
824 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
825 mImpl->mEventData->mUpdateRightSelectionPosition = true;
826 mImpl->mEventData->mScrollAfterUpdatePosition = true;
831 FontWidth Controller::GetInputFontWidth() const
833 if( NULL != mImpl->mEventData )
835 return mImpl->mEventData->mInputStyle.width;
838 return GetDefaultFontWidth();
841 void Controller::SetInputFontSlant( FontSlant slant )
843 if( NULL != mImpl->mEventData )
845 mImpl->mEventData->mInputStyle.slant = slant;
846 mImpl->mEventData->mInputStyle.slantDefined = true;
848 if( EventData::SELECTING == mImpl->mEventData->mState )
850 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
851 mImpl->mLogicalModel );
853 fontDescriptionRun.slant = slant;
854 fontDescriptionRun.slantDefined = true;
856 // Request to relayout.
857 mImpl->mOperationsPending = ALL_OPERATIONS;
858 mImpl->mRecalculateNaturalSize = true;
859 mImpl->RequestRelayout();
861 // As the font might change, recalculate the handle positions is needed.
862 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
863 mImpl->mEventData->mUpdateRightSelectionPosition = true;
864 mImpl->mEventData->mScrollAfterUpdatePosition = true;
869 FontSlant Controller::GetInputFontSlant() const
871 if( NULL != mImpl->mEventData )
873 return mImpl->mEventData->mInputStyle.slant;
876 return GetDefaultFontSlant();
879 void Controller::SetInputFontPointSize( float size )
881 if( NULL != mImpl->mEventData )
883 mImpl->mEventData->mInputStyle.size = size;
885 if( EventData::SELECTING == mImpl->mEventData->mState )
887 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
888 mImpl->mLogicalModel );
890 fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
891 fontDescriptionRun.sizeDefined = true;
893 // Request to relayout.
894 mImpl->mOperationsPending = ALL_OPERATIONS;
895 mImpl->mRecalculateNaturalSize = true;
896 mImpl->RequestRelayout();
898 // As the font might change, recalculate the handle positions is needed.
899 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
900 mImpl->mEventData->mUpdateRightSelectionPosition = true;
901 mImpl->mEventData->mScrollAfterUpdatePosition = true;
906 float Controller::GetInputFontPointSize() const
908 if( NULL != mImpl->mEventData )
910 return mImpl->mEventData->mInputStyle.size;
913 // Return the default font's point size if there is no EventData.
914 return GetDefaultPointSize();
917 void Controller::SetEnableCursorBlink( bool enable )
919 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
921 if( NULL != mImpl->mEventData )
923 mImpl->mEventData->mCursorBlinkEnabled = enable;
926 mImpl->mEventData->mDecorator )
928 mImpl->mEventData->mDecorator->StopCursorBlink();
933 bool Controller::GetEnableCursorBlink() const
935 if( NULL != mImpl->mEventData )
937 return mImpl->mEventData->mCursorBlinkEnabled;
943 const Vector2& Controller::GetScrollPosition() const
945 if( NULL != mImpl->mEventData )
947 return mImpl->mEventData->mScrollPosition;
950 return Vector2::ZERO;
953 const Vector2& Controller::GetAlignmentOffset() const
955 return mImpl->mAlignmentOffset;
958 Vector3 Controller::GetNaturalSize()
960 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
963 // Make sure the model is up-to-date before layouting
964 ProcessModifyEvents();
966 if( mImpl->mRecalculateNaturalSize )
968 // Operations that can be done only once until the text changes.
969 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
977 // Make sure the model is up-to-date before layouting
978 mImpl->UpdateModel( onlyOnceOperations );
980 // Operations that need to be done if the size changes.
981 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
985 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
986 static_cast<OperationsMask>( onlyOnceOperations |
988 naturalSize.GetVectorXY() );
990 // Do not do again the only once operations.
991 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
993 // Do the size related operations again.
994 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
996 // Stores the natural size to avoid recalculate it again
997 // unless the text/style changes.
998 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
1000 mImpl->mRecalculateNaturalSize = false;
1002 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1006 naturalSize = mImpl->mVisualModel->GetNaturalSize();
1008 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1011 naturalSize.x = ConvertToEven( naturalSize.x );
1012 naturalSize.y = ConvertToEven( naturalSize.y );
1017 float Controller::GetHeightForWidth( float width )
1019 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1020 // Make sure the model is up-to-date before layouting
1021 ProcessModifyEvents();
1024 if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
1026 // Operations that can be done only once until the text changes.
1027 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1034 GET_GLYPH_METRICS );
1035 // Make sure the model is up-to-date before layouting
1036 mImpl->UpdateModel( onlyOnceOperations );
1038 // Operations that need to be done if the size changes.
1039 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1043 DoRelayout( Size( width, MAX_FLOAT ),
1044 static_cast<OperationsMask>( onlyOnceOperations |
1048 // Do not do again the only once operations.
1049 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1051 // Do the size related operations again.
1052 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1053 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1057 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1058 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1061 return layoutSize.height;
1064 bool Controller::Relayout( const Size& size )
1066 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
1068 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1070 bool glyphsRemoved( false );
1071 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1073 mImpl->mVisualModel->mGlyphPositions.Clear();
1074 glyphsRemoved = true;
1076 // Not worth to relayout if width or height is equal to zero.
1077 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1078 return glyphsRemoved;
1081 // Whether a new size has been set.
1082 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1086 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1088 // Layout operations that need to be done if the size changes.
1089 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1092 UPDATE_ACTUAL_SIZE |
1095 mImpl->mVisualModel->mControlSize = size;
1098 // Whether there are modify events.
1099 const bool isModifyEventsEmpty = 0u == mImpl->mModifyEvents.Count();
1101 // Make sure the model is up-to-date before layouting.
1102 ProcessModifyEvents();
1103 mImpl->UpdateModel( mImpl->mOperationsPending );
1105 // Style operations that need to be done if the text is modified.
1106 if( !isModifyEventsEmpty )
1108 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1112 // Apply the style runs if text is modified.
1113 bool updated = mImpl->UpdateModelStyle( mImpl->mOperationsPending );
1117 updated = DoRelayout( mImpl->mVisualModel->mControlSize,
1118 mImpl->mOperationsPending,
1119 layoutSize ) || updated;
1121 // Do not re-do any operation until something changes.
1122 mImpl->mOperationsPending = NO_OPERATION;
1124 // Whether the text control is editable
1125 const bool isEditable = NULL != mImpl->mEventData;
1127 // Keep the current offset and alignment as it will be used to update the decorator's positions (if the size changes).
1129 if( newSize && isEditable )
1131 offset = mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition;
1134 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
1135 CalculateTextAlignment( size );
1141 // If there is a new size, the scroll position needs to be clamped.
1142 mImpl->ClampHorizontalScroll( layoutSize );
1144 // Update the decorator's positions is needed if there is a new size.
1145 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
1148 // Move the cursor, grab handle etc.
1149 updated = mImpl->ProcessInputEvents() || updated;
1152 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1156 void Controller::ProcessModifyEvents()
1158 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
1160 if( 0u == events.Count() )
1166 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
1167 endIt = events.End();
1171 const ModifyEvent& event = *it;
1173 if( ModifyEvent::TEXT_REPLACED == event.type )
1175 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
1176 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
1178 TextReplacedEvent();
1180 else if( ModifyEvent::TEXT_INSERTED == event.type )
1182 TextInsertedEvent();
1184 else if( ModifyEvent::TEXT_DELETED == event.type )
1186 // Placeholder-text cannot be deleted
1187 if( !mImpl->IsShowingPlaceholderText() )
1194 if( NULL != mImpl->mEventData )
1196 // When the text is being modified, delay cursor blinking
1197 mImpl->mEventData->mDecorator->DelayCursorBlink();
1200 // Discard temporary text
1204 void Controller::ResetText()
1207 mImpl->mLogicalModel->mText.Clear();
1210 // We have cleared everything including the placeholder-text
1211 mImpl->PlaceholderCleared();
1213 // The natural size needs to be re-calculated.
1214 mImpl->mRecalculateNaturalSize = true;
1216 // Apply modifications to the model
1217 mImpl->mOperationsPending = ALL_OPERATIONS;
1220 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
1222 // Reset the cursor position
1223 if( NULL != mImpl->mEventData )
1225 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
1227 // Update the cursor if it's in editing mode.
1228 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1230 mImpl->mEventData->mUpdateCursorPosition = true;
1235 void Controller::ResetScrollPosition()
1237 if( NULL != mImpl->mEventData )
1239 // Reset the scroll position.
1240 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
1241 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1245 void Controller::TextReplacedEvent()
1250 // The natural size needs to be re-calculated.
1251 mImpl->mRecalculateNaturalSize = true;
1253 // Apply modifications to the model
1254 mImpl->mOperationsPending = ALL_OPERATIONS;
1255 mImpl->UpdateModel( ALL_OPERATIONS );
1256 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
1258 UPDATE_ACTUAL_SIZE |
1262 void Controller::TextInsertedEvent()
1264 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
1266 if( NULL == mImpl->mEventData )
1271 // TODO - Optimize this
1274 // The natural size needs to be re-calculated.
1275 mImpl->mRecalculateNaturalSize = true;
1277 // Apply modifications to the model; TODO - Optimize this
1278 mImpl->mOperationsPending = ALL_OPERATIONS;
1279 mImpl->UpdateModel( ALL_OPERATIONS );
1280 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
1282 UPDATE_ACTUAL_SIZE |
1285 // Queue a cursor reposition event; this must wait until after DoRelayout()
1286 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1288 mImpl->mEventData->mUpdateCursorPosition = true;
1289 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1293 void Controller::TextDeletedEvent()
1295 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
1297 if( NULL == mImpl->mEventData )
1302 // TODO - Optimize this
1305 // The natural size needs to be re-calculated.
1306 mImpl->mRecalculateNaturalSize = true;
1308 // Apply modifications to the model; TODO - Optimize this
1309 mImpl->mOperationsPending = ALL_OPERATIONS;
1310 mImpl->UpdateModel( ALL_OPERATIONS );
1311 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
1313 UPDATE_ACTUAL_SIZE |
1316 // Queue a cursor reposition event; this must wait until after DoRelayout()
1317 mImpl->mEventData->mUpdateCursorPosition = true;
1318 if( 0u != mImpl->mLogicalModel->mText.Count() )
1320 mImpl->mEventData->mScrollAfterDelete = true;
1324 bool Controller::DoRelayout( const Size& size,
1325 OperationsMask operationsRequired,
1328 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
1329 bool viewUpdated( false );
1331 // Calculate the operations to be done.
1332 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
1334 if( LAYOUT & operations )
1336 // Some vectors with data needed to layout and reorder may be void
1337 // after the first time the text has been laid out.
1338 // Fill the vectors again.
1340 const Length numberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
1342 if( 0u == numberOfGlyphs )
1344 if( UPDATE_ACTUAL_SIZE & operations )
1346 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
1349 // Nothing else to do if there is no glyphs.
1350 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1354 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1355 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1356 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1357 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1358 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1359 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1360 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1361 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1362 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1364 // Set the layout parameters.
1365 LayoutParameters layoutParameters( size,
1367 lineBreakInfo.Begin(),
1368 wordBreakInfo.Begin(),
1369 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1371 glyphsToCharactersMap.Begin(),
1372 charactersPerGlyph.Begin(),
1373 charactersToGlyph.Begin(),
1374 glyphsPerCharacter.Begin(),
1377 // The laid-out lines.
1378 // It's not possible to know in how many lines the text is going to be laid-out,
1379 // but it can be resized at least with the number of 'paragraphs' to avoid
1380 // some re-allocations.
1381 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1383 // Delete any previous laid out lines before setting the new ones.
1386 // The capacity of the bidirectional paragraph info is the number of paragraphs.
1387 lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
1389 // Resize the vector of positions to have the same size than the vector of glyphs.
1390 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1391 glyphPositions.Resize( numberOfGlyphs );
1393 // Whether the last character is a new paragraph character.
1394 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1396 // The initial glyph and the number of glyphs to layout.
1397 layoutParameters.startGlyphIndex = 0u;
1398 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1400 // Update the visual model.
1401 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1408 // Reorder the lines
1409 if( REORDER & operations )
1411 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1412 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1414 // Check first if there are paragraphs with bidirectional info.
1415 if( 0u != bidirectionalInfo.Count() )
1418 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1419 const CharacterIndex startIndex = 0u;
1420 const Length requestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1422 // Reorder the lines.
1423 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1424 ReorderLines( bidirectionalInfo,
1426 requestedNumberOfCharacters,
1428 bidirectionalLineInfo );
1430 // Set the bidirectional info per line into the layout parameters.
1431 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1432 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1434 // Set the bidirectional info into the model.
1435 mImpl->mLogicalModel->SetVisualToLogicalMap( layoutParameters.lineBidirectionalInfoRunsBuffer,
1436 layoutParameters.numberOfBidirectionalInfoRuns,
1438 requestedNumberOfCharacters );
1440 // Re-layout the text. Reorder those lines with right to left characters.
1441 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1443 requestedNumberOfCharacters,
1446 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
1447 for( Vector<BidirectionalLineInfoRun>::Iterator it = bidirectionalLineInfo.Begin(),
1448 endIt = bidirectionalLineInfo.End();
1452 BidirectionalLineInfoRun& bidiLineInfo = *it;
1454 free( bidiLineInfo.visualToLogicalMap );
1455 bidiLineInfo.visualToLogicalMap = NULL;
1458 bidirectionalLineInfo.Clear();
1462 // Sets the actual size.
1463 if( UPDATE_ACTUAL_SIZE & operations )
1465 mImpl->mVisualModel->SetLayoutSize( layoutSize );
1471 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1474 if( ALIGN & operations )
1476 // The laid-out lines.
1477 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1479 const CharacterIndex startIndex = 0u;
1480 const Length requestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1482 mImpl->mLayoutEngine.Align( size,
1484 requestedNumberOfCharacters,
1490 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1494 void Controller::SetMultiLineEnabled( bool enable )
1496 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1498 if( layout != mImpl->mLayoutEngine.GetLayout() )
1500 // Set the layout type.
1501 mImpl->mLayoutEngine.SetLayout( layout );
1503 // Set the flags to redo the layout operations
1504 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1505 UPDATE_ACTUAL_SIZE |
1509 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1511 mImpl->RequestRelayout();
1515 bool Controller::IsMultiLineEnabled() const
1517 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1520 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1522 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1524 // Set the alignment.
1525 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1527 // Set the flag to redo the alignment operation.
1528 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1530 mImpl->RequestRelayout();
1534 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1536 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1539 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1541 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1543 // Set the alignment.
1544 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1546 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1548 mImpl->RequestRelayout();
1552 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1554 return mImpl->mLayoutEngine.GetVerticalAlignment();
1557 void Controller::CalculateTextAlignment( const Size& controlSize )
1559 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1561 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1563 // Get the line height of the default font.
1564 layoutSize.height = mImpl->GetDefaultFontLineHeight();
1567 if( LayoutEngine::SINGLE_LINE_BOX == mImpl->mLayoutEngine.GetLayout() )
1569 // Get the direction of the first character.
1570 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1572 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1573 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1574 if( firstParagraphDirection )
1576 switch( horizontalAlignment )
1578 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1580 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1583 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1588 case LayoutEngine::HORIZONTAL_ALIGN_END:
1590 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1596 switch( horizontalAlignment )
1598 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1600 mImpl->mAlignmentOffset.x = 0.f;
1603 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1605 mImpl->mAlignmentOffset.x = floorf( 0.5f * ( controlSize.width - layoutSize.width ) ); // try to avoid pixel alignment.
1608 case LayoutEngine::HORIZONTAL_ALIGN_END:
1610 mImpl->mAlignmentOffset.x = controlSize.width - layoutSize.width;
1616 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1617 switch( verticalAlignment )
1619 case LayoutEngine::VERTICAL_ALIGN_TOP:
1621 mImpl->mAlignmentOffset.y = 0.f;
1624 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1626 mImpl->mAlignmentOffset.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
1629 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1631 mImpl->mAlignmentOffset.y = controlSize.height - layoutSize.height;
1637 LayoutEngine& Controller::GetLayoutEngine()
1639 return mImpl->mLayoutEngine;
1642 View& Controller::GetView()
1644 return mImpl->mView;
1647 void Controller::KeyboardFocusGainEvent()
1649 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1651 if( NULL != mImpl->mEventData )
1653 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1654 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1656 mImpl->ChangeState( EventData::EDITING );
1657 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1660 if( mImpl->IsShowingPlaceholderText() )
1662 // Show alternative placeholder-text when editing
1663 ShowPlaceholderText();
1666 mImpl->RequestRelayout();
1670 void Controller::KeyboardFocusLostEvent()
1672 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1674 if( NULL != mImpl->mEventData )
1676 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1678 mImpl->ChangeState( EventData::INACTIVE );
1680 if( !mImpl->IsShowingRealText() )
1682 // Revert to regular placeholder-text when not editing
1683 ShowPlaceholderText();
1687 mImpl->RequestRelayout();
1690 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1692 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1694 bool textChanged( false );
1696 if( ( NULL != mImpl->mEventData ) &&
1697 ( keyEvent.state == KeyEvent::Down ) )
1699 int keyCode = keyEvent.keyCode;
1700 const std::string& keyString = keyEvent.keyPressed;
1702 // Pre-process to separate modifying events from non-modifying input events.
1703 if( Dali::DALI_KEY_ESCAPE == keyCode )
1705 // Escape key is a special case which causes focus loss
1706 KeyboardFocusLostEvent();
1708 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
1709 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1710 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
1711 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
1713 Event event( Event::CURSOR_KEY_EVENT );
1714 event.p1.mInt = keyCode;
1715 mImpl->mEventData->mEventQueue.push_back( event );
1717 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1719 textChanged = BackspaceKeyEvent();
1721 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1723 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1724 // Avoids calling the InsertText() method which can delete selected text
1726 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1727 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1729 mImpl->ChangeState( EventData::INACTIVE );
1730 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1731 // Avoids calling the InsertText() method which can delete selected text
1733 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1735 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1736 // and a character is typed after the type of a upper case latin character.
1742 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1744 // IMF manager is no longer handling key-events
1745 mImpl->ClearPreEditFlag();
1747 InsertText( keyString, COMMIT );
1751 if( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1752 ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1754 mImpl->ChangeState( EventData::EDITING );
1757 mImpl->RequestRelayout();
1762 // Do this last since it provides callbacks into application code
1763 mImpl->mControlInterface.TextChanged();
1769 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1771 bool removedPrevious( false );
1772 bool maxLengthReached( false );
1774 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1776 if( NULL == mImpl->mEventData )
1781 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1782 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1783 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1785 // TODO: At the moment the underline runs are only for pre-edit.
1786 mImpl->mVisualModel->mUnderlineRuns.Clear();
1788 Vector<Character> utf32Characters;
1789 Length characterCount( 0u );
1791 // Remove the previous IMF pre-edit (predicitive text)
1792 if( mImpl->mEventData->mPreEditFlag &&
1793 ( 0u != mImpl->mEventData->mPreEditLength ) )
1795 const CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1797 removedPrevious = RemoveText( -static_cast<int>( offset ), mImpl->mEventData->mPreEditLength );
1799 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1800 mImpl->mEventData->mPreEditLength = 0u;
1804 // Remove the previous Selection
1805 removedPrevious = RemoveSelectedText();
1810 // Convert text into UTF-32
1811 utf32Characters.Resize( text.size() );
1813 // This is a bit horrible but std::string returns a (signed) char*
1814 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1816 // Transform a text array encoded in utf8 into an array encoded in utf32.
1817 // It returns the actual number of characters.
1818 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1819 utf32Characters.Resize( characterCount );
1821 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1822 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1825 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1827 // The placeholder text is no longer needed
1828 if( mImpl->IsShowingPlaceholderText() )
1833 mImpl->ChangeState( EventData::EDITING );
1835 // Handle the IMF (predicitive text) state changes
1836 if( COMMIT == type )
1838 // IMF manager is no longer handling key-events
1839 mImpl->ClearPreEditFlag();
1843 if( !mImpl->mEventData->mPreEditFlag )
1845 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1847 // Record the start of the pre-edit text
1848 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1851 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1852 mImpl->mEventData->mPreEditFlag = true;
1854 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1857 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1859 // Restrict new text to fit within Maximum characters setting.
1860 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1861 maxLengthReached = ( characterCount > maxSizeOfNewText );
1863 // The cursor position.
1864 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1866 // Update the text's style.
1868 // Updates the text style runs by adding characters.
1869 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
1871 // Get the character index from the cursor index.
1872 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
1874 // Retrieve the text's style for the given index.
1876 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
1878 // Whether to add a new text color run.
1879 const bool addColorRun = style.textColor != mImpl->mEventData->mInputStyle.textColor;
1881 // Whether to add a new font run.
1882 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
1883 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
1884 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
1885 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
1886 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
1891 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
1892 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
1894 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
1895 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
1896 colorRun.characterRun.characterIndex = cursorIndex;
1897 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1900 if( addFontNameRun ||
1906 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
1907 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
1909 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
1911 if( addFontNameRun )
1913 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
1914 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
1915 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
1916 fontDescriptionRun.familyDefined = true;
1918 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
1921 if( addFontWeightRun )
1923 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
1924 fontDescriptionRun.weightDefined = true;
1927 if( addFontWidthRun )
1929 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
1930 fontDescriptionRun.widthDefined = true;
1933 if( addFontSlantRun )
1935 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
1936 fontDescriptionRun.slantDefined = true;
1939 if( addFontSizeRun )
1941 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
1942 fontDescriptionRun.sizeDefined = true;
1945 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
1946 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1949 // Insert at current cursor position.
1950 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1952 if( cursorIndex < numberOfCharactersInModel )
1954 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1958 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1961 cursorIndex += maxSizeOfNewText;
1963 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1966 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
1967 mImpl->IsPlaceholderAvailable() )
1969 // Show place-holder if empty after removing the pre-edit text
1970 ShowPlaceholderText();
1971 mImpl->mEventData->mUpdateCursorPosition = true;
1972 mImpl->ClearPreEditFlag();
1974 else if( removedPrevious ||
1975 ( 0 != utf32Characters.Count() ) )
1977 // Queue an inserted event
1978 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1981 if( maxLengthReached )
1983 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1985 mImpl->ResetImfManager();
1987 // Do this last since it provides callbacks into application code
1988 mImpl->mControlInterface.MaxLengthReached();
1992 bool Controller::RemoveSelectedText()
1994 bool textRemoved( false );
1996 if( EventData::SELECTING == mImpl->mEventData->mState )
1998 std::string removedString;
1999 mImpl->RetrieveSelection( removedString, true );
2001 if( !removedString.empty() )
2004 mImpl->ChangeState( EventData::EDITING );
2011 void Controller::TapEvent( unsigned int tapCount, float x, float y )
2013 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
2015 if( NULL != mImpl->mEventData )
2017 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
2019 if( 1u == tapCount )
2021 // This is to avoid unnecessary relayouts when tapping an empty text-field
2022 bool relayoutNeeded( false );
2024 if( ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) ||
2025 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
2027 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE); // If Popup shown hide it here so can be shown again if required.
2030 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
2032 // Already in an active state so show a popup
2033 if( !mImpl->IsClipboardEmpty() )
2035 // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
2036 mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
2040 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2042 relayoutNeeded = true;
2046 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2048 // Hide placeholder text
2052 if( EventData::INACTIVE == mImpl->mEventData->mState )
2054 mImpl->ChangeState( EventData::EDITING );
2056 else if( !mImpl->IsClipboardEmpty() )
2058 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2060 relayoutNeeded = true;
2063 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2064 if( relayoutNeeded )
2066 Event event( Event::TAP_EVENT );
2067 event.p1.mUint = tapCount;
2068 event.p2.mFloat = x;
2069 event.p3.mFloat = y;
2070 mImpl->mEventData->mEventQueue.push_back( event );
2072 mImpl->RequestRelayout();
2075 else if( 2u == tapCount )
2077 if( mImpl->mEventData->mSelectionEnabled &&
2078 mImpl->IsShowingRealText() )
2080 SelectEvent( x, y, false );
2085 // Reset keyboard as tap event has occurred.
2086 mImpl->ResetImfManager();
2089 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2090 // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2092 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2094 if( NULL != mImpl->mEventData )
2096 Event event( Event::PAN_EVENT );
2097 event.p1.mInt = state;
2098 event.p2.mFloat = displacement.x;
2099 event.p3.mFloat = displacement.y;
2100 mImpl->mEventData->mEventQueue.push_back( event );
2102 mImpl->RequestRelayout();
2106 void Controller::LongPressEvent( Gesture::State state, float x, float y )
2108 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2110 if( ( state == Gesture::Started ) &&
2111 ( NULL != mImpl->mEventData ) )
2113 if( !mImpl->IsShowingRealText() )
2115 Event event( Event::LONG_PRESS_EVENT );
2116 event.p1.mInt = state;
2117 mImpl->mEventData->mEventQueue.push_back( event );
2118 mImpl->RequestRelayout();
2122 // The 1st long-press on inactive text-field is treated as tap
2123 if( EventData::INACTIVE == mImpl->mEventData->mState )
2125 mImpl->ChangeState( EventData::EDITING );
2127 Event event( Event::TAP_EVENT );
2129 event.p2.mFloat = x;
2130 event.p3.mFloat = y;
2131 mImpl->mEventData->mEventQueue.push_back( event );
2133 mImpl->RequestRelayout();
2137 // Reset the imf manger to commit the pre-edit before selecting the text.
2138 mImpl->ResetImfManager();
2140 SelectEvent( x, y, false );
2146 void Controller::SelectEvent( float x, float y, bool selectAll )
2148 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2150 if( NULL != mImpl->mEventData )
2152 mImpl->ChangeState( EventData::SELECTING );
2156 Event event( Event::SELECT_ALL );
2157 mImpl->mEventData->mEventQueue.push_back( event );
2161 Event event( Event::SELECT );
2162 event.p2.mFloat = x;
2163 event.p3.mFloat = y;
2164 mImpl->mEventData->mEventQueue.push_back( event );
2167 mImpl->RequestRelayout();
2171 void Controller::GetTargetSize( Vector2& targetSize )
2173 targetSize = mImpl->mVisualModel->mControlSize;
2176 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2178 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2181 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2183 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2185 if( NULL != mImpl->mEventData )
2187 switch( handleType )
2191 Event event( Event::GRAB_HANDLE_EVENT );
2192 event.p1.mUint = state;
2193 event.p2.mFloat = x;
2194 event.p3.mFloat = y;
2196 mImpl->mEventData->mEventQueue.push_back( event );
2199 case LEFT_SELECTION_HANDLE:
2201 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2202 event.p1.mUint = state;
2203 event.p2.mFloat = x;
2204 event.p3.mFloat = y;
2206 mImpl->mEventData->mEventQueue.push_back( event );
2209 case RIGHT_SELECTION_HANDLE:
2211 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2212 event.p1.mUint = state;
2213 event.p2.mFloat = x;
2214 event.p3.mFloat = y;
2216 mImpl->mEventData->mEventQueue.push_back( event );
2219 case LEFT_SELECTION_HANDLE_MARKER:
2220 case RIGHT_SELECTION_HANDLE_MARKER:
2222 // Markers do not move the handles.
2225 case HANDLE_TYPE_COUNT:
2227 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2231 mImpl->RequestRelayout();
2235 void Controller::PasteText( const std::string& stringToPaste )
2237 InsertText( stringToPaste, Text::Controller::COMMIT );
2238 mImpl->ChangeState( EventData::EDITING );
2239 mImpl->RequestRelayout();
2241 // Do this last since it provides callbacks into application code
2242 mImpl->mControlInterface.TextChanged();
2245 void Controller::PasteClipboardItemEvent()
2247 // Retrieve the clipboard contents first
2248 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2249 std::string stringToPaste( notifier.GetContent() );
2251 // Commit the current pre-edit text; the contents of the clipboard should be appended
2252 mImpl->ResetImfManager();
2255 PasteText( stringToPaste );
2258 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2260 if( NULL == mImpl->mEventData )
2267 case Toolkit::TextSelectionPopup::CUT:
2269 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2270 mImpl->mOperationsPending = ALL_OPERATIONS;
2272 // This is to reset the virtual keyboard to Upper-case
2273 if( 0u == mImpl->mLogicalModel->mText.Count() )
2278 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2279 !mImpl->IsPlaceholderAvailable() )
2281 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2285 ShowPlaceholderText();
2286 mImpl->mEventData->mUpdateCursorPosition = true;
2288 mImpl->RequestRelayout();
2289 mImpl->mControlInterface.TextChanged();
2292 case Toolkit::TextSelectionPopup::COPY:
2294 mImpl->SendSelectionToClipboard( false ); // Text not modified
2295 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
2298 case Toolkit::TextSelectionPopup::PASTE:
2300 std::string stringToPaste("");
2301 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2302 PasteText( stringToPaste );
2305 case Toolkit::TextSelectionPopup::SELECT:
2307 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2309 if( mImpl->mEventData->mSelectionEnabled )
2311 // Creates a SELECT event.
2312 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2316 case Toolkit::TextSelectionPopup::SELECT_ALL:
2318 // Creates a SELECT_ALL event
2319 SelectEvent( 0.f, 0.f, true );
2322 case Toolkit::TextSelectionPopup::CLIPBOARD:
2324 mImpl->ShowClipboard();
2327 case Toolkit::TextSelectionPopup::NONE:
2335 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2337 bool update = false;
2338 bool requestRelayout = false;
2341 unsigned int cursorPosition = 0u;
2343 switch( imfEvent.eventName )
2345 case ImfManager::COMMIT:
2347 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2349 requestRelayout = true;
2352 case ImfManager::PREEDIT:
2354 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2356 requestRelayout = true;
2359 case ImfManager::DELETESURROUNDING:
2361 update = RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
2365 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2366 !mImpl->IsPlaceholderAvailable() )
2368 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2372 ShowPlaceholderText();
2373 mImpl->mEventData->mUpdateCursorPosition = true;
2376 requestRelayout = true;
2379 case ImfManager::GETSURROUNDING:
2382 cursorPosition = GetLogicalCursorPosition();
2384 imfManager.SetSurroundingText( text );
2385 imfManager.SetCursorPosition( cursorPosition );
2388 case ImfManager::VOID:
2395 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
2398 cursorPosition = GetLogicalCursorPosition();
2401 if( requestRelayout )
2403 mImpl->mOperationsPending = ALL_OPERATIONS;
2404 mImpl->RequestRelayout();
2406 // Do this last since it provides callbacks into application code
2407 mImpl->mControlInterface.TextChanged();
2410 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
2412 return callbackData;
2415 Controller::~Controller()
2420 bool Controller::BackspaceKeyEvent()
2422 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2424 bool removed = false;
2426 if( NULL == mImpl->mEventData )
2431 // IMF manager is no longer handling key-events
2432 mImpl->ClearPreEditFlag();
2434 if( EventData::SELECTING == mImpl->mEventData->mState )
2436 removed = RemoveSelectedText();
2438 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2440 // Remove the character before the current cursor position
2441 removed = RemoveText( -1, 1 );
2446 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE RemovedText\n", this );
2447 // Notifiy the IMF manager after text changed
2448 // Automatic Upper-case and restarting prediction on an existing word require this.
2451 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2452 !mImpl->IsPlaceholderAvailable() )
2454 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2458 ShowPlaceholderText();
2459 mImpl->mEventData->mUpdateCursorPosition = true;
2466 void Controller::NotifyImfManager()
2468 if( NULL != mImpl->mEventData )
2470 if( mImpl->mEventData->mImfManager )
2472 // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
2475 mImpl->mEventData->mImfManager.SetSurroundingText( text );
2477 mImpl->mEventData->mImfManager.SetCursorPosition( GetLogicalCursorPosition() );
2478 mImpl->mEventData->mImfManager.NotifyCursorPosition();
2483 void Controller::ShowPlaceholderText()
2485 if( mImpl->IsPlaceholderAvailable() )
2487 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2489 if( NULL == mImpl->mEventData )
2494 mImpl->mEventData->mIsShowingPlaceholderText = true;
2496 // Disable handles when showing place-holder text
2497 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2498 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2499 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2501 const char* text( NULL );
2504 // TODO - Switch placeholder text styles when changing state
2505 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2506 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2508 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2509 size = mImpl->mEventData->mPlaceholderTextActive.size();
2513 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2514 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2517 // Reset model for showing placeholder.
2518 mImpl->mLogicalModel->mText.Clear();
2520 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2522 // Convert text into UTF-32
2523 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2524 utf32Characters.Resize( size );
2526 // This is a bit horrible but std::string returns a (signed) char*
2527 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2529 // Transform a text array encoded in utf8 into an array encoded in utf32.
2530 // It returns the actual number of characters.
2531 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2532 utf32Characters.Resize( characterCount );
2534 // Reset the cursor position
2535 mImpl->mEventData->mPrimaryCursorPosition = 0;
2537 // The natural size needs to be re-calculated.
2538 mImpl->mRecalculateNaturalSize = true;
2540 // Apply modifications to the model
2541 mImpl->mOperationsPending = ALL_OPERATIONS;
2543 // Update the rest of the model during size negotiation
2544 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2548 void Controller::ClearModelData()
2550 // n.b. This does not Clear the mText from mLogicalModel
2551 mImpl->mLogicalModel->mScriptRuns.Clear();
2552 mImpl->mLogicalModel->mFontRuns.Clear();
2553 mImpl->mLogicalModel->mLineBreakInfo.Clear();
2554 mImpl->mLogicalModel->mWordBreakInfo.Clear();
2555 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
2556 mImpl->mLogicalModel->mCharacterDirections.Clear();
2557 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
2558 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
2559 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
2560 mImpl->mVisualModel->mGlyphs.Clear();
2561 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2562 mImpl->mVisualModel->mCharactersToGlyph.Clear();
2563 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2564 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2565 mImpl->mVisualModel->mGlyphPositions.Clear();
2566 mImpl->mVisualModel->mLines.Clear();
2567 mImpl->mVisualModel->mColorRuns.Clear();
2568 mImpl->mVisualModel->ClearCaches();
2571 void Controller::ClearFontData()
2573 if( mImpl->mFontDefaults )
2575 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2577 mImpl->mLogicalModel->mFontRuns.Clear();
2578 mImpl->mVisualModel->mGlyphs.Clear();
2579 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2580 mImpl->mVisualModel->mCharactersToGlyph.Clear();
2581 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2582 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2583 mImpl->mVisualModel->mGlyphPositions.Clear();
2584 mImpl->mVisualModel->mLines.Clear();
2585 mImpl->mVisualModel->ClearCaches();
2588 void Controller::ClearStyleData()
2590 mImpl->mLogicalModel->mColorRuns.Clear();
2591 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2594 Controller::Controller( ControlInterface& controlInterface )
2597 mImpl = new Controller::Impl( controlInterface );
2602 } // namespace Toolkit