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::SetMarkupProcessorEnabled( bool enable )
112 mImpl->mMarkupProcessorEnabled = enable;
115 bool Controller::IsMarkupProcessorEnabled() const
117 return mImpl->mMarkupProcessorEnabled;
120 void Controller::SetText( const std::string& text )
122 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
124 // Reset keyboard as text changed
125 mImpl->ResetImfManager();
127 // Remove the previously set text and style.
133 CharacterIndex lastCursorIndex = 0u;
135 if( NULL != mImpl->mEventData )
137 // If popup shown then hide it by switching to Editing state
138 if( ( EventData::SELECTING == mImpl->mEventData->mState ) ||
139 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
140 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
141 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
143 mImpl->ChangeState( EventData::EDITING );
149 MarkupProcessData markupProcessData( mImpl->mLogicalModel->mColorRuns,
150 mImpl->mLogicalModel->mFontDescriptionRuns );
152 Length textSize = 0u;
153 const uint8_t* utf8 = NULL;
154 if( mImpl->mMarkupProcessorEnabled )
156 ProcessMarkupString( text, markupProcessData );
157 textSize = markupProcessData.markupProcessedText.size();
159 // This is a bit horrible but std::string returns a (signed) char*
160 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
164 textSize = text.size();
166 // This is a bit horrible but std::string returns a (signed) char*
167 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
170 // Convert text into UTF-32
171 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
172 utf32Characters.Resize( textSize );
174 // Transform a text array encoded in utf8 into an array encoded in utf32.
175 // It returns the actual number of characters.
176 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
177 utf32Characters.Resize( characterCount );
179 DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
180 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mLogicalModel->mText.Count() );
182 // To reset the cursor position
183 lastCursorIndex = characterCount;
185 // Update the rest of the model during size negotiation
186 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
188 // The natural size needs to be re-calculated.
189 mImpl->mRecalculateNaturalSize = true;
191 // Apply modifications to the model
192 mImpl->mOperationsPending = ALL_OPERATIONS;
196 ShowPlaceholderText();
199 // Resets the cursor position.
200 ResetCursorPosition( lastCursorIndex );
202 // Scrolls the text to make the cursor visible.
203 ResetScrollPosition();
205 mImpl->RequestRelayout();
207 if( NULL != mImpl->mEventData )
209 // Cancel previously queued events
210 mImpl->mEventData->mEventQueue.clear();
213 // Notify IMF as text changed
216 // Do this last since it provides callbacks into application code
217 mImpl->mControlInterface.TextChanged();
220 void Controller::GetText( std::string& text ) const
222 if( !mImpl->IsShowingPlaceholderText() )
224 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
226 if( 0u != utf32Characters.Count() )
228 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
233 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
237 unsigned int Controller::GetLogicalCursorPosition() const
239 if( NULL != mImpl->mEventData )
241 return mImpl->mEventData->mPrimaryCursorPosition;
247 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
249 if( NULL != mImpl->mEventData )
251 if( PLACEHOLDER_TYPE_INACTIVE == type )
253 mImpl->mEventData->mPlaceholderTextInactive = text;
257 mImpl->mEventData->mPlaceholderTextActive = text;
260 // Update placeholder if there is no text
261 if( mImpl->IsShowingPlaceholderText() ||
262 ( 0u == mImpl->mLogicalModel->mText.Count() ) )
264 ShowPlaceholderText();
269 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
271 if( NULL != mImpl->mEventData )
273 if( PLACEHOLDER_TYPE_INACTIVE == type )
275 text = mImpl->mEventData->mPlaceholderTextInactive;
279 text = mImpl->mEventData->mPlaceholderTextActive;
284 void Controller::SetMaximumNumberOfCharacters( Length maxCharacters )
286 mImpl->mMaximumNumberOfCharacters = maxCharacters;
289 int Controller::GetMaximumNumberOfCharacters()
291 return mImpl->mMaximumNumberOfCharacters;
294 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
296 if( NULL == mImpl->mFontDefaults )
298 mImpl->mFontDefaults = new FontDefaults();
301 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
302 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
303 mImpl->mFontDefaults->familyDefined = true;
305 // Clear the font-specific data
308 mImpl->mOperationsPending = ALL_OPERATIONS;
309 mImpl->mRecalculateNaturalSize = true;
311 mImpl->RequestRelayout();
314 const std::string& Controller::GetDefaultFontFamily() const
316 if( NULL != mImpl->mFontDefaults )
318 return mImpl->mFontDefaults->mFontDescription.family;
324 void Controller::SetDefaultFontStyle( const std::string& style )
326 if( NULL == mImpl->mFontDefaults )
328 mImpl->mFontDefaults = new FontDefaults();
331 mImpl->mFontDefaults->mFontStyle = style;
334 const std::string& Controller::GetDefaultFontStyle() const
336 if( NULL != mImpl->mFontDefaults )
338 return mImpl->mFontDefaults->mFontStyle;
344 void Controller::SetDefaultFontWeight( FontWeight weight )
346 if( NULL == mImpl->mFontDefaults )
348 mImpl->mFontDefaults = new FontDefaults();
351 mImpl->mFontDefaults->mFontDescription.weight = weight;
352 mImpl->mFontDefaults->weightDefined = true;
354 // Clear the font-specific data
357 mImpl->mOperationsPending = ALL_OPERATIONS;
358 mImpl->mRecalculateNaturalSize = true;
360 mImpl->RequestRelayout();
363 FontWeight Controller::GetDefaultFontWeight() const
365 if( NULL != mImpl->mFontDefaults )
367 return mImpl->mFontDefaults->mFontDescription.weight;
370 return TextAbstraction::FontWeight::NORMAL;
373 void Controller::SetDefaultFontWidth( FontWidth width )
375 if( NULL == mImpl->mFontDefaults )
377 mImpl->mFontDefaults = new FontDefaults();
380 mImpl->mFontDefaults->mFontDescription.width = width;
381 mImpl->mFontDefaults->widthDefined = true;
383 // Clear the font-specific data
386 mImpl->mOperationsPending = ALL_OPERATIONS;
387 mImpl->mRecalculateNaturalSize = true;
389 mImpl->RequestRelayout();
392 FontWidth Controller::GetDefaultFontWidth() const
394 if( NULL != mImpl->mFontDefaults )
396 return mImpl->mFontDefaults->mFontDescription.width;
399 return TextAbstraction::FontWidth::NORMAL;
402 void Controller::SetDefaultFontSlant( FontSlant slant )
404 if( NULL == mImpl->mFontDefaults )
406 mImpl->mFontDefaults = new FontDefaults();
409 mImpl->mFontDefaults->mFontDescription.slant = slant;
410 mImpl->mFontDefaults->slantDefined = true;
412 // Clear the font-specific data
415 mImpl->mOperationsPending = ALL_OPERATIONS;
416 mImpl->mRecalculateNaturalSize = true;
418 mImpl->RequestRelayout();
421 FontSlant Controller::GetDefaultFontSlant() const
423 if( NULL != mImpl->mFontDefaults )
425 return mImpl->mFontDefaults->mFontDescription.slant;
428 return TextAbstraction::FontSlant::NORMAL;
431 void Controller::SetDefaultPointSize( float pointSize )
433 if( NULL == mImpl->mFontDefaults )
435 mImpl->mFontDefaults = new FontDefaults();
438 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
439 mImpl->mFontDefaults->sizeDefined = true;
441 unsigned int horizontalDpi( 0u );
442 unsigned int verticalDpi( 0u );
443 mImpl->mFontClient.GetDpi( horizontalDpi, verticalDpi );
445 // Adjust the metrics if the fixed-size font should be down-scaled
446 int maxEmojiSize( pointSize/POINTS_PER_INCH * verticalDpi );
447 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultPointSize %p setting MaxEmojiSize %d\n", this, maxEmojiSize );
448 mImpl->mMetrics->SetMaxEmojiSize( maxEmojiSize );
450 // Clear the font-specific data
453 mImpl->mOperationsPending = ALL_OPERATIONS;
454 mImpl->mRecalculateNaturalSize = true;
456 mImpl->RequestRelayout();
459 float Controller::GetDefaultPointSize() const
461 if( NULL != mImpl->mFontDefaults )
463 return mImpl->mFontDefaults->mDefaultPointSize;
469 void Controller::UpdateAfterFontChange( std::string& newDefaultFont )
471 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange");
473 if( !mImpl->mFontDefaults->familyDefined ) // If user defined font then should not update when system font changes
475 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
477 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
478 mImpl->UpdateModel( ALL_OPERATIONS );
479 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
480 mImpl->mRecalculateNaturalSize = true;
481 mImpl->RequestRelayout();
485 void Controller::SetTextColor( const Vector4& textColor )
487 mImpl->mTextColor = textColor;
489 if( !mImpl->IsShowingPlaceholderText() )
491 mImpl->mVisualModel->SetTextColor( textColor );
493 mImpl->RequestRelayout();
497 const Vector4& Controller::GetTextColor() const
499 return mImpl->mTextColor;
502 bool Controller::RemoveText( int cursorOffset, int numberOfChars )
504 bool removed = false;
506 if( NULL == mImpl->mEventData )
511 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfChars %d\n",
512 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfChars );
514 if( !mImpl->IsShowingPlaceholderText() )
516 // Delete at current cursor position
517 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
518 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
520 CharacterIndex cursorIndex = oldCursorIndex;
522 // Validate the cursor position & number of characters
523 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
525 cursorIndex = oldCursorIndex + cursorOffset;
528 if( ( cursorIndex + numberOfChars ) > currentText.Count() )
530 numberOfChars = currentText.Count() - cursorIndex;
533 if( ( cursorIndex + numberOfChars ) <= currentText.Count() )
535 // Update the input style and remove the text's style before removing the text.
537 // Set first the default input style.
538 mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
540 // Update the input style.
541 mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
543 // Remove the text's style before removing the text.
544 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfChars );
546 // Remove the characters.
547 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
548 Vector<Character>::Iterator last = first + numberOfChars;
550 currentText.Erase( first, last );
552 // Cursor position retreat
553 oldCursorIndex = cursorIndex;
555 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfChars );
563 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
565 if( NULL != mImpl->mEventData )
567 mImpl->mEventData->mPlaceholderTextColor = textColor;
570 if( mImpl->IsShowingPlaceholderText() )
572 mImpl->mVisualModel->SetTextColor( textColor );
573 mImpl->RequestRelayout();
577 const Vector4& Controller::GetPlaceholderTextColor() const
579 if( NULL != mImpl->mEventData )
581 return mImpl->mEventData->mPlaceholderTextColor;
587 void Controller::SetShadowOffset( const Vector2& shadowOffset )
589 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
591 mImpl->RequestRelayout();
594 const Vector2& Controller::GetShadowOffset() const
596 return mImpl->mVisualModel->GetShadowOffset();
599 void Controller::SetShadowColor( const Vector4& shadowColor )
601 mImpl->mVisualModel->SetShadowColor( shadowColor );
603 mImpl->RequestRelayout();
606 const Vector4& Controller::GetShadowColor() const
608 return mImpl->mVisualModel->GetShadowColor();
611 void Controller::SetUnderlineColor( const Vector4& color )
613 mImpl->mVisualModel->SetUnderlineColor( color );
615 mImpl->RequestRelayout();
618 const Vector4& Controller::GetUnderlineColor() const
620 return mImpl->mVisualModel->GetUnderlineColor();
623 void Controller::SetUnderlineEnabled( bool enabled )
625 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
627 mImpl->RequestRelayout();
630 bool Controller::IsUnderlineEnabled() const
632 return mImpl->mVisualModel->IsUnderlineEnabled();
635 void Controller::SetUnderlineHeight( float height )
637 mImpl->mVisualModel->SetUnderlineHeight( height );
639 mImpl->RequestRelayout();
642 float Controller::GetUnderlineHeight() const
644 return mImpl->mVisualModel->GetUnderlineHeight();
647 void Controller::SetInputColor( const Vector4& color )
649 if( NULL != mImpl->mEventData )
651 mImpl->mEventData->mInputStyle.textColor = color;
653 if( EventData::SELECTING == mImpl->mEventData->mState )
655 const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
657 // Get start and end position of selection
658 const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
659 const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
661 // Add the color run.
662 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
663 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
665 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
666 colorRun.color = color;
667 colorRun.characterRun.characterIndex = startOfSelectedText;
668 colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
670 // Request to relayout.
671 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
672 mImpl->RequestRelayout();
677 const Vector4& Controller::GetInputColor() const
679 if( NULL != mImpl->mEventData )
681 return mImpl->mEventData->mInputStyle.textColor;
684 // Return the default text's color if there is no EventData.
685 return mImpl->mTextColor;
689 void Controller::SetInputFontFamily( const std::string& fontFamily )
691 if( NULL != mImpl->mEventData )
693 mImpl->mEventData->mInputStyle.familyName = fontFamily;
694 mImpl->mEventData->mInputStyle.familyDefined = true;
696 if( EventData::SELECTING == mImpl->mEventData->mState )
698 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
699 mImpl->mLogicalModel );
701 fontDescriptionRun.familyLength = fontFamily.size();
702 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
703 memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
704 fontDescriptionRun.familyDefined = true;
706 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
708 // Request to relayout.
709 mImpl->mOperationsPending = ALL_OPERATIONS;
710 mImpl->mRecalculateNaturalSize = true;
711 mImpl->RequestRelayout();
713 // As the font changes, recalculate the handle positions is needed.
714 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
715 mImpl->mEventData->mUpdateRightSelectionPosition = true;
716 mImpl->mEventData->mScrollAfterUpdatePosition = true;
721 const std::string& Controller::GetInputFontFamily() const
723 if( NULL != mImpl->mEventData )
725 return mImpl->mEventData->mInputStyle.familyName;
728 // Return the default font's family if there is no EventData.
729 return GetDefaultFontFamily();
732 void Controller::SetInputFontStyle( const std::string& fontStyle )
734 if( NULL != mImpl->mEventData )
736 mImpl->mEventData->mInputStyle.fontStyle = fontStyle;
740 const std::string& Controller::GetInputFontStyle() const
742 if( NULL != mImpl->mEventData )
744 return mImpl->mEventData->mInputStyle.fontStyle;
747 // Return the default font's style if there is no EventData.
748 return GetDefaultFontStyle();
751 void Controller::SetInputFontWeight( FontWeight weight )
753 if( NULL != mImpl->mEventData )
755 mImpl->mEventData->mInputStyle.weight = weight;
756 mImpl->mEventData->mInputStyle.weightDefined = true;
758 if( EventData::SELECTING == mImpl->mEventData->mState )
760 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
761 mImpl->mLogicalModel );
763 fontDescriptionRun.weight = weight;
764 fontDescriptionRun.weightDefined = true;
766 // Request to relayout.
767 mImpl->mOperationsPending = ALL_OPERATIONS;
768 mImpl->mRecalculateNaturalSize = true;
769 mImpl->RequestRelayout();
771 // As the font might change, recalculate the handle positions is needed.
772 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
773 mImpl->mEventData->mUpdateRightSelectionPosition = true;
774 mImpl->mEventData->mScrollAfterUpdatePosition = true;
779 FontWeight Controller::GetInputFontWeight() const
781 if( NULL != mImpl->mEventData )
783 return mImpl->mEventData->mInputStyle.weight;
786 return GetDefaultFontWeight();
789 void Controller::SetInputFontWidth( FontWidth width )
791 if( NULL != mImpl->mEventData )
793 mImpl->mEventData->mInputStyle.width = width;
794 mImpl->mEventData->mInputStyle.widthDefined = true;
796 if( EventData::SELECTING == mImpl->mEventData->mState )
798 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
799 mImpl->mLogicalModel );
801 fontDescriptionRun.width = width;
802 fontDescriptionRun.widthDefined = true;
804 // Request to relayout.
805 mImpl->mOperationsPending = ALL_OPERATIONS;
806 mImpl->mRecalculateNaturalSize = true;
807 mImpl->RequestRelayout();
809 // As the font might change, recalculate the handle positions is needed.
810 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
811 mImpl->mEventData->mUpdateRightSelectionPosition = true;
812 mImpl->mEventData->mScrollAfterUpdatePosition = true;
817 FontWidth Controller::GetInputFontWidth() const
819 if( NULL != mImpl->mEventData )
821 return mImpl->mEventData->mInputStyle.width;
824 return GetDefaultFontWidth();
827 void Controller::SetInputFontSlant( FontSlant slant )
829 if( NULL != mImpl->mEventData )
831 mImpl->mEventData->mInputStyle.slant = slant;
832 mImpl->mEventData->mInputStyle.slantDefined = true;
834 if( EventData::SELECTING == mImpl->mEventData->mState )
836 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
837 mImpl->mLogicalModel );
839 fontDescriptionRun.slant = slant;
840 fontDescriptionRun.slantDefined = true;
842 // Request to relayout.
843 mImpl->mOperationsPending = ALL_OPERATIONS;
844 mImpl->mRecalculateNaturalSize = true;
845 mImpl->RequestRelayout();
847 // As the font might change, recalculate the handle positions is needed.
848 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
849 mImpl->mEventData->mUpdateRightSelectionPosition = true;
850 mImpl->mEventData->mScrollAfterUpdatePosition = true;
855 FontSlant Controller::GetInputFontSlant() const
857 if( NULL != mImpl->mEventData )
859 return mImpl->mEventData->mInputStyle.slant;
862 return GetDefaultFontSlant();
865 void Controller::SetInputFontPointSize( float size )
867 if( NULL != mImpl->mEventData )
869 mImpl->mEventData->mInputStyle.size = size;
871 if( EventData::SELECTING == mImpl->mEventData->mState )
873 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
874 mImpl->mLogicalModel );
876 fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
877 fontDescriptionRun.sizeDefined = true;
879 // Request to relayout.
880 mImpl->mOperationsPending = ALL_OPERATIONS;
881 mImpl->mRecalculateNaturalSize = true;
882 mImpl->RequestRelayout();
884 // As the font might change, recalculate the handle positions is needed.
885 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
886 mImpl->mEventData->mUpdateRightSelectionPosition = true;
887 mImpl->mEventData->mScrollAfterUpdatePosition = true;
892 float Controller::GetInputFontPointSize() const
894 if( NULL != mImpl->mEventData )
896 return mImpl->mEventData->mInputStyle.size;
899 // Return the default font's point size if there is no EventData.
900 return GetDefaultPointSize();
903 void Controller::SetEnableCursorBlink( bool enable )
905 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
907 if( NULL != mImpl->mEventData )
909 mImpl->mEventData->mCursorBlinkEnabled = enable;
912 mImpl->mEventData->mDecorator )
914 mImpl->mEventData->mDecorator->StopCursorBlink();
919 bool Controller::GetEnableCursorBlink() const
921 if( NULL != mImpl->mEventData )
923 return mImpl->mEventData->mCursorBlinkEnabled;
929 const Vector2& Controller::GetScrollPosition() const
931 if( NULL != mImpl->mEventData )
933 return mImpl->mEventData->mScrollPosition;
936 return Vector2::ZERO;
939 const Vector2& Controller::GetAlignmentOffset() const
941 return mImpl->mAlignmentOffset;
944 Vector3 Controller::GetNaturalSize()
946 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
949 // Make sure the model is up-to-date before layouting
950 ProcessModifyEvents();
952 if( mImpl->mRecalculateNaturalSize )
954 // Operations that can be done only once until the text changes.
955 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
963 // Make sure the model is up-to-date before layouting
964 mImpl->UpdateModel( onlyOnceOperations );
966 // Operations that need to be done if the size changes.
967 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
971 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
972 static_cast<OperationsMask>( onlyOnceOperations |
974 naturalSize.GetVectorXY() );
976 // Do not do again the only once operations.
977 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
979 // Do the size related operations again.
980 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
982 // Stores the natural size to avoid recalculate it again
983 // unless the text/style changes.
984 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
986 mImpl->mRecalculateNaturalSize = false;
988 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
992 naturalSize = mImpl->mVisualModel->GetNaturalSize();
994 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
997 naturalSize.x = ConvertToEven( naturalSize.x );
998 naturalSize.y = ConvertToEven( naturalSize.y );
1003 float Controller::GetHeightForWidth( float width )
1005 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1006 // Make sure the model is up-to-date before layouting
1007 ProcessModifyEvents();
1010 if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
1012 // Operations that can be done only once until the text changes.
1013 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1020 GET_GLYPH_METRICS );
1021 // Make sure the model is up-to-date before layouting
1022 mImpl->UpdateModel( onlyOnceOperations );
1024 // Operations that need to be done if the size changes.
1025 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1029 DoRelayout( Size( width, MAX_FLOAT ),
1030 static_cast<OperationsMask>( onlyOnceOperations |
1034 // Do not do again the only once operations.
1035 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1037 // Do the size related operations again.
1038 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1039 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1043 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1044 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1047 return layoutSize.height;
1050 bool Controller::Relayout( const Size& size )
1052 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
1054 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1056 bool glyphsRemoved( false );
1057 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1059 mImpl->mVisualModel->mGlyphPositions.Clear();
1060 glyphsRemoved = true;
1062 // Not worth to relayout if width or height is equal to zero.
1063 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1064 return glyphsRemoved;
1067 // Whether a new size has been set.
1068 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1072 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1074 // Layout operations that need to be done if the size changes.
1075 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1078 UPDATE_ACTUAL_SIZE |
1081 mImpl->mVisualModel->mControlSize = size;
1084 // Whether there are modify events.
1085 const bool isModifyEventsEmpty = 0u == mImpl->mModifyEvents.Count();
1087 // Make sure the model is up-to-date before layouting.
1088 ProcessModifyEvents();
1089 mImpl->UpdateModel( mImpl->mOperationsPending );
1091 // Style operations that need to be done if the text is modified.
1092 if( !isModifyEventsEmpty )
1094 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1098 // Apply the style runs if text is modified.
1099 bool updated = mImpl->UpdateModelStyle( mImpl->mOperationsPending );
1103 updated = DoRelayout( mImpl->mVisualModel->mControlSize,
1104 mImpl->mOperationsPending,
1105 layoutSize ) || updated;
1107 // Do not re-do any operation until something changes.
1108 mImpl->mOperationsPending = NO_OPERATION;
1110 // Whether the text control is editable
1111 const bool isEditable = NULL != mImpl->mEventData;
1113 // Keep the current offset and alignment as it will be used to update the decorator's positions (if the size changes).
1115 if( newSize && isEditable )
1117 offset = mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition;
1120 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
1121 CalculateTextAlignment( size );
1127 // If there is a new size, the scroll position needs to be clamped.
1128 mImpl->ClampHorizontalScroll( layoutSize );
1130 // Update the decorator's positions is needed if there is a new size.
1131 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
1134 // Move the cursor, grab handle etc.
1135 updated = mImpl->ProcessInputEvents() || updated;
1138 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1142 void Controller::ProcessModifyEvents()
1144 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
1146 if( 0u == events.Count() )
1152 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
1153 endIt = events.End();
1157 const ModifyEvent& event = *it;
1159 if( ModifyEvent::TEXT_REPLACED == event.type )
1161 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
1162 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
1164 TextReplacedEvent();
1166 else if( ModifyEvent::TEXT_INSERTED == event.type )
1168 TextInsertedEvent();
1170 else if( ModifyEvent::TEXT_DELETED == event.type )
1172 // Placeholder-text cannot be deleted
1173 if( !mImpl->IsShowingPlaceholderText() )
1180 if( NULL != mImpl->mEventData )
1182 // When the text is being modified, delay cursor blinking
1183 mImpl->mEventData->mDecorator->DelayCursorBlink();
1186 // Discard temporary text
1190 void Controller::ResetText()
1193 mImpl->mLogicalModel->mText.Clear();
1196 // We have cleared everything including the placeholder-text
1197 mImpl->PlaceholderCleared();
1199 // The natural size needs to be re-calculated.
1200 mImpl->mRecalculateNaturalSize = true;
1202 // Apply modifications to the model
1203 mImpl->mOperationsPending = ALL_OPERATIONS;
1206 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
1208 // Reset the cursor position
1209 if( NULL != mImpl->mEventData )
1211 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
1213 // Update the cursor if it's in editing mode.
1214 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1216 mImpl->mEventData->mUpdateCursorPosition = true;
1221 void Controller::ResetScrollPosition()
1223 if( NULL != mImpl->mEventData )
1225 // Reset the scroll position.
1226 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
1227 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1231 void Controller::TextReplacedEvent()
1236 // The natural size needs to be re-calculated.
1237 mImpl->mRecalculateNaturalSize = true;
1239 // Apply modifications to the model
1240 mImpl->mOperationsPending = ALL_OPERATIONS;
1241 mImpl->UpdateModel( ALL_OPERATIONS );
1242 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
1244 UPDATE_ACTUAL_SIZE |
1248 void Controller::TextInsertedEvent()
1250 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
1252 if( NULL == mImpl->mEventData )
1257 // TODO - Optimize this
1260 // The natural size needs to be re-calculated.
1261 mImpl->mRecalculateNaturalSize = true;
1263 // Apply modifications to the model; TODO - Optimize this
1264 mImpl->mOperationsPending = ALL_OPERATIONS;
1265 mImpl->UpdateModel( ALL_OPERATIONS );
1266 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
1268 UPDATE_ACTUAL_SIZE |
1271 // Queue a cursor reposition event; this must wait until after DoRelayout()
1272 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1274 mImpl->mEventData->mUpdateCursorPosition = true;
1275 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1279 void Controller::TextDeletedEvent()
1281 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
1283 if( NULL == mImpl->mEventData )
1288 // TODO - Optimize this
1291 // The natural size needs to be re-calculated.
1292 mImpl->mRecalculateNaturalSize = true;
1294 // Apply modifications to the model; TODO - Optimize this
1295 mImpl->mOperationsPending = ALL_OPERATIONS;
1296 mImpl->UpdateModel( ALL_OPERATIONS );
1297 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
1299 UPDATE_ACTUAL_SIZE |
1302 // Queue a cursor reposition event; this must wait until after DoRelayout()
1303 mImpl->mEventData->mUpdateCursorPosition = true;
1304 if( 0u != mImpl->mLogicalModel->mText.Count() )
1306 mImpl->mEventData->mScrollAfterDelete = true;
1310 bool Controller::DoRelayout( const Size& size,
1311 OperationsMask operationsRequired,
1314 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
1315 bool viewUpdated( false );
1317 // Calculate the operations to be done.
1318 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
1320 if( LAYOUT & operations )
1322 // Some vectors with data needed to layout and reorder may be void
1323 // after the first time the text has been laid out.
1324 // Fill the vectors again.
1326 const Length numberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
1328 if( 0u == numberOfGlyphs )
1330 // Nothing else to do if there is no glyphs.
1331 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1335 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1336 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1337 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1338 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1339 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1340 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1341 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1342 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1343 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1345 // Set the layout parameters.
1346 LayoutParameters layoutParameters( size,
1348 lineBreakInfo.Begin(),
1349 wordBreakInfo.Begin(),
1350 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1352 glyphsToCharactersMap.Begin(),
1353 charactersPerGlyph.Begin(),
1354 charactersToGlyph.Begin(),
1355 glyphsPerCharacter.Begin(),
1358 // The laid-out lines.
1359 // It's not possible to know in how many lines the text is going to be laid-out,
1360 // but it can be resized at least with the number of 'paragraphs' to avoid
1361 // some re-allocations.
1362 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1364 // Delete any previous laid out lines before setting the new ones.
1367 // The capacity of the bidirectional paragraph info is the number of paragraphs.
1368 lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
1370 // Resize the vector of positions to have the same size than the vector of glyphs.
1371 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1372 glyphPositions.Resize( numberOfGlyphs );
1374 // Whether the last character is a new paragraph character.
1375 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1377 // The initial glyph and the number of glyphs to layout.
1378 layoutParameters.startGlyphIndex = 0u;
1379 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1381 // Update the visual model.
1382 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1389 // Reorder the lines
1390 if( REORDER & operations )
1392 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1393 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1395 // Check first if there are paragraphs with bidirectional info.
1396 if( 0u != bidirectionalInfo.Count() )
1399 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1400 const CharacterIndex startIndex = 0u;
1401 const Length requestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1403 // Reorder the lines.
1404 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1405 ReorderLines( bidirectionalInfo,
1407 requestedNumberOfCharacters,
1409 bidirectionalLineInfo );
1411 // Set the bidirectional info per line into the layout parameters.
1412 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1413 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1415 // Set the bidirectional info into the model.
1416 mImpl->mLogicalModel->SetVisualToLogicalMap( layoutParameters.lineBidirectionalInfoRunsBuffer,
1417 layoutParameters.numberOfBidirectionalInfoRuns,
1419 requestedNumberOfCharacters );
1421 // Re-layout the text. Reorder those lines with right to left characters.
1422 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1424 requestedNumberOfCharacters,
1427 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
1428 for( Vector<BidirectionalLineInfoRun>::Iterator it = bidirectionalLineInfo.Begin(),
1429 endIt = bidirectionalLineInfo.End();
1433 BidirectionalLineInfoRun& bidiLineInfo = *it;
1435 free( bidiLineInfo.visualToLogicalMap );
1436 bidiLineInfo.visualToLogicalMap = NULL;
1439 bidirectionalLineInfo.Clear();
1443 // Sets the actual size.
1444 if( UPDATE_ACTUAL_SIZE & operations )
1446 mImpl->mVisualModel->SetLayoutSize( layoutSize );
1452 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1455 if( ALIGN & operations )
1457 // The laid-out lines.
1458 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1460 const CharacterIndex startIndex = 0u;
1461 const Length requestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1463 mImpl->mLayoutEngine.Align( size,
1465 requestedNumberOfCharacters,
1471 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1475 void Controller::SetMultiLineEnabled( bool enable )
1477 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1479 if( layout != mImpl->mLayoutEngine.GetLayout() )
1481 // Set the layout type.
1482 mImpl->mLayoutEngine.SetLayout( layout );
1484 // Set the flags to redo the layout operations
1485 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1486 UPDATE_ACTUAL_SIZE |
1490 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1492 mImpl->RequestRelayout();
1496 bool Controller::IsMultiLineEnabled() const
1498 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1501 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1503 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1505 // Set the alignment.
1506 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1508 // Set the flag to redo the alignment operation.
1509 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1511 mImpl->RequestRelayout();
1515 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1517 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1520 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1522 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1524 // Set the alignment.
1525 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1527 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1529 mImpl->RequestRelayout();
1533 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1535 return mImpl->mLayoutEngine.GetVerticalAlignment();
1538 void Controller::CalculateTextAlignment( const Size& controlSize )
1540 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1542 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1544 // Get the line height of the default font.
1545 layoutSize.height = mImpl->GetDefaultFontLineHeight();
1548 if( LayoutEngine::SINGLE_LINE_BOX == mImpl->mLayoutEngine.GetLayout() )
1550 // Get the direction of the first character.
1551 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1553 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1554 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1555 if( firstParagraphDirection )
1557 switch( horizontalAlignment )
1559 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1561 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1564 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1569 case LayoutEngine::HORIZONTAL_ALIGN_END:
1571 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1577 switch( horizontalAlignment )
1579 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1581 mImpl->mAlignmentOffset.x = 0.f;
1584 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1586 mImpl->mAlignmentOffset.x = floorf( 0.5f * ( controlSize.width - layoutSize.width ) ); // try to avoid pixel alignment.
1589 case LayoutEngine::HORIZONTAL_ALIGN_END:
1591 mImpl->mAlignmentOffset.x = controlSize.width - layoutSize.width;
1597 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1598 switch( verticalAlignment )
1600 case LayoutEngine::VERTICAL_ALIGN_TOP:
1602 mImpl->mAlignmentOffset.y = 0.f;
1605 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1607 mImpl->mAlignmentOffset.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
1610 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1612 mImpl->mAlignmentOffset.y = controlSize.height - layoutSize.height;
1618 LayoutEngine& Controller::GetLayoutEngine()
1620 return mImpl->mLayoutEngine;
1623 View& Controller::GetView()
1625 return mImpl->mView;
1628 void Controller::KeyboardFocusGainEvent()
1630 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1632 if( NULL != mImpl->mEventData )
1634 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1635 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1637 mImpl->ChangeState( EventData::EDITING );
1638 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1641 if( mImpl->IsShowingPlaceholderText() )
1643 // Show alternative placeholder-text when editing
1644 ShowPlaceholderText();
1647 mImpl->RequestRelayout();
1651 void Controller::KeyboardFocusLostEvent()
1653 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1655 if( NULL != mImpl->mEventData )
1657 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1659 mImpl->ChangeState( EventData::INACTIVE );
1661 if( !mImpl->IsShowingRealText() )
1663 // Revert to regular placeholder-text when not editing
1664 ShowPlaceholderText();
1668 mImpl->RequestRelayout();
1671 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1673 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1675 bool textChanged( false );
1677 if( ( NULL != mImpl->mEventData ) &&
1678 ( keyEvent.state == KeyEvent::Down ) )
1680 int keyCode = keyEvent.keyCode;
1681 const std::string& keyString = keyEvent.keyPressed;
1683 // Pre-process to separate modifying events from non-modifying input events.
1684 if( Dali::DALI_KEY_ESCAPE == keyCode )
1686 // Escape key is a special case which causes focus loss
1687 KeyboardFocusLostEvent();
1689 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
1690 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1691 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
1692 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
1694 Event event( Event::CURSOR_KEY_EVENT );
1695 event.p1.mInt = keyCode;
1696 mImpl->mEventData->mEventQueue.push_back( event );
1698 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1700 textChanged = BackspaceKeyEvent();
1702 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1704 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1705 // Avoids calling the InsertText() method which can delete selected text
1707 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1708 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1710 mImpl->ChangeState( EventData::INACTIVE );
1711 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1712 // Avoids calling the InsertText() method which can delete selected text
1714 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1716 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1717 // and a character is typed after the type of a upper case latin character.
1723 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1725 // IMF manager is no longer handling key-events
1726 mImpl->ClearPreEditFlag();
1728 InsertText( keyString, COMMIT );
1732 if( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1733 ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1735 mImpl->ChangeState( EventData::EDITING );
1738 mImpl->RequestRelayout();
1743 // Do this last since it provides callbacks into application code
1744 mImpl->mControlInterface.TextChanged();
1750 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1752 bool removedPrevious( false );
1753 bool maxLengthReached( false );
1755 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1757 if( NULL == mImpl->mEventData )
1762 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1763 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1764 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1766 // TODO: At the moment the underline runs are only for pre-edit.
1767 mImpl->mVisualModel->mUnderlineRuns.Clear();
1769 Vector<Character> utf32Characters;
1770 Length characterCount( 0u );
1772 // Remove the previous IMF pre-edit (predicitive text)
1773 if( mImpl->mEventData->mPreEditFlag &&
1774 ( 0 != mImpl->mEventData->mPreEditLength ) )
1776 CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1778 removedPrevious = RemoveText( -static_cast<int>(offset), mImpl->mEventData->mPreEditLength );
1780 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1781 mImpl->mEventData->mPreEditLength = 0;
1785 // Remove the previous Selection
1786 removedPrevious = RemoveSelectedText();
1791 // Convert text into UTF-32
1792 utf32Characters.Resize( text.size() );
1794 // This is a bit horrible but std::string returns a (signed) char*
1795 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1797 // Transform a text array encoded in utf8 into an array encoded in utf32.
1798 // It returns the actual number of characters.
1799 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1800 utf32Characters.Resize( characterCount );
1802 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1803 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1806 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1808 // The placeholder text is no longer needed
1809 if( mImpl->IsShowingPlaceholderText() )
1814 mImpl->ChangeState( EventData::EDITING );
1816 // Handle the IMF (predicitive text) state changes
1817 if( COMMIT == type )
1819 // IMF manager is no longer handling key-events
1820 mImpl->ClearPreEditFlag();
1824 if( !mImpl->mEventData->mPreEditFlag )
1826 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1828 // Record the start of the pre-edit text
1829 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1832 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1833 mImpl->mEventData->mPreEditFlag = true;
1835 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1838 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1840 // Restrict new text to fit within Maximum characters setting
1841 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1842 maxLengthReached = ( characterCount > maxSizeOfNewText );
1844 // The cursor position.
1845 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1847 // Update the text's style.
1849 // Updates the text style runs by adding characters.
1850 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
1852 // Get the character index from the cursor index.
1853 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
1855 // Retrieve the text's style for the given index.
1857 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
1859 // Whether to add a new text color run.
1860 const bool addColorRun = style.textColor != mImpl->mEventData->mInputStyle.textColor;
1862 // Whether to add a new font run.
1863 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
1864 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
1865 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
1866 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
1867 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
1872 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
1873 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
1875 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
1876 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
1877 colorRun.characterRun.characterIndex = cursorIndex;
1878 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1881 if( addFontNameRun ||
1887 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
1888 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
1890 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
1892 if( addFontNameRun )
1894 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
1895 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
1896 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
1897 fontDescriptionRun.familyDefined = true;
1899 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
1902 if( addFontWeightRun )
1904 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
1905 fontDescriptionRun.weightDefined = true;
1908 if( addFontWidthRun )
1910 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
1911 fontDescriptionRun.widthDefined = true;
1914 if( addFontSlantRun )
1916 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
1917 fontDescriptionRun.slantDefined = true;
1920 if( addFontSizeRun )
1922 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
1923 fontDescriptionRun.sizeDefined = true;
1926 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
1927 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1930 // Insert at current cursor position.
1931 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1933 if( cursorIndex < numberOfCharactersInModel )
1935 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1939 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1942 cursorIndex += maxSizeOfNewText;
1944 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1947 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
1948 mImpl->IsPlaceholderAvailable() )
1950 // Show place-holder if empty after removing the pre-edit text
1951 ShowPlaceholderText();
1952 mImpl->mEventData->mUpdateCursorPosition = true;
1953 mImpl->ClearPreEditFlag();
1955 else if( removedPrevious ||
1956 ( 0 != utf32Characters.Count() ) )
1958 // Queue an inserted event
1959 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1962 if( maxLengthReached )
1964 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1966 mImpl->ResetImfManager();
1968 // Do this last since it provides callbacks into application code
1969 mImpl->mControlInterface.MaxLengthReached();
1973 bool Controller::RemoveSelectedText()
1975 bool textRemoved( false );
1977 if( EventData::SELECTING == mImpl->mEventData->mState )
1979 std::string removedString;
1980 mImpl->RetrieveSelection( removedString, true );
1982 if( !removedString.empty() )
1985 mImpl->ChangeState( EventData::EDITING );
1992 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1994 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1996 if( NULL != mImpl->mEventData )
1998 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
2000 if( 1u == tapCount )
2002 // This is to avoid unnecessary relayouts when tapping an empty text-field
2003 bool relayoutNeeded( false );
2005 if( ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) ||
2006 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
2008 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE); // If Popup shown hide it here so can be shown again if required.
2011 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
2013 // Already in an active state so show a popup
2014 if( !mImpl->IsClipboardEmpty() )
2016 // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
2017 mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
2021 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2023 relayoutNeeded = true;
2027 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2029 // Hide placeholder text
2033 if( EventData::INACTIVE == mImpl->mEventData->mState )
2035 mImpl->ChangeState( EventData::EDITING );
2037 else if( !mImpl->IsClipboardEmpty() )
2039 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2041 relayoutNeeded = true;
2044 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2045 if( relayoutNeeded )
2047 Event event( Event::TAP_EVENT );
2048 event.p1.mUint = tapCount;
2049 event.p2.mFloat = x;
2050 event.p3.mFloat = y;
2051 mImpl->mEventData->mEventQueue.push_back( event );
2053 mImpl->RequestRelayout();
2056 else if( 2u == tapCount )
2058 if( mImpl->mEventData->mSelectionEnabled &&
2059 mImpl->IsShowingRealText() )
2061 SelectEvent( x, y, false );
2066 // Reset keyboard as tap event has occurred.
2067 mImpl->ResetImfManager();
2070 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2071 // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2073 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2075 if( NULL != mImpl->mEventData )
2077 Event event( Event::PAN_EVENT );
2078 event.p1.mInt = state;
2079 event.p2.mFloat = displacement.x;
2080 event.p3.mFloat = displacement.y;
2081 mImpl->mEventData->mEventQueue.push_back( event );
2083 mImpl->RequestRelayout();
2087 void Controller::LongPressEvent( Gesture::State state, float x, float y )
2089 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2091 if( ( state == Gesture::Started ) &&
2092 ( NULL != mImpl->mEventData ) )
2094 if( !mImpl->IsShowingRealText() )
2096 Event event( Event::LONG_PRESS_EVENT );
2097 event.p1.mInt = state;
2098 mImpl->mEventData->mEventQueue.push_back( event );
2099 mImpl->RequestRelayout();
2103 // The 1st long-press on inactive text-field is treated as tap
2104 if( EventData::INACTIVE == mImpl->mEventData->mState )
2106 mImpl->ChangeState( EventData::EDITING );
2108 Event event( Event::TAP_EVENT );
2110 event.p2.mFloat = x;
2111 event.p3.mFloat = y;
2112 mImpl->mEventData->mEventQueue.push_back( event );
2114 mImpl->RequestRelayout();
2118 // Reset the imf manger to commit the pre-edit before selecting the text.
2119 mImpl->ResetImfManager();
2121 SelectEvent( x, y, false );
2127 void Controller::SelectEvent( float x, float y, bool selectAll )
2129 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2131 if( NULL != mImpl->mEventData )
2133 mImpl->ChangeState( EventData::SELECTING );
2137 Event event( Event::SELECT_ALL );
2138 mImpl->mEventData->mEventQueue.push_back( event );
2142 Event event( Event::SELECT );
2143 event.p2.mFloat = x;
2144 event.p3.mFloat = y;
2145 mImpl->mEventData->mEventQueue.push_back( event );
2148 mImpl->RequestRelayout();
2152 void Controller::GetTargetSize( Vector2& targetSize )
2154 targetSize = mImpl->mVisualModel->mControlSize;
2157 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2159 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2162 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2164 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2166 if( NULL != mImpl->mEventData )
2168 switch( handleType )
2172 Event event( Event::GRAB_HANDLE_EVENT );
2173 event.p1.mUint = state;
2174 event.p2.mFloat = x;
2175 event.p3.mFloat = y;
2177 mImpl->mEventData->mEventQueue.push_back( event );
2180 case LEFT_SELECTION_HANDLE:
2182 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2183 event.p1.mUint = state;
2184 event.p2.mFloat = x;
2185 event.p3.mFloat = y;
2187 mImpl->mEventData->mEventQueue.push_back( event );
2190 case RIGHT_SELECTION_HANDLE:
2192 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2193 event.p1.mUint = state;
2194 event.p2.mFloat = x;
2195 event.p3.mFloat = y;
2197 mImpl->mEventData->mEventQueue.push_back( event );
2200 case LEFT_SELECTION_HANDLE_MARKER:
2201 case RIGHT_SELECTION_HANDLE_MARKER:
2203 // Markers do not move the handles.
2206 case HANDLE_TYPE_COUNT:
2208 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2212 mImpl->RequestRelayout();
2216 void Controller::PasteText( const std::string& stringToPaste )
2218 InsertText( stringToPaste, Text::Controller::COMMIT );
2219 mImpl->ChangeState( EventData::EDITING );
2220 mImpl->RequestRelayout();
2222 // Do this last since it provides callbacks into application code
2223 mImpl->mControlInterface.TextChanged();
2226 void Controller::PasteClipboardItemEvent()
2228 // Retrieve the clipboard contents first
2229 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2230 std::string stringToPaste( notifier.GetContent() );
2232 // Commit the current pre-edit text; the contents of the clipboard should be appended
2233 mImpl->ResetImfManager();
2236 PasteText( stringToPaste );
2239 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2241 if( NULL == mImpl->mEventData )
2248 case Toolkit::TextSelectionPopup::CUT:
2250 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2251 mImpl->mOperationsPending = ALL_OPERATIONS;
2253 // This is to reset the virtual keyboard to Upper-case
2254 if( 0u == mImpl->mLogicalModel->mText.Count() )
2259 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2260 !mImpl->IsPlaceholderAvailable() )
2262 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2266 ShowPlaceholderText();
2267 mImpl->mEventData->mUpdateCursorPosition = true;
2269 mImpl->RequestRelayout();
2270 mImpl->mControlInterface.TextChanged();
2273 case Toolkit::TextSelectionPopup::COPY:
2275 mImpl->SendSelectionToClipboard( false ); // Text not modified
2276 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
2279 case Toolkit::TextSelectionPopup::PASTE:
2281 std::string stringToPaste("");
2282 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2283 PasteText( stringToPaste );
2286 case Toolkit::TextSelectionPopup::SELECT:
2288 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2290 if( mImpl->mEventData->mSelectionEnabled )
2292 // Creates a SELECT event.
2293 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2297 case Toolkit::TextSelectionPopup::SELECT_ALL:
2299 // Creates a SELECT_ALL event
2300 SelectEvent( 0.f, 0.f, true );
2303 case Toolkit::TextSelectionPopup::CLIPBOARD:
2305 mImpl->ShowClipboard();
2308 case Toolkit::TextSelectionPopup::NONE:
2316 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2318 bool update = false;
2319 bool requestRelayout = false;
2322 unsigned int cursorPosition = 0u;
2324 switch( imfEvent.eventName )
2326 case ImfManager::COMMIT:
2328 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2330 requestRelayout = true;
2333 case ImfManager::PREEDIT:
2335 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2337 requestRelayout = true;
2340 case ImfManager::DELETESURROUNDING:
2342 update = RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
2346 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2347 !mImpl->IsPlaceholderAvailable() )
2349 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2353 ShowPlaceholderText();
2354 mImpl->mEventData->mUpdateCursorPosition = true;
2357 requestRelayout = true;
2360 case ImfManager::GETSURROUNDING:
2363 cursorPosition = GetLogicalCursorPosition();
2365 imfManager.SetSurroundingText( text );
2366 imfManager.SetCursorPosition( cursorPosition );
2369 case ImfManager::VOID:
2376 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
2379 cursorPosition = GetLogicalCursorPosition();
2382 if( requestRelayout )
2384 mImpl->mOperationsPending = ALL_OPERATIONS;
2385 mImpl->RequestRelayout();
2387 // Do this last since it provides callbacks into application code
2388 mImpl->mControlInterface.TextChanged();
2391 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
2393 return callbackData;
2396 Controller::~Controller()
2401 bool Controller::BackspaceKeyEvent()
2403 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2405 bool removed = false;
2407 if( NULL == mImpl->mEventData )
2412 // IMF manager is no longer handling key-events
2413 mImpl->ClearPreEditFlag();
2415 if( EventData::SELECTING == mImpl->mEventData->mState )
2417 removed = RemoveSelectedText();
2419 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2421 // Remove the character before the current cursor position
2422 removed = RemoveText( -1, 1 );
2427 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE RemovedText\n", this );
2428 // Notifiy the IMF manager after text changed
2429 // Automatic Upper-case and restarting prediction on an existing word require this.
2432 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2433 !mImpl->IsPlaceholderAvailable() )
2435 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2439 ShowPlaceholderText();
2440 mImpl->mEventData->mUpdateCursorPosition = true;
2447 void Controller::NotifyImfManager()
2449 if( NULL != mImpl->mEventData )
2451 if( mImpl->mEventData->mImfManager )
2453 // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
2456 mImpl->mEventData->mImfManager.SetSurroundingText( text );
2458 mImpl->mEventData->mImfManager.SetCursorPosition( GetLogicalCursorPosition() );
2459 mImpl->mEventData->mImfManager.NotifyCursorPosition();
2464 void Controller::ShowPlaceholderText()
2466 if( mImpl->IsPlaceholderAvailable() )
2468 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2470 if( NULL == mImpl->mEventData )
2475 mImpl->mEventData->mIsShowingPlaceholderText = true;
2477 // Disable handles when showing place-holder text
2478 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2479 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2480 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2482 const char* text( NULL );
2485 // TODO - Switch placeholder text styles when changing state
2486 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2487 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2489 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2490 size = mImpl->mEventData->mPlaceholderTextActive.size();
2494 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2495 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2498 // Reset model for showing placeholder.
2499 mImpl->mLogicalModel->mText.Clear();
2501 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2503 // Convert text into UTF-32
2504 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2505 utf32Characters.Resize( size );
2507 // This is a bit horrible but std::string returns a (signed) char*
2508 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2510 // Transform a text array encoded in utf8 into an array encoded in utf32.
2511 // It returns the actual number of characters.
2512 Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2513 utf32Characters.Resize( characterCount );
2515 // Reset the cursor position
2516 mImpl->mEventData->mPrimaryCursorPosition = 0;
2518 // The natural size needs to be re-calculated.
2519 mImpl->mRecalculateNaturalSize = true;
2521 // Apply modifications to the model
2522 mImpl->mOperationsPending = ALL_OPERATIONS;
2524 // Update the rest of the model during size negotiation
2525 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2529 void Controller::ClearModelData()
2531 // n.b. This does not Clear the mText from mLogicalModel
2532 mImpl->mLogicalModel->mScriptRuns.Clear();
2533 mImpl->mLogicalModel->mFontRuns.Clear();
2534 mImpl->mLogicalModel->mLineBreakInfo.Clear();
2535 mImpl->mLogicalModel->mWordBreakInfo.Clear();
2536 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
2537 mImpl->mLogicalModel->mCharacterDirections.Clear();
2538 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
2539 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
2540 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
2541 mImpl->mVisualModel->mGlyphs.Clear();
2542 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2543 mImpl->mVisualModel->mCharactersToGlyph.Clear();
2544 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2545 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2546 mImpl->mVisualModel->mGlyphPositions.Clear();
2547 mImpl->mVisualModel->mLines.Clear();
2548 mImpl->mVisualModel->mColorRuns.Clear();
2549 mImpl->mVisualModel->ClearCaches();
2552 void Controller::ClearFontData()
2554 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2555 mImpl->mLogicalModel->mFontRuns.Clear();
2556 mImpl->mVisualModel->mGlyphs.Clear();
2557 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2558 mImpl->mVisualModel->mCharactersToGlyph.Clear();
2559 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2560 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2561 mImpl->mVisualModel->mGlyphPositions.Clear();
2562 mImpl->mVisualModel->mLines.Clear();
2563 mImpl->mVisualModel->ClearCaches();
2566 void Controller::ClearStyleData()
2568 mImpl->mLogicalModel->mColorRuns.Clear();
2569 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2572 Controller::Controller( ControlInterface& controlInterface )
2575 mImpl = new Controller::Impl( controlInterface );
2580 } // namespace Toolkit