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( const 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 numberOfCharacters )
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 numberOfCharacters %d\n",
512 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters );
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 + numberOfCharacters ) > currentText.Count() )
530 numberOfCharacters = currentText.Count() - cursorIndex;
533 if( ( cursorIndex + numberOfCharacters ) <= 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, -numberOfCharacters );
546 // Remove the characters.
547 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
548 Vector<Character>::Iterator last = first + numberOfCharacters;
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, numberOfCharacters );
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 if( UPDATE_ACTUAL_SIZE & operations )
1332 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
1335 // Nothing else to do if there is no glyphs.
1336 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1340 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1341 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1342 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1343 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1344 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1345 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1346 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1347 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1348 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1350 // Set the layout parameters.
1351 LayoutParameters layoutParameters( size,
1353 lineBreakInfo.Begin(),
1354 wordBreakInfo.Begin(),
1355 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1357 glyphsToCharactersMap.Begin(),
1358 charactersPerGlyph.Begin(),
1359 charactersToGlyph.Begin(),
1360 glyphsPerCharacter.Begin(),
1363 // The laid-out lines.
1364 // It's not possible to know in how many lines the text is going to be laid-out,
1365 // but it can be resized at least with the number of 'paragraphs' to avoid
1366 // some re-allocations.
1367 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1369 // Delete any previous laid out lines before setting the new ones.
1372 // The capacity of the bidirectional paragraph info is the number of paragraphs.
1373 lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
1375 // Resize the vector of positions to have the same size than the vector of glyphs.
1376 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1377 glyphPositions.Resize( numberOfGlyphs );
1379 // Whether the last character is a new paragraph character.
1380 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1382 // The initial glyph and the number of glyphs to layout.
1383 layoutParameters.startGlyphIndex = 0u;
1384 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1386 // Update the visual model.
1387 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1394 // Reorder the lines
1395 if( REORDER & operations )
1397 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1398 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1400 // Check first if there are paragraphs with bidirectional info.
1401 if( 0u != bidirectionalInfo.Count() )
1404 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1405 const CharacterIndex startIndex = 0u;
1406 const Length requestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1408 // Reorder the lines.
1409 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1410 ReorderLines( bidirectionalInfo,
1412 requestedNumberOfCharacters,
1414 bidirectionalLineInfo );
1416 // Set the bidirectional info per line into the layout parameters.
1417 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1418 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1420 // Set the bidirectional info into the model.
1421 mImpl->mLogicalModel->SetVisualToLogicalMap( layoutParameters.lineBidirectionalInfoRunsBuffer,
1422 layoutParameters.numberOfBidirectionalInfoRuns,
1424 requestedNumberOfCharacters );
1426 // Re-layout the text. Reorder those lines with right to left characters.
1427 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1429 requestedNumberOfCharacters,
1432 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
1433 for( Vector<BidirectionalLineInfoRun>::Iterator it = bidirectionalLineInfo.Begin(),
1434 endIt = bidirectionalLineInfo.End();
1438 BidirectionalLineInfoRun& bidiLineInfo = *it;
1440 free( bidiLineInfo.visualToLogicalMap );
1441 bidiLineInfo.visualToLogicalMap = NULL;
1444 bidirectionalLineInfo.Clear();
1448 // Sets the actual size.
1449 if( UPDATE_ACTUAL_SIZE & operations )
1451 mImpl->mVisualModel->SetLayoutSize( layoutSize );
1457 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1460 if( ALIGN & operations )
1462 // The laid-out lines.
1463 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1465 const CharacterIndex startIndex = 0u;
1466 const Length requestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1468 mImpl->mLayoutEngine.Align( size,
1470 requestedNumberOfCharacters,
1476 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1480 void Controller::SetMultiLineEnabled( bool enable )
1482 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1484 if( layout != mImpl->mLayoutEngine.GetLayout() )
1486 // Set the layout type.
1487 mImpl->mLayoutEngine.SetLayout( layout );
1489 // Set the flags to redo the layout operations
1490 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1491 UPDATE_ACTUAL_SIZE |
1495 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1497 mImpl->RequestRelayout();
1501 bool Controller::IsMultiLineEnabled() const
1503 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1506 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1508 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1510 // Set the alignment.
1511 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1513 // Set the flag to redo the alignment operation.
1514 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1516 mImpl->RequestRelayout();
1520 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1522 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1525 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1527 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1529 // Set the alignment.
1530 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1532 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1534 mImpl->RequestRelayout();
1538 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1540 return mImpl->mLayoutEngine.GetVerticalAlignment();
1543 void Controller::CalculateTextAlignment( const Size& controlSize )
1545 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1547 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1549 // Get the line height of the default font.
1550 layoutSize.height = mImpl->GetDefaultFontLineHeight();
1553 if( LayoutEngine::SINGLE_LINE_BOX == mImpl->mLayoutEngine.GetLayout() )
1555 // Get the direction of the first character.
1556 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1558 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1559 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1560 if( firstParagraphDirection )
1562 switch( horizontalAlignment )
1564 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1566 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1569 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1574 case LayoutEngine::HORIZONTAL_ALIGN_END:
1576 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1582 switch( horizontalAlignment )
1584 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1586 mImpl->mAlignmentOffset.x = 0.f;
1589 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1591 mImpl->mAlignmentOffset.x = floorf( 0.5f * ( controlSize.width - layoutSize.width ) ); // try to avoid pixel alignment.
1594 case LayoutEngine::HORIZONTAL_ALIGN_END:
1596 mImpl->mAlignmentOffset.x = controlSize.width - layoutSize.width;
1602 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1603 switch( verticalAlignment )
1605 case LayoutEngine::VERTICAL_ALIGN_TOP:
1607 mImpl->mAlignmentOffset.y = 0.f;
1610 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1612 mImpl->mAlignmentOffset.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
1615 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1617 mImpl->mAlignmentOffset.y = controlSize.height - layoutSize.height;
1623 LayoutEngine& Controller::GetLayoutEngine()
1625 return mImpl->mLayoutEngine;
1628 View& Controller::GetView()
1630 return mImpl->mView;
1633 void Controller::KeyboardFocusGainEvent()
1635 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1637 if( NULL != mImpl->mEventData )
1639 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1640 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1642 mImpl->ChangeState( EventData::EDITING );
1643 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1646 if( mImpl->IsShowingPlaceholderText() )
1648 // Show alternative placeholder-text when editing
1649 ShowPlaceholderText();
1652 mImpl->RequestRelayout();
1656 void Controller::KeyboardFocusLostEvent()
1658 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1660 if( NULL != mImpl->mEventData )
1662 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1664 mImpl->ChangeState( EventData::INACTIVE );
1666 if( !mImpl->IsShowingRealText() )
1668 // Revert to regular placeholder-text when not editing
1669 ShowPlaceholderText();
1673 mImpl->RequestRelayout();
1676 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1678 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1680 bool textChanged( false );
1682 if( ( NULL != mImpl->mEventData ) &&
1683 ( keyEvent.state == KeyEvent::Down ) )
1685 int keyCode = keyEvent.keyCode;
1686 const std::string& keyString = keyEvent.keyPressed;
1688 // Pre-process to separate modifying events from non-modifying input events.
1689 if( Dali::DALI_KEY_ESCAPE == keyCode )
1691 // Escape key is a special case which causes focus loss
1692 KeyboardFocusLostEvent();
1694 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
1695 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1696 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
1697 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
1699 Event event( Event::CURSOR_KEY_EVENT );
1700 event.p1.mInt = keyCode;
1701 mImpl->mEventData->mEventQueue.push_back( event );
1703 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1705 textChanged = BackspaceKeyEvent();
1707 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1709 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1710 // Avoids calling the InsertText() method which can delete selected text
1712 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1713 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1715 mImpl->ChangeState( EventData::INACTIVE );
1716 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1717 // Avoids calling the InsertText() method which can delete selected text
1719 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1721 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1722 // and a character is typed after the type of a upper case latin character.
1728 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1730 // IMF manager is no longer handling key-events
1731 mImpl->ClearPreEditFlag();
1733 InsertText( keyString, COMMIT );
1737 if( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1738 ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1740 mImpl->ChangeState( EventData::EDITING );
1743 mImpl->RequestRelayout();
1748 // Do this last since it provides callbacks into application code
1749 mImpl->mControlInterface.TextChanged();
1755 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1757 bool removedPrevious( false );
1758 bool maxLengthReached( false );
1760 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1762 if( NULL == mImpl->mEventData )
1767 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1768 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1769 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1771 // TODO: At the moment the underline runs are only for pre-edit.
1772 mImpl->mVisualModel->mUnderlineRuns.Clear();
1774 Vector<Character> utf32Characters;
1775 Length characterCount( 0u );
1777 // Remove the previous IMF pre-edit (predicitive text)
1778 if( mImpl->mEventData->mPreEditFlag &&
1779 ( 0u != mImpl->mEventData->mPreEditLength ) )
1781 const CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1783 removedPrevious = RemoveText( -static_cast<int>( offset ), mImpl->mEventData->mPreEditLength );
1785 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1786 mImpl->mEventData->mPreEditLength = 0u;
1790 // Remove the previous Selection
1791 removedPrevious = RemoveSelectedText();
1796 // Convert text into UTF-32
1797 utf32Characters.Resize( text.size() );
1799 // This is a bit horrible but std::string returns a (signed) char*
1800 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1802 // Transform a text array encoded in utf8 into an array encoded in utf32.
1803 // It returns the actual number of characters.
1804 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1805 utf32Characters.Resize( characterCount );
1807 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1808 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1811 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1813 // The placeholder text is no longer needed
1814 if( mImpl->IsShowingPlaceholderText() )
1819 mImpl->ChangeState( EventData::EDITING );
1821 // Handle the IMF (predicitive text) state changes
1822 if( COMMIT == type )
1824 // IMF manager is no longer handling key-events
1825 mImpl->ClearPreEditFlag();
1829 if( !mImpl->mEventData->mPreEditFlag )
1831 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1833 // Record the start of the pre-edit text
1834 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1837 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1838 mImpl->mEventData->mPreEditFlag = true;
1840 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1843 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1845 // Restrict new text to fit within Maximum characters setting.
1846 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1847 maxLengthReached = ( characterCount > maxSizeOfNewText );
1849 // The cursor position.
1850 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1852 // Update the text's style.
1854 // Updates the text style runs by adding characters.
1855 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
1857 // Get the character index from the cursor index.
1858 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
1860 // Retrieve the text's style for the given index.
1862 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
1864 // Whether to add a new text color run.
1865 const bool addColorRun = style.textColor != mImpl->mEventData->mInputStyle.textColor;
1867 // Whether to add a new font run.
1868 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
1869 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
1870 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
1871 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
1872 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
1877 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
1878 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
1880 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
1881 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
1882 colorRun.characterRun.characterIndex = cursorIndex;
1883 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1886 if( addFontNameRun ||
1892 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
1893 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
1895 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
1897 if( addFontNameRun )
1899 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
1900 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
1901 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
1902 fontDescriptionRun.familyDefined = true;
1904 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
1907 if( addFontWeightRun )
1909 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
1910 fontDescriptionRun.weightDefined = true;
1913 if( addFontWidthRun )
1915 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
1916 fontDescriptionRun.widthDefined = true;
1919 if( addFontSlantRun )
1921 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
1922 fontDescriptionRun.slantDefined = true;
1925 if( addFontSizeRun )
1927 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
1928 fontDescriptionRun.sizeDefined = true;
1931 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
1932 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1935 // Insert at current cursor position.
1936 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1938 if( cursorIndex < numberOfCharactersInModel )
1940 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1944 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1947 cursorIndex += maxSizeOfNewText;
1949 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1952 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
1953 mImpl->IsPlaceholderAvailable() )
1955 // Show place-holder if empty after removing the pre-edit text
1956 ShowPlaceholderText();
1957 mImpl->mEventData->mUpdateCursorPosition = true;
1958 mImpl->ClearPreEditFlag();
1960 else if( removedPrevious ||
1961 ( 0 != utf32Characters.Count() ) )
1963 // Queue an inserted event
1964 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1967 if( maxLengthReached )
1969 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1971 mImpl->ResetImfManager();
1973 // Do this last since it provides callbacks into application code
1974 mImpl->mControlInterface.MaxLengthReached();
1978 bool Controller::RemoveSelectedText()
1980 bool textRemoved( false );
1982 if( EventData::SELECTING == mImpl->mEventData->mState )
1984 std::string removedString;
1985 mImpl->RetrieveSelection( removedString, true );
1987 if( !removedString.empty() )
1990 mImpl->ChangeState( EventData::EDITING );
1997 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1999 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
2001 if( NULL != mImpl->mEventData )
2003 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
2005 if( 1u == tapCount )
2007 // This is to avoid unnecessary relayouts when tapping an empty text-field
2008 bool relayoutNeeded( false );
2010 if( ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) ||
2011 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
2013 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE); // If Popup shown hide it here so can be shown again if required.
2016 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
2018 // Already in an active state so show a popup
2019 if( !mImpl->IsClipboardEmpty() )
2021 // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
2022 mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
2026 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2028 relayoutNeeded = true;
2032 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2034 // Hide placeholder text
2038 if( EventData::INACTIVE == mImpl->mEventData->mState )
2040 mImpl->ChangeState( EventData::EDITING );
2042 else if( !mImpl->IsClipboardEmpty() )
2044 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2046 relayoutNeeded = true;
2049 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2050 if( relayoutNeeded )
2052 Event event( Event::TAP_EVENT );
2053 event.p1.mUint = tapCount;
2054 event.p2.mFloat = x;
2055 event.p3.mFloat = y;
2056 mImpl->mEventData->mEventQueue.push_back( event );
2058 mImpl->RequestRelayout();
2061 else if( 2u == tapCount )
2063 if( mImpl->mEventData->mSelectionEnabled &&
2064 mImpl->IsShowingRealText() )
2066 SelectEvent( x, y, false );
2071 // Reset keyboard as tap event has occurred.
2072 mImpl->ResetImfManager();
2075 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2076 // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2078 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2080 if( NULL != mImpl->mEventData )
2082 Event event( Event::PAN_EVENT );
2083 event.p1.mInt = state;
2084 event.p2.mFloat = displacement.x;
2085 event.p3.mFloat = displacement.y;
2086 mImpl->mEventData->mEventQueue.push_back( event );
2088 mImpl->RequestRelayout();
2092 void Controller::LongPressEvent( Gesture::State state, float x, float y )
2094 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2096 if( ( state == Gesture::Started ) &&
2097 ( NULL != mImpl->mEventData ) )
2099 if( !mImpl->IsShowingRealText() )
2101 Event event( Event::LONG_PRESS_EVENT );
2102 event.p1.mInt = state;
2103 mImpl->mEventData->mEventQueue.push_back( event );
2104 mImpl->RequestRelayout();
2108 // The 1st long-press on inactive text-field is treated as tap
2109 if( EventData::INACTIVE == mImpl->mEventData->mState )
2111 mImpl->ChangeState( EventData::EDITING );
2113 Event event( Event::TAP_EVENT );
2115 event.p2.mFloat = x;
2116 event.p3.mFloat = y;
2117 mImpl->mEventData->mEventQueue.push_back( event );
2119 mImpl->RequestRelayout();
2123 // Reset the imf manger to commit the pre-edit before selecting the text.
2124 mImpl->ResetImfManager();
2126 SelectEvent( x, y, false );
2132 void Controller::SelectEvent( float x, float y, bool selectAll )
2134 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2136 if( NULL != mImpl->mEventData )
2138 mImpl->ChangeState( EventData::SELECTING );
2142 Event event( Event::SELECT_ALL );
2143 mImpl->mEventData->mEventQueue.push_back( event );
2147 Event event( Event::SELECT );
2148 event.p2.mFloat = x;
2149 event.p3.mFloat = y;
2150 mImpl->mEventData->mEventQueue.push_back( event );
2153 mImpl->RequestRelayout();
2157 void Controller::GetTargetSize( Vector2& targetSize )
2159 targetSize = mImpl->mVisualModel->mControlSize;
2162 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2164 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2167 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2169 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2171 if( NULL != mImpl->mEventData )
2173 switch( handleType )
2177 Event event( Event::GRAB_HANDLE_EVENT );
2178 event.p1.mUint = state;
2179 event.p2.mFloat = x;
2180 event.p3.mFloat = y;
2182 mImpl->mEventData->mEventQueue.push_back( event );
2185 case LEFT_SELECTION_HANDLE:
2187 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2188 event.p1.mUint = state;
2189 event.p2.mFloat = x;
2190 event.p3.mFloat = y;
2192 mImpl->mEventData->mEventQueue.push_back( event );
2195 case RIGHT_SELECTION_HANDLE:
2197 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2198 event.p1.mUint = state;
2199 event.p2.mFloat = x;
2200 event.p3.mFloat = y;
2202 mImpl->mEventData->mEventQueue.push_back( event );
2205 case LEFT_SELECTION_HANDLE_MARKER:
2206 case RIGHT_SELECTION_HANDLE_MARKER:
2208 // Markers do not move the handles.
2211 case HANDLE_TYPE_COUNT:
2213 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2217 mImpl->RequestRelayout();
2221 void Controller::PasteText( const std::string& stringToPaste )
2223 InsertText( stringToPaste, Text::Controller::COMMIT );
2224 mImpl->ChangeState( EventData::EDITING );
2225 mImpl->RequestRelayout();
2227 // Do this last since it provides callbacks into application code
2228 mImpl->mControlInterface.TextChanged();
2231 void Controller::PasteClipboardItemEvent()
2233 // Retrieve the clipboard contents first
2234 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2235 std::string stringToPaste( notifier.GetContent() );
2237 // Commit the current pre-edit text; the contents of the clipboard should be appended
2238 mImpl->ResetImfManager();
2241 PasteText( stringToPaste );
2244 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2246 if( NULL == mImpl->mEventData )
2253 case Toolkit::TextSelectionPopup::CUT:
2255 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2256 mImpl->mOperationsPending = ALL_OPERATIONS;
2258 // This is to reset the virtual keyboard to Upper-case
2259 if( 0u == mImpl->mLogicalModel->mText.Count() )
2264 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2265 !mImpl->IsPlaceholderAvailable() )
2267 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2271 ShowPlaceholderText();
2272 mImpl->mEventData->mUpdateCursorPosition = true;
2274 mImpl->RequestRelayout();
2275 mImpl->mControlInterface.TextChanged();
2278 case Toolkit::TextSelectionPopup::COPY:
2280 mImpl->SendSelectionToClipboard( false ); // Text not modified
2281 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
2284 case Toolkit::TextSelectionPopup::PASTE:
2286 std::string stringToPaste("");
2287 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2288 PasteText( stringToPaste );
2291 case Toolkit::TextSelectionPopup::SELECT:
2293 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2295 if( mImpl->mEventData->mSelectionEnabled )
2297 // Creates a SELECT event.
2298 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2302 case Toolkit::TextSelectionPopup::SELECT_ALL:
2304 // Creates a SELECT_ALL event
2305 SelectEvent( 0.f, 0.f, true );
2308 case Toolkit::TextSelectionPopup::CLIPBOARD:
2310 mImpl->ShowClipboard();
2313 case Toolkit::TextSelectionPopup::NONE:
2321 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2323 bool update = false;
2324 bool requestRelayout = false;
2327 unsigned int cursorPosition = 0u;
2329 switch( imfEvent.eventName )
2331 case ImfManager::COMMIT:
2333 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2335 requestRelayout = true;
2338 case ImfManager::PREEDIT:
2340 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2342 requestRelayout = true;
2345 case ImfManager::DELETESURROUNDING:
2347 update = RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
2351 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2352 !mImpl->IsPlaceholderAvailable() )
2354 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2358 ShowPlaceholderText();
2359 mImpl->mEventData->mUpdateCursorPosition = true;
2362 requestRelayout = true;
2365 case ImfManager::GETSURROUNDING:
2368 cursorPosition = GetLogicalCursorPosition();
2370 imfManager.SetSurroundingText( text );
2371 imfManager.SetCursorPosition( cursorPosition );
2374 case ImfManager::VOID:
2381 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
2384 cursorPosition = GetLogicalCursorPosition();
2387 if( requestRelayout )
2389 mImpl->mOperationsPending = ALL_OPERATIONS;
2390 mImpl->RequestRelayout();
2392 // Do this last since it provides callbacks into application code
2393 mImpl->mControlInterface.TextChanged();
2396 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
2398 return callbackData;
2401 Controller::~Controller()
2406 bool Controller::BackspaceKeyEvent()
2408 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2410 bool removed = false;
2412 if( NULL == mImpl->mEventData )
2417 // IMF manager is no longer handling key-events
2418 mImpl->ClearPreEditFlag();
2420 if( EventData::SELECTING == mImpl->mEventData->mState )
2422 removed = RemoveSelectedText();
2424 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2426 // Remove the character before the current cursor position
2427 removed = RemoveText( -1, 1 );
2432 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE RemovedText\n", this );
2433 // Notifiy the IMF manager after text changed
2434 // Automatic Upper-case and restarting prediction on an existing word require this.
2437 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2438 !mImpl->IsPlaceholderAvailable() )
2440 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2444 ShowPlaceholderText();
2445 mImpl->mEventData->mUpdateCursorPosition = true;
2452 void Controller::NotifyImfManager()
2454 if( NULL != mImpl->mEventData )
2456 if( mImpl->mEventData->mImfManager )
2458 // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
2461 mImpl->mEventData->mImfManager.SetSurroundingText( text );
2463 mImpl->mEventData->mImfManager.SetCursorPosition( GetLogicalCursorPosition() );
2464 mImpl->mEventData->mImfManager.NotifyCursorPosition();
2469 void Controller::ShowPlaceholderText()
2471 if( mImpl->IsPlaceholderAvailable() )
2473 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2475 if( NULL == mImpl->mEventData )
2480 mImpl->mEventData->mIsShowingPlaceholderText = true;
2482 // Disable handles when showing place-holder text
2483 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2484 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2485 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2487 const char* text( NULL );
2490 // TODO - Switch placeholder text styles when changing state
2491 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2492 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2494 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2495 size = mImpl->mEventData->mPlaceholderTextActive.size();
2499 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2500 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2503 // Reset model for showing placeholder.
2504 mImpl->mLogicalModel->mText.Clear();
2506 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2508 // Convert text into UTF-32
2509 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2510 utf32Characters.Resize( size );
2512 // This is a bit horrible but std::string returns a (signed) char*
2513 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2515 // Transform a text array encoded in utf8 into an array encoded in utf32.
2516 // It returns the actual number of characters.
2517 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2518 utf32Characters.Resize( characterCount );
2520 // Reset the cursor position
2521 mImpl->mEventData->mPrimaryCursorPosition = 0;
2523 // The natural size needs to be re-calculated.
2524 mImpl->mRecalculateNaturalSize = true;
2526 // Apply modifications to the model
2527 mImpl->mOperationsPending = ALL_OPERATIONS;
2529 // Update the rest of the model during size negotiation
2530 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2534 void Controller::ClearModelData()
2536 // n.b. This does not Clear the mText from mLogicalModel
2537 mImpl->mLogicalModel->mScriptRuns.Clear();
2538 mImpl->mLogicalModel->mFontRuns.Clear();
2539 mImpl->mLogicalModel->mLineBreakInfo.Clear();
2540 mImpl->mLogicalModel->mWordBreakInfo.Clear();
2541 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
2542 mImpl->mLogicalModel->mCharacterDirections.Clear();
2543 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
2544 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
2545 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
2546 mImpl->mVisualModel->mGlyphs.Clear();
2547 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2548 mImpl->mVisualModel->mCharactersToGlyph.Clear();
2549 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2550 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2551 mImpl->mVisualModel->mGlyphPositions.Clear();
2552 mImpl->mVisualModel->mLines.Clear();
2553 mImpl->mVisualModel->mColorRuns.Clear();
2554 mImpl->mVisualModel->ClearCaches();
2557 void Controller::ClearFontData()
2559 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2560 mImpl->mLogicalModel->mFontRuns.Clear();
2561 mImpl->mVisualModel->mGlyphs.Clear();
2562 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2563 mImpl->mVisualModel->mCharactersToGlyph.Clear();
2564 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2565 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2566 mImpl->mVisualModel->mGlyphPositions.Clear();
2567 mImpl->mVisualModel->mLines.Clear();
2568 mImpl->mVisualModel->ClearCaches();
2571 void Controller::ClearStyleData()
2573 mImpl->mLogicalModel->mColorRuns.Clear();
2574 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2577 Controller::Controller( ControlInterface& controlInterface )
2580 mImpl = new Controller::Impl( controlInterface );
2585 } // namespace Toolkit