2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/text/text-controller.h>
24 #include <dali/public-api/adaptor-framework/key.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/devel-api/adaptor-framework/clipboard-event-notifier.h>
29 #include <dali-toolkit/internal/text/bidirectional-support.h>
30 #include <dali-toolkit/internal/text/character-set-conversion.h>
31 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
32 #include <dali-toolkit/internal/text/markup-processor.h>
33 #include <dali-toolkit/internal/text/text-controller-impl.h>
38 #if defined(DEBUG_ENABLED)
39 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
42 const float MAX_FLOAT = std::numeric_limits<float>::max();
43 const unsigned int POINTS_PER_INCH = 72;
45 const std::string EMPTY_STRING("");
47 float ConvertToEven( float value )
49 int intValue(static_cast<int>( value ));
50 return static_cast<float>(intValue % 2 == 0) ? intValue : (intValue + 1);
65 * @brief Adds a new font description run for the selected text.
67 * The new font parameters are added after the call to this method.
69 * @param[in] eventData The event data pointer.
70 * @param[in] logicalModel The logical model where to add the new font description run.
72 FontDescriptionRun& UpdateSelectionFontStyleRun( EventData* eventData,
73 LogicalModelPtr logicalModel )
75 const bool handlesCrossed = eventData->mLeftSelectionPosition > eventData->mRightSelectionPosition;
77 // Get start and end position of selection
78 const CharacterIndex startOfSelectedText = handlesCrossed ? eventData->mRightSelectionPosition : eventData->mLeftSelectionPosition;
79 const Length lengthOfSelectedText = ( handlesCrossed ? eventData->mLeftSelectionPosition : eventData->mRightSelectionPosition ) - startOfSelectedText;
82 const VectorBase::SizeType numberOfRuns = logicalModel->mFontDescriptionRuns.Count();
83 logicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
85 FontDescriptionRun& fontDescriptionRun = *( logicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
87 fontDescriptionRun.characterRun.characterIndex = startOfSelectedText;
88 fontDescriptionRun.characterRun.numberOfCharacters = lengthOfSelectedText;
90 // Recalculate the selection highlight as the metrics may have changed.
91 eventData->mUpdateLeftSelectionPosition = true;
92 eventData->mUpdateRightSelectionPosition = true;
94 return fontDescriptionRun;
97 ControllerPtr Controller::New( ControlInterface& controlInterface )
99 return ControllerPtr( new Controller( controlInterface ) );
102 void Controller::EnableTextInput( DecoratorPtr decorator )
104 if( NULL == mImpl->mEventData )
106 mImpl->mEventData = new EventData( decorator );
110 void Controller::SetGlyphType( TextAbstraction::GlyphType glyphType )
112 // Metrics for bitmap & vector based glyphs are different
113 mImpl->mMetrics->SetGlyphType( glyphType );
115 // Clear the font-specific data
118 mImpl->mRecalculateNaturalSize = true;
120 mImpl->RequestRelayout();
123 void Controller::SetMarkupProcessorEnabled( bool enable )
125 mImpl->mMarkupProcessorEnabled = enable;
128 bool Controller::IsMarkupProcessorEnabled() const
130 return mImpl->mMarkupProcessorEnabled;
133 void Controller::SetText( const std::string& text )
135 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
137 // Reset keyboard as text changed
138 mImpl->ResetImfManager();
140 // Remove the previously set text and style.
146 CharacterIndex lastCursorIndex = 0u;
148 if( NULL != mImpl->mEventData )
150 // If popup shown then hide it by switching to Editing state
151 if( ( EventData::SELECTING == mImpl->mEventData->mState ) ||
152 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
153 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
154 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
156 mImpl->ChangeState( EventData::EDITING );
162 MarkupProcessData markupProcessData( mImpl->mLogicalModel->mColorRuns,
163 mImpl->mLogicalModel->mFontDescriptionRuns );
165 Length textSize = 0u;
166 const uint8_t* utf8 = NULL;
167 if( mImpl->mMarkupProcessorEnabled )
169 ProcessMarkupString( text, markupProcessData );
170 textSize = markupProcessData.markupProcessedText.size();
172 // This is a bit horrible but std::string returns a (signed) char*
173 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
177 textSize = text.size();
179 // This is a bit horrible but std::string returns a (signed) char*
180 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
183 // Convert text into UTF-32
184 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
185 utf32Characters.Resize( textSize );
187 // Transform a text array encoded in utf8 into an array encoded in utf32.
188 // It returns the actual number of characters.
189 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
190 utf32Characters.Resize( characterCount );
192 DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
193 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mLogicalModel->mText.Count() );
195 // The characters to be added.
196 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
198 // To reset the cursor position
199 lastCursorIndex = characterCount;
201 // Update the rest of the model during size negotiation
202 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
204 // The natural size needs to be re-calculated.
205 mImpl->mRecalculateNaturalSize = true;
207 // Apply modifications to the model
208 mImpl->mOperationsPending = ALL_OPERATIONS;
212 ShowPlaceholderText();
215 // Resets the cursor position.
216 ResetCursorPosition( lastCursorIndex );
218 // Scrolls the text to make the cursor visible.
219 ResetScrollPosition();
221 mImpl->RequestRelayout();
223 if( NULL != mImpl->mEventData )
225 // Cancel previously queued events
226 mImpl->mEventData->mEventQueue.clear();
229 // Notify IMF as text changed
232 // Do this last since it provides callbacks into application code
233 mImpl->mControlInterface.TextChanged();
236 void Controller::GetText( std::string& text ) const
238 if( !mImpl->IsShowingPlaceholderText() )
240 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
242 if( 0u != utf32Characters.Count() )
244 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
249 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
253 unsigned int Controller::GetLogicalCursorPosition() const
255 if( NULL != mImpl->mEventData )
257 return mImpl->mEventData->mPrimaryCursorPosition;
263 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
265 if( NULL != mImpl->mEventData )
267 if( PLACEHOLDER_TYPE_INACTIVE == type )
269 mImpl->mEventData->mPlaceholderTextInactive = text;
273 mImpl->mEventData->mPlaceholderTextActive = text;
276 // Update placeholder if there is no text
277 if( mImpl->IsShowingPlaceholderText() ||
278 ( 0u == mImpl->mLogicalModel->mText.Count() ) )
280 ShowPlaceholderText();
285 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
287 if( NULL != mImpl->mEventData )
289 if( PLACEHOLDER_TYPE_INACTIVE == type )
291 text = mImpl->mEventData->mPlaceholderTextInactive;
295 text = mImpl->mEventData->mPlaceholderTextActive;
300 void Controller::SetMaximumNumberOfCharacters( Length maxCharacters )
302 mImpl->mMaximumNumberOfCharacters = maxCharacters;
305 int Controller::GetMaximumNumberOfCharacters()
307 return mImpl->mMaximumNumberOfCharacters;
310 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
312 if( NULL == mImpl->mFontDefaults )
314 mImpl->mFontDefaults = new FontDefaults();
317 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
318 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
319 mImpl->mFontDefaults->familyDefined = true;
321 // Clear the font-specific data
324 mImpl->mRecalculateNaturalSize = true;
326 mImpl->RequestRelayout();
329 const std::string& Controller::GetDefaultFontFamily() const
331 if( NULL != mImpl->mFontDefaults )
333 return mImpl->mFontDefaults->mFontDescription.family;
339 void Controller::SetDefaultFontStyle( const std::string& style )
341 if( NULL == mImpl->mFontDefaults )
343 mImpl->mFontDefaults = new FontDefaults();
346 mImpl->mFontDefaults->mFontStyle = style;
349 const std::string& Controller::GetDefaultFontStyle() const
351 if( NULL != mImpl->mFontDefaults )
353 return mImpl->mFontDefaults->mFontStyle;
359 void Controller::SetDefaultFontWeight( FontWeight weight )
361 if( NULL == mImpl->mFontDefaults )
363 mImpl->mFontDefaults = new FontDefaults();
366 mImpl->mFontDefaults->mFontDescription.weight = weight;
367 mImpl->mFontDefaults->weightDefined = true;
369 // Clear the font-specific data
372 mImpl->mRecalculateNaturalSize = true;
374 mImpl->RequestRelayout();
377 FontWeight Controller::GetDefaultFontWeight() const
379 if( NULL != mImpl->mFontDefaults )
381 return mImpl->mFontDefaults->mFontDescription.weight;
384 return TextAbstraction::FontWeight::NORMAL;
387 void Controller::SetDefaultFontWidth( FontWidth width )
389 if( NULL == mImpl->mFontDefaults )
391 mImpl->mFontDefaults = new FontDefaults();
394 mImpl->mFontDefaults->mFontDescription.width = width;
395 mImpl->mFontDefaults->widthDefined = true;
397 // Clear the font-specific data
400 mImpl->mRecalculateNaturalSize = true;
402 mImpl->RequestRelayout();
405 FontWidth Controller::GetDefaultFontWidth() const
407 if( NULL != mImpl->mFontDefaults )
409 return mImpl->mFontDefaults->mFontDescription.width;
412 return TextAbstraction::FontWidth::NORMAL;
415 void Controller::SetDefaultFontSlant( FontSlant slant )
417 if( NULL == mImpl->mFontDefaults )
419 mImpl->mFontDefaults = new FontDefaults();
422 mImpl->mFontDefaults->mFontDescription.slant = slant;
423 mImpl->mFontDefaults->slantDefined = true;
425 // Clear the font-specific data
428 mImpl->mRecalculateNaturalSize = true;
430 mImpl->RequestRelayout();
433 FontSlant Controller::GetDefaultFontSlant() const
435 if( NULL != mImpl->mFontDefaults )
437 return mImpl->mFontDefaults->mFontDescription.slant;
440 return TextAbstraction::FontSlant::NORMAL;
443 void Controller::SetDefaultPointSize( float pointSize )
445 if( NULL == mImpl->mFontDefaults )
447 mImpl->mFontDefaults = new FontDefaults();
450 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
451 mImpl->mFontDefaults->sizeDefined = true;
453 unsigned int horizontalDpi( 0u );
454 unsigned int verticalDpi( 0u );
455 mImpl->mFontClient.GetDpi( horizontalDpi, verticalDpi );
457 // Adjust the metrics if the fixed-size font should be down-scaled
458 int maxEmojiSize( pointSize/POINTS_PER_INCH * verticalDpi );
459 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultPointSize %p setting MaxEmojiSize %d\n", this, maxEmojiSize );
460 mImpl->mMetrics->SetMaxEmojiSize( maxEmojiSize );
462 // Clear the font-specific data
465 mImpl->mRecalculateNaturalSize = true;
467 mImpl->RequestRelayout();
470 float Controller::GetDefaultPointSize() const
472 if( NULL != mImpl->mFontDefaults )
474 return mImpl->mFontDefaults->mDefaultPointSize;
480 void Controller::UpdateAfterFontChange( const std::string& newDefaultFont )
482 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange");
484 if( !mImpl->mFontDefaults->familyDefined ) // If user defined font then should not update when system font changes
486 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
488 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
489 mImpl->mRecalculateNaturalSize = true;
490 mImpl->RequestRelayout();
494 void Controller::SetTextColor( const Vector4& textColor )
496 mImpl->mTextColor = textColor;
498 if( !mImpl->IsShowingPlaceholderText() )
500 mImpl->mVisualModel->SetTextColor( textColor );
502 mImpl->RequestRelayout();
506 const Vector4& Controller::GetTextColor() const
508 return mImpl->mTextColor;
511 bool Controller::RemoveText( int cursorOffset,
512 int numberOfCharacters,
513 UpdateInputStyleType type )
515 bool removed = false;
517 if( NULL == mImpl->mEventData )
522 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfCharacters %d\n",
523 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters );
525 if( !mImpl->IsShowingPlaceholderText() )
527 // Delete at current cursor position
528 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
529 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
531 CharacterIndex cursorIndex = oldCursorIndex;
533 // Validate the cursor position & number of characters
534 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
536 cursorIndex = oldCursorIndex + cursorOffset;
539 if( ( cursorIndex + numberOfCharacters ) > currentText.Count() )
541 numberOfCharacters = currentText.Count() - cursorIndex;
544 if( ( cursorIndex + numberOfCharacters ) <= mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters )
546 // Mark the paragraphs to be updated.
547 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
548 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove += numberOfCharacters;
550 // Update the input style and remove the text's style before removing the text.
552 if( UPDATE_INPUT_STYLE == type )
554 // Set first the default input style.
555 mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
557 // Update the input style.
558 mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
561 // Updates the text style runs by removing characters. Runs with no characters are removed.
562 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfCharacters );
564 // Remove the characters.
565 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
566 Vector<Character>::Iterator last = first + numberOfCharacters;
568 currentText.Erase( first, last );
570 // Cursor position retreat
571 oldCursorIndex = cursorIndex;
573 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfCharacters );
581 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
583 if( NULL != mImpl->mEventData )
585 mImpl->mEventData->mPlaceholderTextColor = textColor;
588 if( mImpl->IsShowingPlaceholderText() )
590 mImpl->mVisualModel->SetTextColor( textColor );
591 mImpl->RequestRelayout();
595 const Vector4& Controller::GetPlaceholderTextColor() const
597 if( NULL != mImpl->mEventData )
599 return mImpl->mEventData->mPlaceholderTextColor;
605 void Controller::SetShadowOffset( const Vector2& shadowOffset )
607 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
609 mImpl->RequestRelayout();
612 const Vector2& Controller::GetShadowOffset() const
614 return mImpl->mVisualModel->GetShadowOffset();
617 void Controller::SetShadowColor( const Vector4& shadowColor )
619 mImpl->mVisualModel->SetShadowColor( shadowColor );
621 mImpl->RequestRelayout();
624 const Vector4& Controller::GetShadowColor() const
626 return mImpl->mVisualModel->GetShadowColor();
629 void Controller::SetUnderlineColor( const Vector4& color )
631 mImpl->mVisualModel->SetUnderlineColor( color );
633 mImpl->RequestRelayout();
636 const Vector4& Controller::GetUnderlineColor() const
638 return mImpl->mVisualModel->GetUnderlineColor();
641 void Controller::SetUnderlineEnabled( bool enabled )
643 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
645 mImpl->RequestRelayout();
648 bool Controller::IsUnderlineEnabled() const
650 return mImpl->mVisualModel->IsUnderlineEnabled();
653 void Controller::SetUnderlineHeight( float height )
655 mImpl->mVisualModel->SetUnderlineHeight( height );
657 mImpl->RequestRelayout();
660 float Controller::GetUnderlineHeight() const
662 return mImpl->mVisualModel->GetUnderlineHeight();
665 void Controller::SetInputColor( const Vector4& color )
667 if( NULL != mImpl->mEventData )
669 mImpl->mEventData->mInputStyle.textColor = color;
671 if( EventData::SELECTING == mImpl->mEventData->mState )
673 const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
675 // Get start and end position of selection
676 const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
677 const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
679 // Add the color run.
680 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
681 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
683 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
684 colorRun.color = color;
685 colorRun.characterRun.characterIndex = startOfSelectedText;
686 colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
688 // Request to relayout.
689 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
690 mImpl->RequestRelayout();
695 const Vector4& Controller::GetInputColor() const
697 if( NULL != mImpl->mEventData )
699 return mImpl->mEventData->mInputStyle.textColor;
702 // Return the default text's color if there is no EventData.
703 return mImpl->mTextColor;
707 void Controller::SetInputFontFamily( const std::string& fontFamily )
709 if( NULL != mImpl->mEventData )
711 mImpl->mEventData->mInputStyle.familyName = fontFamily;
712 mImpl->mEventData->mInputStyle.familyDefined = true;
714 if( EventData::SELECTING == mImpl->mEventData->mState )
716 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
717 mImpl->mLogicalModel );
719 fontDescriptionRun.familyLength = fontFamily.size();
720 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
721 memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
722 fontDescriptionRun.familyDefined = true;
724 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
726 // Request to relayout.
727 mImpl->mOperationsPending = ALL_OPERATIONS;
728 mImpl->mRecalculateNaturalSize = true;
729 mImpl->RequestRelayout();
731 // As the font changes, recalculate the handle positions is needed.
732 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
733 mImpl->mEventData->mUpdateRightSelectionPosition = true;
734 mImpl->mEventData->mScrollAfterUpdatePosition = true;
739 const std::string& Controller::GetInputFontFamily() const
741 if( NULL != mImpl->mEventData )
743 return mImpl->mEventData->mInputStyle.familyName;
746 // Return the default font's family if there is no EventData.
747 return GetDefaultFontFamily();
750 void Controller::SetInputFontStyle( const std::string& fontStyle )
752 if( NULL != mImpl->mEventData )
754 mImpl->mEventData->mInputStyle.fontStyle = fontStyle;
758 const std::string& Controller::GetInputFontStyle() const
760 if( NULL != mImpl->mEventData )
762 return mImpl->mEventData->mInputStyle.fontStyle;
765 // Return the default font's style if there is no EventData.
766 return GetDefaultFontStyle();
769 void Controller::SetInputFontWeight( FontWeight weight )
771 if( NULL != mImpl->mEventData )
773 mImpl->mEventData->mInputStyle.weight = weight;
774 mImpl->mEventData->mInputStyle.weightDefined = true;
776 if( EventData::SELECTING == mImpl->mEventData->mState )
778 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
779 mImpl->mLogicalModel );
781 fontDescriptionRun.weight = weight;
782 fontDescriptionRun.weightDefined = true;
784 // Request to relayout.
785 mImpl->mOperationsPending = ALL_OPERATIONS;
786 mImpl->mRecalculateNaturalSize = true;
787 mImpl->RequestRelayout();
789 // As the font might change, recalculate the handle positions is needed.
790 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
791 mImpl->mEventData->mUpdateRightSelectionPosition = true;
792 mImpl->mEventData->mScrollAfterUpdatePosition = true;
797 FontWeight Controller::GetInputFontWeight() const
799 if( NULL != mImpl->mEventData )
801 return mImpl->mEventData->mInputStyle.weight;
804 return GetDefaultFontWeight();
807 void Controller::SetInputFontWidth( FontWidth width )
809 if( NULL != mImpl->mEventData )
811 mImpl->mEventData->mInputStyle.width = width;
812 mImpl->mEventData->mInputStyle.widthDefined = true;
814 if( EventData::SELECTING == mImpl->mEventData->mState )
816 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
817 mImpl->mLogicalModel );
819 fontDescriptionRun.width = width;
820 fontDescriptionRun.widthDefined = true;
822 // Request to relayout.
823 mImpl->mOperationsPending = ALL_OPERATIONS;
824 mImpl->mRecalculateNaturalSize = true;
825 mImpl->RequestRelayout();
827 // As the font might change, recalculate the handle positions is needed.
828 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
829 mImpl->mEventData->mUpdateRightSelectionPosition = true;
830 mImpl->mEventData->mScrollAfterUpdatePosition = true;
835 FontWidth Controller::GetInputFontWidth() const
837 if( NULL != mImpl->mEventData )
839 return mImpl->mEventData->mInputStyle.width;
842 return GetDefaultFontWidth();
845 void Controller::SetInputFontSlant( FontSlant slant )
847 if( NULL != mImpl->mEventData )
849 mImpl->mEventData->mInputStyle.slant = slant;
850 mImpl->mEventData->mInputStyle.slantDefined = true;
852 if( EventData::SELECTING == mImpl->mEventData->mState )
854 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
855 mImpl->mLogicalModel );
857 fontDescriptionRun.slant = slant;
858 fontDescriptionRun.slantDefined = true;
860 // Request to relayout.
861 mImpl->mOperationsPending = ALL_OPERATIONS;
862 mImpl->mRecalculateNaturalSize = true;
863 mImpl->RequestRelayout();
865 // As the font might change, recalculate the handle positions is needed.
866 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
867 mImpl->mEventData->mUpdateRightSelectionPosition = true;
868 mImpl->mEventData->mScrollAfterUpdatePosition = true;
873 FontSlant Controller::GetInputFontSlant() const
875 if( NULL != mImpl->mEventData )
877 return mImpl->mEventData->mInputStyle.slant;
880 return GetDefaultFontSlant();
883 void Controller::SetInputFontPointSize( float size )
885 if( NULL != mImpl->mEventData )
887 mImpl->mEventData->mInputStyle.size = size;
889 if( EventData::SELECTING == mImpl->mEventData->mState )
891 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
892 mImpl->mLogicalModel );
894 fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
895 fontDescriptionRun.sizeDefined = true;
897 // Request to relayout.
898 mImpl->mOperationsPending = ALL_OPERATIONS;
899 mImpl->mRecalculateNaturalSize = true;
900 mImpl->RequestRelayout();
902 // As the font might change, recalculate the handle positions is needed.
903 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
904 mImpl->mEventData->mUpdateRightSelectionPosition = true;
905 mImpl->mEventData->mScrollAfterUpdatePosition = true;
910 float Controller::GetInputFontPointSize() const
912 if( NULL != mImpl->mEventData )
914 return mImpl->mEventData->mInputStyle.size;
917 // Return the default font's point size if there is no EventData.
918 return GetDefaultPointSize();
921 void Controller::SetEnableCursorBlink( bool enable )
923 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
925 if( NULL != mImpl->mEventData )
927 mImpl->mEventData->mCursorBlinkEnabled = enable;
930 mImpl->mEventData->mDecorator )
932 mImpl->mEventData->mDecorator->StopCursorBlink();
937 bool Controller::GetEnableCursorBlink() const
939 if( NULL != mImpl->mEventData )
941 return mImpl->mEventData->mCursorBlinkEnabled;
947 const Vector2& Controller::GetScrollPosition() const
949 if( NULL != mImpl->mEventData )
951 return mImpl->mEventData->mScrollPosition;
954 return Vector2::ZERO;
957 const Vector2& Controller::GetAlignmentOffset() const
959 return mImpl->mAlignmentOffset;
962 Vector3 Controller::GetNaturalSize()
964 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
967 // Make sure the model is up-to-date before layouting
968 ProcessModifyEvents();
970 if( mImpl->mRecalculateNaturalSize )
972 // Operations that can be done only once until the text changes.
973 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
981 // Make sure the model is up-to-date before layouting
982 mImpl->UpdateModel( onlyOnceOperations );
984 // Layout the text for the new width.
985 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
987 // Set the update info to relayout the whole text.
988 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
989 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
991 // Store the actual control's width.
992 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
994 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
995 static_cast<OperationsMask>( onlyOnceOperations |
997 naturalSize.GetVectorXY() );
999 // Do not do again the only once operations.
1000 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1002 // Do the size related operations again.
1003 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1006 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1008 // Stores the natural size to avoid recalculate it again
1009 // unless the text/style changes.
1010 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
1012 mImpl->mRecalculateNaturalSize = false;
1014 // Clear the update info. This info will be set the next time the text is updated.
1015 mImpl->mTextUpdateInfo.Clear();
1017 // Restore the actual control's width.
1018 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1020 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1024 naturalSize = mImpl->mVisualModel->GetNaturalSize();
1026 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1029 naturalSize.x = ConvertToEven( naturalSize.x );
1030 naturalSize.y = ConvertToEven( naturalSize.y );
1035 float Controller::GetHeightForWidth( float width )
1037 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1038 // Make sure the model is up-to-date before layouting
1039 ProcessModifyEvents();
1042 if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
1044 // Operations that can be done only once until the text changes.
1045 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1052 GET_GLYPH_METRICS );
1053 // Make sure the model is up-to-date before layouting
1054 mImpl->UpdateModel( onlyOnceOperations );
1057 // Layout the text for the new width.
1058 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1060 // Set the update info to relayout the whole text.
1061 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1062 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1064 // Store the actual control's width.
1065 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
1067 DoRelayout( Size( width, MAX_FLOAT ),
1068 static_cast<OperationsMask>( onlyOnceOperations |
1072 // Do not do again the only once operations.
1073 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1075 // Do the size related operations again.
1076 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1080 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1082 // Clear the update info. This info will be set the next time the text is updated.
1083 mImpl->mTextUpdateInfo.Clear();
1085 // Restore the actual control's width.
1086 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1088 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1092 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1093 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1096 return layoutSize.height;
1099 bool Controller::Relayout( const Size& size )
1101 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
1103 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1105 bool glyphsRemoved( false );
1106 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1108 mImpl->mVisualModel->mGlyphPositions.Clear();
1109 glyphsRemoved = true;
1112 // Clear the update info. This info will be set the next time the text is updated.
1113 mImpl->mTextUpdateInfo.Clear();
1115 // Not worth to relayout if width or height is equal to zero.
1116 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1117 return glyphsRemoved;
1120 // Whether a new size has been set.
1121 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1125 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1127 // Layout operations that need to be done if the size changes.
1128 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1131 UPDATE_ACTUAL_SIZE |
1133 // Set the update info to relayout the whole text.
1134 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
1135 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1138 // Whether there are modify events.
1139 const bool isModifyEventsEmpty = 0u == mImpl->mModifyEvents.Count();
1141 // Make sure the model is up-to-date before layouting.
1142 ProcessModifyEvents();
1143 mImpl->UpdateModel( mImpl->mOperationsPending );
1145 // Style operations that need to be done if the text is modified.
1146 if( !isModifyEventsEmpty )
1148 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1152 // Apply the style runs if text is modified.
1153 bool updated = mImpl->UpdateModelStyle( mImpl->mOperationsPending );
1157 updated = DoRelayout( size,
1158 mImpl->mOperationsPending,
1159 layoutSize ) || updated;
1161 // Do not re-do any operation until something changes.
1162 mImpl->mOperationsPending = NO_OPERATION;
1164 // Whether the text control is editable
1165 const bool isEditable = NULL != mImpl->mEventData;
1167 // Keep the current offset and alignment as it will be used to update the decorator's positions (if the size changes).
1169 if( newSize && isEditable )
1171 offset = mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition;
1174 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
1175 CalculateTextAlignment( size );
1181 // If there is a new size, the scroll position needs to be clamped.
1182 mImpl->ClampHorizontalScroll( layoutSize );
1184 // Update the decorator's positions is needed if there is a new size.
1185 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
1188 // Move the cursor, grab handle etc.
1189 updated = mImpl->ProcessInputEvents() || updated;
1192 // Clear the update info. This info will be set the next time the text is updated.
1193 mImpl->mTextUpdateInfo.Clear();
1195 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1199 void Controller::ProcessModifyEvents()
1201 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
1203 if( 0u == events.Count() )
1209 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
1210 endIt = events.End();
1214 const ModifyEvent& event = *it;
1216 if( ModifyEvent::TEXT_REPLACED == event.type )
1218 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
1219 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
1221 TextReplacedEvent();
1223 else if( ModifyEvent::TEXT_INSERTED == event.type )
1225 TextInsertedEvent();
1227 else if( ModifyEvent::TEXT_DELETED == event.type )
1229 // Placeholder-text cannot be deleted
1230 if( !mImpl->IsShowingPlaceholderText() )
1237 if( NULL != mImpl->mEventData )
1239 // When the text is being modified, delay cursor blinking
1240 mImpl->mEventData->mDecorator->DelayCursorBlink();
1243 // Discard temporary text
1247 void Controller::ResetText()
1250 mImpl->mLogicalModel->mText.Clear();
1252 // We have cleared everything including the placeholder-text
1253 mImpl->PlaceholderCleared();
1255 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1256 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1257 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
1259 // Clear any previous text.
1260 mImpl->mTextUpdateInfo.mClearAll = true;
1262 // The natural size needs to be re-calculated.
1263 mImpl->mRecalculateNaturalSize = true;
1265 // Apply modifications to the model
1266 mImpl->mOperationsPending = ALL_OPERATIONS;
1269 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
1271 // Reset the cursor position
1272 if( NULL != mImpl->mEventData )
1274 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
1276 // Update the cursor if it's in editing mode.
1277 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1279 mImpl->mEventData->mUpdateCursorPosition = true;
1284 void Controller::ResetScrollPosition()
1286 if( NULL != mImpl->mEventData )
1288 // Reset the scroll position.
1289 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
1290 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1294 void Controller::TextReplacedEvent()
1296 // The natural size needs to be re-calculated.
1297 mImpl->mRecalculateNaturalSize = true;
1299 // Apply modifications to the model
1300 mImpl->mOperationsPending = ALL_OPERATIONS;
1303 void Controller::TextInsertedEvent()
1305 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
1307 if( NULL == mImpl->mEventData )
1312 // The natural size needs to be re-calculated.
1313 mImpl->mRecalculateNaturalSize = true;
1315 // Apply modifications to the model; TODO - Optimize this
1316 mImpl->mOperationsPending = ALL_OPERATIONS;
1318 // Queue a cursor reposition event; this must wait until after DoRelayout()
1319 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1321 mImpl->mEventData->mUpdateCursorPosition = true;
1322 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1326 void Controller::TextDeletedEvent()
1328 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
1330 if( NULL == mImpl->mEventData )
1335 // The natural size needs to be re-calculated.
1336 mImpl->mRecalculateNaturalSize = true;
1338 // Apply modifications to the model; TODO - Optimize this
1339 mImpl->mOperationsPending = ALL_OPERATIONS;
1341 // Queue a cursor reposition event; this must wait until after DoRelayout()
1342 mImpl->mEventData->mUpdateCursorPosition = true;
1343 if( 0u != mImpl->mLogicalModel->mText.Count() )
1345 mImpl->mEventData->mScrollAfterDelete = true;
1349 bool Controller::DoRelayout( const Size& size,
1350 OperationsMask operationsRequired,
1353 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
1354 bool viewUpdated( false );
1356 // Calculate the operations to be done.
1357 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
1359 const CharacterIndex startIndex = mImpl->mTextUpdateInfo.mParagraphCharacterIndex;
1360 const Length requestedNumberOfCharacters = mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters;
1362 if( LAYOUT & operations )
1364 // Some vectors with data needed to layout and reorder may be void
1365 // after the first time the text has been laid out.
1366 // Fill the vectors again.
1368 // Calculate the number of glyphs to layout.
1369 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1370 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1371 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
1372 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
1374 const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
1375 const GlyphIndex startGlyphIndex = mImpl->mTextUpdateInfo.mStartGlyphIndex;
1376 const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
1377 const Length totalNumberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
1379 if( 0u == totalNumberOfGlyphs )
1381 if( UPDATE_ACTUAL_SIZE & operations )
1383 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
1386 // Nothing else to do if there is no glyphs.
1387 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1391 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1392 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1393 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1394 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1395 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1396 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1397 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1399 // Set the layout parameters.
1400 LayoutParameters layoutParameters( size,
1402 lineBreakInfo.Begin(),
1403 wordBreakInfo.Begin(),
1404 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1406 glyphsToCharactersMap.Begin(),
1407 charactersPerGlyph.Begin(),
1408 charactersToGlyphBuffer,
1409 glyphsPerCharacterBuffer,
1410 totalNumberOfGlyphs );
1412 // Resize the vector of positions to have the same size than the vector of glyphs.
1413 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1414 glyphPositions.Resize( totalNumberOfGlyphs );
1416 // Whether the last character is a new paragraph character.
1417 mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1418 layoutParameters.isLastNewParagraph = mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph;
1420 // The initial glyph and the number of glyphs to layout.
1421 layoutParameters.startGlyphIndex = startGlyphIndex;
1422 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1423 layoutParameters.startLineIndex = mImpl->mTextUpdateInfo.mStartLineIndex;
1424 layoutParameters.estimatedNumberOfLines = mImpl->mTextUpdateInfo.mEstimatedNumberOfLines;
1426 // Update the visual model.
1427 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1429 mImpl->mVisualModel->mLines,
1434 // Reorder the lines
1435 if( REORDER & operations )
1437 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1438 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1440 // Check first if there are paragraphs with bidirectional info.
1441 if( 0u != bidirectionalInfo.Count() )
1444 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1446 // Reorder the lines.
1447 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1448 ReorderLines( bidirectionalInfo,
1450 requestedNumberOfCharacters,
1451 mImpl->mVisualModel->mLines,
1452 bidirectionalLineInfo );
1454 // Set the bidirectional info per line into the layout parameters.
1455 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1456 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1458 // TODO: update the conversion map instead creating it from scratch.
1459 // Note this tables store indices to characters, so update the table means modify all the indices of the text after the last updated character.
1460 // It's better to refactor this. Store this table per line and don't update the indices.
1461 // For the cursor position probably is better to use the function instead creating a table.
1462 // Set the bidirectional info into the model.
1463 mImpl->mLogicalModel->SetVisualToLogicalMap( layoutParameters.lineBidirectionalInfoRunsBuffer,
1464 layoutParameters.numberOfBidirectionalInfoRuns,
1466 mImpl->mLogicalModel->mText.Count() );
1468 // Re-layout the text. Reorder those lines with right to left characters.
1469 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1471 requestedNumberOfCharacters,
1477 // Sets the actual size.
1478 if( UPDATE_ACTUAL_SIZE & operations )
1480 mImpl->mVisualModel->SetLayoutSize( layoutSize );
1484 // Store the size used to layout the text.
1485 mImpl->mVisualModel->mControlSize = size;
1489 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1492 if( ALIGN & operations )
1494 // The laid-out lines.
1495 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1497 mImpl->mLayoutEngine.Align( size,
1499 requestedNumberOfCharacters,
1505 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1509 void Controller::SetMultiLineEnabled( bool enable )
1511 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1513 if( layout != mImpl->mLayoutEngine.GetLayout() )
1515 // Set the layout type.
1516 mImpl->mLayoutEngine.SetLayout( layout );
1518 // Set the flags to redo the layout operations
1519 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1520 UPDATE_ACTUAL_SIZE |
1524 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1526 mImpl->RequestRelayout();
1530 bool Controller::IsMultiLineEnabled() const
1532 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1535 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1537 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1539 // Set the alignment.
1540 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1542 // Set the flag to redo the alignment operation.
1543 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1545 mImpl->RequestRelayout();
1549 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1551 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1554 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1556 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1558 // Set the alignment.
1559 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1561 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1563 mImpl->RequestRelayout();
1567 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1569 return mImpl->mLayoutEngine.GetVerticalAlignment();
1572 void Controller::CalculateTextAlignment( const Size& controlSize )
1574 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1576 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1578 // Get the line height of the default font.
1579 layoutSize.height = mImpl->GetDefaultFontLineHeight();
1582 if( LayoutEngine::SINGLE_LINE_BOX == mImpl->mLayoutEngine.GetLayout() )
1584 // Get the direction of the first character.
1585 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1587 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1588 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1589 if( firstParagraphDirection )
1591 switch( horizontalAlignment )
1593 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1595 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1598 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1603 case LayoutEngine::HORIZONTAL_ALIGN_END:
1605 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1611 switch( horizontalAlignment )
1613 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1615 mImpl->mAlignmentOffset.x = 0.f;
1618 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1620 mImpl->mAlignmentOffset.x = floorf( 0.5f * ( controlSize.width - layoutSize.width ) ); // try to avoid pixel alignment.
1623 case LayoutEngine::HORIZONTAL_ALIGN_END:
1625 mImpl->mAlignmentOffset.x = controlSize.width - layoutSize.width;
1631 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1632 switch( verticalAlignment )
1634 case LayoutEngine::VERTICAL_ALIGN_TOP:
1636 mImpl->mAlignmentOffset.y = 0.f;
1639 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1641 mImpl->mAlignmentOffset.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
1644 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1646 mImpl->mAlignmentOffset.y = controlSize.height - layoutSize.height;
1652 LayoutEngine& Controller::GetLayoutEngine()
1654 return mImpl->mLayoutEngine;
1657 View& Controller::GetView()
1659 return mImpl->mView;
1662 void Controller::KeyboardFocusGainEvent()
1664 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1666 if( NULL != mImpl->mEventData )
1668 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1669 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1671 mImpl->ChangeState( EventData::EDITING );
1672 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1675 if( mImpl->IsShowingPlaceholderText() )
1677 // Show alternative placeholder-text when editing
1678 ShowPlaceholderText();
1681 mImpl->RequestRelayout();
1685 void Controller::KeyboardFocusLostEvent()
1687 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1689 if( NULL != mImpl->mEventData )
1691 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1693 mImpl->ChangeState( EventData::INACTIVE );
1695 if( !mImpl->IsShowingRealText() )
1697 // Revert to regular placeholder-text when not editing
1698 ShowPlaceholderText();
1702 mImpl->RequestRelayout();
1705 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1707 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1709 bool textChanged( false );
1711 if( ( NULL != mImpl->mEventData ) &&
1712 ( keyEvent.state == KeyEvent::Down ) )
1714 int keyCode = keyEvent.keyCode;
1715 const std::string& keyString = keyEvent.keyPressed;
1717 // Pre-process to separate modifying events from non-modifying input events.
1718 if( Dali::DALI_KEY_ESCAPE == keyCode )
1720 // Escape key is a special case which causes focus loss
1721 KeyboardFocusLostEvent();
1723 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
1724 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1725 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
1726 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
1728 Event event( Event::CURSOR_KEY_EVENT );
1729 event.p1.mInt = keyCode;
1730 mImpl->mEventData->mEventQueue.push_back( event );
1732 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1734 textChanged = BackspaceKeyEvent();
1736 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1738 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1739 // Avoids calling the InsertText() method which can delete selected text
1741 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1742 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1744 mImpl->ChangeState( EventData::INACTIVE );
1745 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1746 // Avoids calling the InsertText() method which can delete selected text
1748 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1750 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1751 // and a character is typed after the type of a upper case latin character.
1757 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1759 // IMF manager is no longer handling key-events
1760 mImpl->ClearPreEditFlag();
1762 InsertText( keyString, COMMIT );
1766 if( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1767 ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1769 mImpl->ChangeState( EventData::EDITING );
1772 mImpl->RequestRelayout();
1777 // Do this last since it provides callbacks into application code
1778 mImpl->mControlInterface.TextChanged();
1784 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1786 bool removedPrevious( false );
1787 bool maxLengthReached( false );
1789 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1791 if( NULL == mImpl->mEventData )
1796 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1797 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1798 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1800 // TODO: At the moment the underline runs are only for pre-edit.
1801 mImpl->mVisualModel->mUnderlineRuns.Clear();
1803 Vector<Character> utf32Characters;
1804 Length characterCount( 0u );
1806 // Remove the previous IMF pre-edit (predicitive text)
1807 if( mImpl->mEventData->mPreEditFlag &&
1808 ( 0u != mImpl->mEventData->mPreEditLength ) )
1810 const CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1812 removedPrevious = RemoveText( -static_cast<int>( offset ),
1813 mImpl->mEventData->mPreEditLength,
1814 DONT_UPDATE_INPUT_STYLE );
1816 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1817 mImpl->mEventData->mPreEditLength = 0u;
1821 // Remove the previous Selection
1822 removedPrevious = RemoveSelectedText();
1827 // Convert text into UTF-32
1828 utf32Characters.Resize( text.size() );
1830 // This is a bit horrible but std::string returns a (signed) char*
1831 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1833 // Transform a text array encoded in utf8 into an array encoded in utf32.
1834 // It returns the actual number of characters.
1835 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1836 utf32Characters.Resize( characterCount );
1838 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1839 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1842 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1844 // The placeholder text is no longer needed
1845 if( mImpl->IsShowingPlaceholderText() )
1850 mImpl->ChangeState( EventData::EDITING );
1852 // Handle the IMF (predicitive text) state changes
1853 if( COMMIT == type )
1855 // IMF manager is no longer handling key-events
1856 mImpl->ClearPreEditFlag();
1860 if( !mImpl->mEventData->mPreEditFlag )
1862 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1864 // Record the start of the pre-edit text
1865 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1868 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1869 mImpl->mEventData->mPreEditFlag = true;
1871 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1874 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1876 // Restrict new text to fit within Maximum characters setting.
1877 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1878 maxLengthReached = ( characterCount > maxSizeOfNewText );
1880 // The cursor position.
1881 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1883 // Update the text's style.
1885 // Updates the text style runs by adding characters.
1886 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
1888 // Get the character index from the cursor index.
1889 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
1891 // Retrieve the text's style for the given index.
1893 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
1895 // Whether to add a new text color run.
1896 const bool addColorRun = style.textColor != mImpl->mEventData->mInputStyle.textColor;
1898 // Whether to add a new font run.
1899 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
1900 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
1901 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
1902 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
1903 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
1908 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
1909 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
1911 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
1912 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
1913 colorRun.characterRun.characterIndex = cursorIndex;
1914 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1917 if( addFontNameRun ||
1923 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
1924 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
1926 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
1928 if( addFontNameRun )
1930 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
1931 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
1932 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
1933 fontDescriptionRun.familyDefined = true;
1935 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
1938 if( addFontWeightRun )
1940 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
1941 fontDescriptionRun.weightDefined = true;
1944 if( addFontWidthRun )
1946 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
1947 fontDescriptionRun.widthDefined = true;
1950 if( addFontSlantRun )
1952 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
1953 fontDescriptionRun.slantDefined = true;
1956 if( addFontSizeRun )
1958 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
1959 fontDescriptionRun.sizeDefined = true;
1962 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
1963 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1966 // Insert at current cursor position.
1967 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1969 if( cursorIndex < numberOfCharactersInModel )
1971 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1975 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1978 // Mark the first paragraph to be updated.
1979 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
1980 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
1982 // Update the cursor index.
1983 cursorIndex += maxSizeOfNewText;
1985 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1988 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
1989 mImpl->IsPlaceholderAvailable() )
1991 // Show place-holder if empty after removing the pre-edit text
1992 ShowPlaceholderText();
1993 mImpl->mEventData->mUpdateCursorPosition = true;
1994 mImpl->ClearPreEditFlag();
1996 else if( removedPrevious ||
1997 ( 0 != utf32Characters.Count() ) )
1999 // Queue an inserted event
2000 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
2003 if( maxLengthReached )
2005 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
2007 mImpl->ResetImfManager();
2009 // Do this last since it provides callbacks into application code
2010 mImpl->mControlInterface.MaxLengthReached();
2014 bool Controller::RemoveSelectedText()
2016 bool textRemoved( false );
2018 if( EventData::SELECTING == mImpl->mEventData->mState )
2020 std::string removedString;
2021 mImpl->RetrieveSelection( removedString, true );
2023 if( !removedString.empty() )
2026 mImpl->ChangeState( EventData::EDITING );
2033 void Controller::TapEvent( unsigned int tapCount, float x, float y )
2035 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
2037 if( NULL != mImpl->mEventData )
2039 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
2041 if( 1u == tapCount )
2043 // This is to avoid unnecessary relayouts when tapping an empty text-field
2044 bool relayoutNeeded( false );
2046 if( ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) ||
2047 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
2049 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE); // If Popup shown hide it here so can be shown again if required.
2052 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
2054 // Already in an active state so show a popup
2055 if( !mImpl->IsClipboardEmpty() )
2057 // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
2058 mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
2062 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2064 relayoutNeeded = true;
2068 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2070 // Hide placeholder text
2074 if( EventData::INACTIVE == mImpl->mEventData->mState )
2076 mImpl->ChangeState( EventData::EDITING );
2078 else if( !mImpl->IsClipboardEmpty() )
2080 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2082 relayoutNeeded = true;
2085 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2086 if( relayoutNeeded )
2088 Event event( Event::TAP_EVENT );
2089 event.p1.mUint = tapCount;
2090 event.p2.mFloat = x;
2091 event.p3.mFloat = y;
2092 mImpl->mEventData->mEventQueue.push_back( event );
2094 mImpl->RequestRelayout();
2097 else if( 2u == tapCount )
2099 if( mImpl->mEventData->mSelectionEnabled &&
2100 mImpl->IsShowingRealText() )
2102 SelectEvent( x, y, false );
2107 // Reset keyboard as tap event has occurred.
2108 mImpl->ResetImfManager();
2111 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2112 // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2114 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2116 if( NULL != mImpl->mEventData )
2118 Event event( Event::PAN_EVENT );
2119 event.p1.mInt = state;
2120 event.p2.mFloat = displacement.x;
2121 event.p3.mFloat = displacement.y;
2122 mImpl->mEventData->mEventQueue.push_back( event );
2124 mImpl->RequestRelayout();
2128 void Controller::LongPressEvent( Gesture::State state, float x, float y )
2130 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2132 if( ( state == Gesture::Started ) &&
2133 ( NULL != mImpl->mEventData ) )
2135 if( !mImpl->IsShowingRealText() )
2137 Event event( Event::LONG_PRESS_EVENT );
2138 event.p1.mInt = state;
2139 mImpl->mEventData->mEventQueue.push_back( event );
2140 mImpl->RequestRelayout();
2144 // The 1st long-press on inactive text-field is treated as tap
2145 if( EventData::INACTIVE == mImpl->mEventData->mState )
2147 mImpl->ChangeState( EventData::EDITING );
2149 Event event( Event::TAP_EVENT );
2151 event.p2.mFloat = x;
2152 event.p3.mFloat = y;
2153 mImpl->mEventData->mEventQueue.push_back( event );
2155 mImpl->RequestRelayout();
2159 // Reset the imf manger to commit the pre-edit before selecting the text.
2160 mImpl->ResetImfManager();
2162 SelectEvent( x, y, false );
2168 void Controller::SelectEvent( float x, float y, bool selectAll )
2170 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2172 if( NULL != mImpl->mEventData )
2174 mImpl->ChangeState( EventData::SELECTING );
2178 Event event( Event::SELECT_ALL );
2179 mImpl->mEventData->mEventQueue.push_back( event );
2183 Event event( Event::SELECT );
2184 event.p2.mFloat = x;
2185 event.p3.mFloat = y;
2186 mImpl->mEventData->mEventQueue.push_back( event );
2189 mImpl->RequestRelayout();
2193 void Controller::GetTargetSize( Vector2& targetSize )
2195 targetSize = mImpl->mVisualModel->mControlSize;
2198 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2200 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2203 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2205 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2207 if( NULL != mImpl->mEventData )
2209 switch( handleType )
2213 Event event( Event::GRAB_HANDLE_EVENT );
2214 event.p1.mUint = state;
2215 event.p2.mFloat = x;
2216 event.p3.mFloat = y;
2218 mImpl->mEventData->mEventQueue.push_back( event );
2221 case LEFT_SELECTION_HANDLE:
2223 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2224 event.p1.mUint = state;
2225 event.p2.mFloat = x;
2226 event.p3.mFloat = y;
2228 mImpl->mEventData->mEventQueue.push_back( event );
2231 case RIGHT_SELECTION_HANDLE:
2233 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2234 event.p1.mUint = state;
2235 event.p2.mFloat = x;
2236 event.p3.mFloat = y;
2238 mImpl->mEventData->mEventQueue.push_back( event );
2241 case LEFT_SELECTION_HANDLE_MARKER:
2242 case RIGHT_SELECTION_HANDLE_MARKER:
2244 // Markers do not move the handles.
2247 case HANDLE_TYPE_COUNT:
2249 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2253 mImpl->RequestRelayout();
2257 void Controller::PasteText( const std::string& stringToPaste )
2259 InsertText( stringToPaste, Text::Controller::COMMIT );
2260 mImpl->ChangeState( EventData::EDITING );
2261 mImpl->RequestRelayout();
2263 // Do this last since it provides callbacks into application code
2264 mImpl->mControlInterface.TextChanged();
2267 void Controller::PasteClipboardItemEvent()
2269 // Retrieve the clipboard contents first
2270 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2271 std::string stringToPaste( notifier.GetContent() );
2273 // Commit the current pre-edit text; the contents of the clipboard should be appended
2274 mImpl->ResetImfManager();
2277 PasteText( stringToPaste );
2280 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2282 if( NULL == mImpl->mEventData )
2289 case Toolkit::TextSelectionPopup::CUT:
2291 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2292 mImpl->mOperationsPending = ALL_OPERATIONS;
2294 // This is to reset the virtual keyboard to Upper-case
2295 if( 0u == mImpl->mLogicalModel->mText.Count() )
2300 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2301 !mImpl->IsPlaceholderAvailable() )
2303 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2307 ShowPlaceholderText();
2308 mImpl->mEventData->mUpdateCursorPosition = true;
2310 mImpl->RequestRelayout();
2311 mImpl->mControlInterface.TextChanged();
2314 case Toolkit::TextSelectionPopup::COPY:
2316 mImpl->SendSelectionToClipboard( false ); // Text not modified
2317 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
2320 case Toolkit::TextSelectionPopup::PASTE:
2322 std::string stringToPaste("");
2323 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2324 PasteText( stringToPaste );
2327 case Toolkit::TextSelectionPopup::SELECT:
2329 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2331 if( mImpl->mEventData->mSelectionEnabled )
2333 // Creates a SELECT event.
2334 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2338 case Toolkit::TextSelectionPopup::SELECT_ALL:
2340 // Creates a SELECT_ALL event
2341 SelectEvent( 0.f, 0.f, true );
2344 case Toolkit::TextSelectionPopup::CLIPBOARD:
2346 mImpl->ShowClipboard();
2349 case Toolkit::TextSelectionPopup::NONE:
2357 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2359 bool update = false;
2360 bool requestRelayout = false;
2363 unsigned int cursorPosition = 0u;
2365 switch( imfEvent.eventName )
2367 case ImfManager::COMMIT:
2369 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2371 requestRelayout = true;
2374 case ImfManager::PREEDIT:
2376 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2378 requestRelayout = true;
2381 case ImfManager::DELETESURROUNDING:
2383 update = RemoveText( imfEvent.cursorOffset,
2384 imfEvent.numberOfChars,
2385 DONT_UPDATE_INPUT_STYLE );
2389 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2390 !mImpl->IsPlaceholderAvailable() )
2392 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2396 ShowPlaceholderText();
2397 mImpl->mEventData->mUpdateCursorPosition = true;
2400 requestRelayout = true;
2403 case ImfManager::GETSURROUNDING:
2406 cursorPosition = GetLogicalCursorPosition();
2408 imfManager.SetSurroundingText( text );
2409 imfManager.SetCursorPosition( cursorPosition );
2412 case ImfManager::VOID:
2419 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
2422 cursorPosition = GetLogicalCursorPosition();
2425 if( requestRelayout )
2427 mImpl->mOperationsPending = ALL_OPERATIONS;
2428 mImpl->RequestRelayout();
2430 // Do this last since it provides callbacks into application code
2431 mImpl->mControlInterface.TextChanged();
2434 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
2436 return callbackData;
2439 Controller::~Controller()
2444 bool Controller::BackspaceKeyEvent()
2446 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2448 bool removed = false;
2450 if( NULL == mImpl->mEventData )
2455 // IMF manager is no longer handling key-events
2456 mImpl->ClearPreEditFlag();
2458 if( EventData::SELECTING == mImpl->mEventData->mState )
2460 removed = RemoveSelectedText();
2462 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2464 // Remove the character before the current cursor position
2465 removed = RemoveText( -1,
2467 UPDATE_INPUT_STYLE );
2472 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE RemovedText\n", this );
2473 // Notifiy the IMF manager after text changed
2474 // Automatic Upper-case and restarting prediction on an existing word require this.
2477 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2478 !mImpl->IsPlaceholderAvailable() )
2480 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2484 ShowPlaceholderText();
2485 mImpl->mEventData->mUpdateCursorPosition = true;
2492 void Controller::NotifyImfManager()
2494 if( NULL != mImpl->mEventData )
2496 if( mImpl->mEventData->mImfManager )
2498 // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
2501 mImpl->mEventData->mImfManager.SetSurroundingText( text );
2503 mImpl->mEventData->mImfManager.SetCursorPosition( GetLogicalCursorPosition() );
2504 mImpl->mEventData->mImfManager.NotifyCursorPosition();
2509 void Controller::ShowPlaceholderText()
2511 if( mImpl->IsPlaceholderAvailable() )
2513 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2515 if( NULL == mImpl->mEventData )
2520 mImpl->mEventData->mIsShowingPlaceholderText = true;
2522 // Disable handles when showing place-holder text
2523 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2524 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2525 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2527 const char* text( NULL );
2530 // TODO - Switch placeholder text styles when changing state
2531 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2532 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2534 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2535 size = mImpl->mEventData->mPlaceholderTextActive.size();
2539 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2540 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2543 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2544 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2546 // Reset model for showing placeholder.
2547 mImpl->mLogicalModel->mText.Clear();
2548 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2550 // Convert text into UTF-32
2551 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2552 utf32Characters.Resize( size );
2554 // This is a bit horrible but std::string returns a (signed) char*
2555 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2557 // Transform a text array encoded in utf8 into an array encoded in utf32.
2558 // It returns the actual number of characters.
2559 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2560 utf32Characters.Resize( characterCount );
2562 // The characters to be added.
2563 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = characterCount;
2565 // Reset the cursor position
2566 mImpl->mEventData->mPrimaryCursorPosition = 0;
2568 // The natural size needs to be re-calculated.
2569 mImpl->mRecalculateNaturalSize = true;
2571 // Apply modifications to the model
2572 mImpl->mOperationsPending = ALL_OPERATIONS;
2574 // Update the rest of the model during size negotiation
2575 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2579 void Controller::ClearFontData()
2581 if( mImpl->mFontDefaults )
2583 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2585 mImpl->mVisualModel->ClearCaches();
2587 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2588 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2589 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
2591 mImpl->mTextUpdateInfo.mClearAll = true;
2592 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2597 UPDATE_ACTUAL_SIZE |
2602 void Controller::ClearStyleData()
2604 mImpl->mLogicalModel->mColorRuns.Clear();
2605 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2608 Controller::Controller( ControlInterface& controlInterface )
2611 mImpl = new Controller::Impl( controlInterface );
2616 } // namespace Toolkit