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.
71 * @param[out] startOfSelectedText Index to the first selected character.
72 * @param[out] lengthOfSelectedText Number of selected characters.
74 FontDescriptionRun& UpdateSelectionFontStyleRun( EventData* eventData,
75 LogicalModelPtr logicalModel,
76 CharacterIndex& startOfSelectedText,
77 Length& lengthOfSelectedText )
79 const bool handlesCrossed = eventData->mLeftSelectionPosition > eventData->mRightSelectionPosition;
81 // Get start and end position of selection
82 startOfSelectedText = handlesCrossed ? eventData->mRightSelectionPosition : eventData->mLeftSelectionPosition;
83 lengthOfSelectedText = ( handlesCrossed ? eventData->mLeftSelectionPosition : eventData->mRightSelectionPosition ) - startOfSelectedText;
86 const VectorBase::SizeType numberOfRuns = logicalModel->mFontDescriptionRuns.Count();
87 logicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
89 FontDescriptionRun& fontDescriptionRun = *( logicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
91 fontDescriptionRun.characterRun.characterIndex = startOfSelectedText;
92 fontDescriptionRun.characterRun.numberOfCharacters = lengthOfSelectedText;
94 // Recalculate the selection highlight as the metrics may have changed.
95 eventData->mUpdateLeftSelectionPosition = true;
96 eventData->mUpdateRightSelectionPosition = true;
98 return fontDescriptionRun;
101 ControllerPtr Controller::New( ControlInterface& controlInterface )
103 return ControllerPtr( new Controller( controlInterface ) );
106 void Controller::EnableTextInput( DecoratorPtr decorator )
108 if( NULL == mImpl->mEventData )
110 mImpl->mEventData = new EventData( decorator );
114 void Controller::SetGlyphType( TextAbstraction::GlyphType glyphType )
116 // Metrics for bitmap & vector based glyphs are different
117 mImpl->mMetrics->SetGlyphType( glyphType );
119 // Clear the font-specific data
122 mImpl->RequestRelayout();
125 void Controller::SetMarkupProcessorEnabled( bool enable )
127 mImpl->mMarkupProcessorEnabled = enable;
130 bool Controller::IsMarkupProcessorEnabled() const
132 return mImpl->mMarkupProcessorEnabled;
135 void Controller::SetAutoScrollEnabled( bool enable )
137 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled[%s] SingleBox[%s]-> [%p]\n", (enable)?"true":"false", ( mImpl->mLayoutEngine.GetLayout() == LayoutEngine::SINGLE_LINE_BOX)?"true":"false", this );
139 if ( mImpl->mLayoutEngine.GetLayout() == LayoutEngine::SINGLE_LINE_BOX )
143 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled for SINGLE_LINE_BOX\n" );
144 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
154 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled Disabling autoscroll\n");
155 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
162 mImpl->mAutoScrollEnabled = enable;
163 mImpl->RequestRelayout();
167 DALI_LOG_WARNING( "Attempted AutoScrolling on a non SINGLE_LINE_BOX, request ignored" );
168 mImpl->mAutoScrollEnabled = false;
172 bool Controller::IsAutoScrollEnabled() const
174 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::IsAutoScrollEnabled[%s]\n", (mImpl->mAutoScrollEnabled)?"true":"false" );
176 return mImpl->mAutoScrollEnabled;
179 CharacterDirection Controller::GetAutoScrollDirection() const
181 return mImpl->mAutoScrollDirectionRTL;
184 void Controller::SetText( const std::string& text )
186 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
188 // Reset keyboard as text changed
189 mImpl->ResetImfManager();
191 // Remove the previously set text and style.
197 CharacterIndex lastCursorIndex = 0u;
199 if( NULL != mImpl->mEventData )
201 // If popup shown then hide it by switching to Editing state
202 if( ( EventData::SELECTING == mImpl->mEventData->mState ) ||
203 ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
204 ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
205 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
207 mImpl->ChangeState( EventData::EDITING );
213 mImpl->mVisualModel->SetTextColor( mImpl->mTextColor );
215 MarkupProcessData markupProcessData( mImpl->mLogicalModel->mColorRuns,
216 mImpl->mLogicalModel->mFontDescriptionRuns );
218 Length textSize = 0u;
219 const uint8_t* utf8 = NULL;
220 if( mImpl->mMarkupProcessorEnabled )
222 ProcessMarkupString( text, markupProcessData );
223 textSize = markupProcessData.markupProcessedText.size();
225 // This is a bit horrible but std::string returns a (signed) char*
226 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
230 textSize = text.size();
232 // This is a bit horrible but std::string returns a (signed) char*
233 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
236 // Convert text into UTF-32
237 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
238 utf32Characters.Resize( textSize );
240 // Transform a text array encoded in utf8 into an array encoded in utf32.
241 // It returns the actual number of characters.
242 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
243 utf32Characters.Resize( characterCount );
245 DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
246 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mLogicalModel->mText.Count() );
248 // The characters to be added.
249 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
251 // To reset the cursor position
252 lastCursorIndex = characterCount;
254 // Update the rest of the model during size negotiation
255 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
257 // The natural size needs to be re-calculated.
258 mImpl->mRecalculateNaturalSize = true;
260 // Apply modifications to the model
261 mImpl->mOperationsPending = ALL_OPERATIONS;
265 ShowPlaceholderText();
268 // Resets the cursor position.
269 ResetCursorPosition( lastCursorIndex );
271 // Scrolls the text to make the cursor visible.
272 ResetScrollPosition();
274 mImpl->RequestRelayout();
276 if( NULL != mImpl->mEventData )
278 // Cancel previously queued events
279 mImpl->mEventData->mEventQueue.clear();
282 // Notify IMF as text changed
285 // Do this last since it provides callbacks into application code
286 mImpl->mControlInterface.TextChanged();
289 void Controller::GetText( std::string& text ) const
291 if( !mImpl->IsShowingPlaceholderText() )
293 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
295 if( 0u != utf32Characters.Count() )
297 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
302 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
306 unsigned int Controller::GetLogicalCursorPosition() const
308 if( NULL != mImpl->mEventData )
310 return mImpl->mEventData->mPrimaryCursorPosition;
316 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
318 if( NULL != mImpl->mEventData )
320 if( PLACEHOLDER_TYPE_INACTIVE == type )
322 mImpl->mEventData->mPlaceholderTextInactive = text;
326 mImpl->mEventData->mPlaceholderTextActive = text;
329 // Update placeholder if there is no text
330 if( mImpl->IsShowingPlaceholderText() ||
331 ( 0u == mImpl->mLogicalModel->mText.Count() ) )
333 ShowPlaceholderText();
338 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
340 if( NULL != mImpl->mEventData )
342 if( PLACEHOLDER_TYPE_INACTIVE == type )
344 text = mImpl->mEventData->mPlaceholderTextInactive;
348 text = mImpl->mEventData->mPlaceholderTextActive;
353 void Controller::SetMaximumNumberOfCharacters( Length maxCharacters )
355 mImpl->mMaximumNumberOfCharacters = maxCharacters;
358 int Controller::GetMaximumNumberOfCharacters()
360 return mImpl->mMaximumNumberOfCharacters;
363 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
365 if( NULL == mImpl->mFontDefaults )
367 mImpl->mFontDefaults = new FontDefaults();
370 mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
371 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
372 mImpl->mFontDefaults->familyDefined = true;
374 // Clear the font-specific data
377 mImpl->RequestRelayout();
380 const std::string& Controller::GetDefaultFontFamily() const
382 if( NULL != mImpl->mFontDefaults )
384 return mImpl->mFontDefaults->mFontDescription.family;
390 void Controller::SetDefaultFontStyle( const std::string& style )
392 if( NULL == mImpl->mFontDefaults )
394 mImpl->mFontDefaults = new FontDefaults();
397 mImpl->mFontDefaults->mFontStyle = style;
400 const std::string& Controller::GetDefaultFontStyle() const
402 if( NULL != mImpl->mFontDefaults )
404 return mImpl->mFontDefaults->mFontStyle;
410 void Controller::SetDefaultFontWeight( FontWeight weight )
412 if( NULL == mImpl->mFontDefaults )
414 mImpl->mFontDefaults = new FontDefaults();
417 mImpl->mFontDefaults->mFontDescription.weight = weight;
418 mImpl->mFontDefaults->weightDefined = true;
420 // Clear the font-specific data
423 mImpl->RequestRelayout();
426 FontWeight Controller::GetDefaultFontWeight() const
428 if( NULL != mImpl->mFontDefaults )
430 return mImpl->mFontDefaults->mFontDescription.weight;
433 return TextAbstraction::FontWeight::NORMAL;
436 void Controller::SetDefaultFontWidth( FontWidth width )
438 if( NULL == mImpl->mFontDefaults )
440 mImpl->mFontDefaults = new FontDefaults();
443 mImpl->mFontDefaults->mFontDescription.width = width;
444 mImpl->mFontDefaults->widthDefined = true;
446 // Clear the font-specific data
449 mImpl->RequestRelayout();
452 FontWidth Controller::GetDefaultFontWidth() const
454 if( NULL != mImpl->mFontDefaults )
456 return mImpl->mFontDefaults->mFontDescription.width;
459 return TextAbstraction::FontWidth::NORMAL;
462 void Controller::SetDefaultFontSlant( FontSlant slant )
464 if( NULL == mImpl->mFontDefaults )
466 mImpl->mFontDefaults = new FontDefaults();
469 mImpl->mFontDefaults->mFontDescription.slant = slant;
470 mImpl->mFontDefaults->slantDefined = true;
472 // Clear the font-specific data
475 mImpl->RequestRelayout();
478 FontSlant Controller::GetDefaultFontSlant() const
480 if( NULL != mImpl->mFontDefaults )
482 return mImpl->mFontDefaults->mFontDescription.slant;
485 return TextAbstraction::FontSlant::NORMAL;
488 void Controller::SetDefaultPointSize( float pointSize )
490 if( NULL == mImpl->mFontDefaults )
492 mImpl->mFontDefaults = new FontDefaults();
495 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
496 mImpl->mFontDefaults->sizeDefined = true;
498 unsigned int horizontalDpi( 0u );
499 unsigned int verticalDpi( 0u );
500 mImpl->mFontClient.GetDpi( horizontalDpi, verticalDpi );
502 // Adjust the metrics if the fixed-size font should be down-scaled
503 int maxEmojiSize( pointSize/POINTS_PER_INCH * verticalDpi );
504 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultPointSize %p setting MaxEmojiSize %d\n", this, maxEmojiSize );
505 mImpl->mMetrics->SetMaxEmojiSize( maxEmojiSize );
507 // Clear the font-specific data
510 mImpl->RequestRelayout();
513 float Controller::GetDefaultPointSize() const
515 if( NULL != mImpl->mFontDefaults )
517 return mImpl->mFontDefaults->mDefaultPointSize;
523 void Controller::UpdateAfterFontChange( const std::string& newDefaultFont )
525 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::UpdateAfterFontChange");
527 if( !mImpl->mFontDefaults->familyDefined ) // If user defined font then should not update when system font changes
529 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
530 mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
534 mImpl->RequestRelayout();
538 void Controller::SetTextColor( const Vector4& textColor )
540 mImpl->mTextColor = textColor;
542 if( !mImpl->IsShowingPlaceholderText() )
544 mImpl->mVisualModel->SetTextColor( textColor );
546 mImpl->RequestRelayout();
550 const Vector4& Controller::GetTextColor() const
552 return mImpl->mTextColor;
555 bool Controller::RemoveText( int cursorOffset,
556 int numberOfCharacters,
557 UpdateInputStyleType type )
559 bool removed = false;
561 if( NULL == mImpl->mEventData )
566 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfCharacters %d\n",
567 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters );
569 if( !mImpl->IsShowingPlaceholderText() )
571 // Delete at current cursor position
572 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
573 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
575 CharacterIndex cursorIndex = oldCursorIndex;
577 // Validate the cursor position & number of characters
578 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
580 cursorIndex = oldCursorIndex + cursorOffset;
583 if( ( cursorIndex + numberOfCharacters ) > currentText.Count() )
585 numberOfCharacters = currentText.Count() - cursorIndex;
588 if( ( cursorIndex + numberOfCharacters ) <= mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters )
590 // Mark the paragraphs to be updated.
591 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
592 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove += numberOfCharacters;
594 // Update the input style and remove the text's style before removing the text.
596 if( UPDATE_INPUT_STYLE == type )
598 // Set first the default input style.
599 mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
601 // Update the input style.
602 mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
605 // Updates the text style runs by removing characters. Runs with no characters are removed.
606 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfCharacters );
608 // Remove the characters.
609 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
610 Vector<Character>::Iterator last = first + numberOfCharacters;
612 currentText.Erase( first, last );
614 // Cursor position retreat
615 oldCursorIndex = cursorIndex;
617 mImpl->mEventData->mScrollAfterDelete = true;
619 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfCharacters );
627 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
629 if( NULL != mImpl->mEventData )
631 mImpl->mEventData->mPlaceholderTextColor = textColor;
634 if( mImpl->IsShowingPlaceholderText() )
636 mImpl->mVisualModel->SetTextColor( textColor );
637 mImpl->RequestRelayout();
641 const Vector4& Controller::GetPlaceholderTextColor() const
643 if( NULL != mImpl->mEventData )
645 return mImpl->mEventData->mPlaceholderTextColor;
651 void Controller::SetShadowOffset( const Vector2& shadowOffset )
653 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
655 mImpl->RequestRelayout();
658 const Vector2& Controller::GetShadowOffset() const
660 return mImpl->mVisualModel->GetShadowOffset();
663 void Controller::SetShadowColor( const Vector4& shadowColor )
665 mImpl->mVisualModel->SetShadowColor( shadowColor );
667 mImpl->RequestRelayout();
670 const Vector4& Controller::GetShadowColor() const
672 return mImpl->mVisualModel->GetShadowColor();
675 void Controller::SetDefaultShadowProperties( const std::string& shadowProperties )
677 if( NULL == mImpl->mShadowDefaults )
679 mImpl->mShadowDefaults = new ShadowDefaults();
682 mImpl->mShadowDefaults->properties = shadowProperties;
685 const std::string& Controller::GetDefaultShadowProperties() const
687 if( NULL != mImpl->mShadowDefaults )
689 return mImpl->mShadowDefaults->properties;
695 void Controller::SetUnderlineColor( const Vector4& color )
697 mImpl->mVisualModel->SetUnderlineColor( color );
699 mImpl->RequestRelayout();
702 const Vector4& Controller::GetUnderlineColor() const
704 return mImpl->mVisualModel->GetUnderlineColor();
707 void Controller::SetUnderlineEnabled( bool enabled )
709 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
711 mImpl->RequestRelayout();
714 bool Controller::IsUnderlineEnabled() const
716 return mImpl->mVisualModel->IsUnderlineEnabled();
719 void Controller::SetUnderlineHeight( float height )
721 mImpl->mVisualModel->SetUnderlineHeight( height );
723 mImpl->RequestRelayout();
726 float Controller::GetUnderlineHeight() const
728 return mImpl->mVisualModel->GetUnderlineHeight();
731 void Controller::SetDefaultUnderlineProperties( const std::string& underlineProperties )
733 if( NULL == mImpl->mUnderlineDefaults )
735 mImpl->mUnderlineDefaults = new UnderlineDefaults();
738 mImpl->mUnderlineDefaults->properties = underlineProperties;
741 const std::string& Controller::GetDefaultUnderlineProperties() const
743 if( NULL != mImpl->mUnderlineDefaults )
745 return mImpl->mUnderlineDefaults->properties;
751 void Controller::SetDefaultEmbossProperties( const std::string& embossProperties )
753 if( NULL == mImpl->mEmbossDefaults )
755 mImpl->mEmbossDefaults = new EmbossDefaults();
758 mImpl->mEmbossDefaults->properties = embossProperties;
761 const std::string& Controller::GetDefaultEmbossProperties() const
763 if( NULL != mImpl->mEmbossDefaults )
765 return mImpl->mEmbossDefaults->properties;
771 void Controller::SetDefaultOutlineProperties( const std::string& outlineProperties )
773 if( NULL == mImpl->mOutlineDefaults )
775 mImpl->mOutlineDefaults = new OutlineDefaults();
778 mImpl->mOutlineDefaults->properties = outlineProperties;
781 const std::string& Controller::GetDefaultOutlineProperties() const
783 if( NULL != mImpl->mOutlineDefaults )
785 return mImpl->mOutlineDefaults->properties;
791 void Controller::SetDefaultLineSpacing( float lineSpacing )
793 //TODO finish implementation
794 mImpl->mLayoutEngine.SetDefaultLineSpacing( lineSpacing );
797 float Controller::GetDefaultLineSpacing() const
799 return mImpl->mLayoutEngine.GetDefaultLineSpacing();
802 void Controller::SetInputColor( const Vector4& color )
804 if( NULL != mImpl->mEventData )
806 mImpl->mEventData->mInputStyle.textColor = color;
807 mImpl->mEventData->mInputStyle.isDefaultColor = false;
809 if( EventData::SELECTING == mImpl->mEventData->mState )
811 const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
813 // Get start and end position of selection
814 const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
815 const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
817 // Add the color run.
818 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
819 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
821 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
822 colorRun.color = color;
823 colorRun.characterRun.characterIndex = startOfSelectedText;
824 colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
826 // Request to relayout.
827 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
828 mImpl->RequestRelayout();
830 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
831 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
832 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
837 const Vector4& Controller::GetInputColor() const
839 if( NULL != mImpl->mEventData )
841 return mImpl->mEventData->mInputStyle.textColor;
844 // Return the default text's color if there is no EventData.
845 return mImpl->mTextColor;
849 void Controller::SetInputFontFamily( const std::string& fontFamily )
851 if( NULL != mImpl->mEventData )
853 mImpl->mEventData->mInputStyle.familyName = fontFamily;
854 mImpl->mEventData->mInputStyle.familyDefined = true;
856 if( EventData::SELECTING == mImpl->mEventData->mState )
858 CharacterIndex startOfSelectedText = 0u;
859 Length lengthOfSelectedText = 0u;
860 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
861 mImpl->mLogicalModel,
863 lengthOfSelectedText );
865 fontDescriptionRun.familyLength = fontFamily.size();
866 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
867 memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
868 fontDescriptionRun.familyDefined = true;
870 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
872 // Request to relayout.
873 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
881 mImpl->mRecalculateNaturalSize = true;
882 mImpl->RequestRelayout();
884 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
885 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
886 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
888 // As the font changes, recalculate the handle positions is needed.
889 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
890 mImpl->mEventData->mUpdateRightSelectionPosition = true;
891 mImpl->mEventData->mScrollAfterUpdatePosition = true;
896 const std::string& Controller::GetInputFontFamily() const
898 if( NULL != mImpl->mEventData )
900 return mImpl->mEventData->mInputStyle.familyName;
903 // Return the default font's family if there is no EventData.
904 return GetDefaultFontFamily();
907 void Controller::SetInputFontStyle( const std::string& fontStyle )
909 if( NULL != mImpl->mEventData )
911 mImpl->mEventData->mInputStyle.fontStyle = fontStyle;
915 const std::string& Controller::GetInputFontStyle() const
917 if( NULL != mImpl->mEventData )
919 return mImpl->mEventData->mInputStyle.fontStyle;
922 // Return the default font's style if there is no EventData.
923 return GetDefaultFontStyle();
926 void Controller::SetInputFontWeight( FontWeight weight )
928 if( NULL != mImpl->mEventData )
930 mImpl->mEventData->mInputStyle.weight = weight;
931 mImpl->mEventData->mInputStyle.weightDefined = true;
933 if( EventData::SELECTING == mImpl->mEventData->mState )
935 CharacterIndex startOfSelectedText = 0u;
936 Length lengthOfSelectedText = 0u;
937 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
938 mImpl->mLogicalModel,
940 lengthOfSelectedText );
942 fontDescriptionRun.weight = weight;
943 fontDescriptionRun.weightDefined = true;
945 // Request to relayout.
946 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
954 mImpl->mRecalculateNaturalSize = true;
955 mImpl->RequestRelayout();
957 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
958 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
959 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
961 // As the font might change, recalculate the handle positions is needed.
962 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
963 mImpl->mEventData->mUpdateRightSelectionPosition = true;
964 mImpl->mEventData->mScrollAfterUpdatePosition = true;
969 FontWeight Controller::GetInputFontWeight() const
971 if( NULL != mImpl->mEventData )
973 return mImpl->mEventData->mInputStyle.weight;
976 return GetDefaultFontWeight();
979 void Controller::SetInputFontWidth( FontWidth width )
981 if( NULL != mImpl->mEventData )
983 mImpl->mEventData->mInputStyle.width = width;
984 mImpl->mEventData->mInputStyle.widthDefined = true;
986 if( EventData::SELECTING == mImpl->mEventData->mState )
988 CharacterIndex startOfSelectedText = 0u;
989 Length lengthOfSelectedText = 0u;
990 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
991 mImpl->mLogicalModel,
993 lengthOfSelectedText );
995 fontDescriptionRun.width = width;
996 fontDescriptionRun.widthDefined = true;
998 // Request to relayout.
999 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1004 UPDATE_ACTUAL_SIZE |
1007 mImpl->mRecalculateNaturalSize = true;
1008 mImpl->RequestRelayout();
1010 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1011 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1012 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1014 // As the font might change, recalculate the handle positions is needed.
1015 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1016 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1017 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1022 FontWidth Controller::GetInputFontWidth() const
1024 if( NULL != mImpl->mEventData )
1026 return mImpl->mEventData->mInputStyle.width;
1029 return GetDefaultFontWidth();
1032 void Controller::SetInputFontSlant( FontSlant slant )
1034 if( NULL != mImpl->mEventData )
1036 mImpl->mEventData->mInputStyle.slant = slant;
1037 mImpl->mEventData->mInputStyle.slantDefined = true;
1039 if( EventData::SELECTING == mImpl->mEventData->mState )
1041 CharacterIndex startOfSelectedText = 0u;
1042 Length lengthOfSelectedText = 0u;
1043 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1044 mImpl->mLogicalModel,
1045 startOfSelectedText,
1046 lengthOfSelectedText );
1048 fontDescriptionRun.slant = slant;
1049 fontDescriptionRun.slantDefined = true;
1051 // Request to relayout.
1052 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1057 UPDATE_ACTUAL_SIZE |
1060 mImpl->mRecalculateNaturalSize = true;
1061 mImpl->RequestRelayout();
1063 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1064 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1065 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1067 // As the font might change, recalculate the handle positions is needed.
1068 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1069 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1070 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1075 FontSlant Controller::GetInputFontSlant() const
1077 if( NULL != mImpl->mEventData )
1079 return mImpl->mEventData->mInputStyle.slant;
1082 return GetDefaultFontSlant();
1085 void Controller::SetInputFontPointSize( float size )
1087 if( NULL != mImpl->mEventData )
1089 mImpl->mEventData->mInputStyle.size = size;
1091 if( EventData::SELECTING == mImpl->mEventData->mState )
1093 CharacterIndex startOfSelectedText = 0u;
1094 Length lengthOfSelectedText = 0u;
1095 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1096 mImpl->mLogicalModel,
1097 startOfSelectedText,
1098 lengthOfSelectedText );
1100 fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
1101 fontDescriptionRun.sizeDefined = true;
1103 // Request to relayout.
1104 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1109 UPDATE_ACTUAL_SIZE |
1112 mImpl->mRecalculateNaturalSize = true;
1113 mImpl->RequestRelayout();
1115 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1116 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1117 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1119 // As the font might change, recalculate the handle positions is needed.
1120 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1121 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1122 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1127 float Controller::GetInputFontPointSize() const
1129 if( NULL != mImpl->mEventData )
1131 return mImpl->mEventData->mInputStyle.size;
1134 // Return the default font's point size if there is no EventData.
1135 return GetDefaultPointSize();
1138 void Controller::SetInputLineSpacing( float lineSpacing )
1140 if( NULL != mImpl->mEventData )
1142 mImpl->mEventData->mInputStyle.lineSpacing = lineSpacing;
1146 float Controller::GetInputLineSpacing() const
1148 if( NULL != mImpl->mEventData )
1150 return mImpl->mEventData->mInputStyle.lineSpacing;
1156 void Controller::SetInputShadowProperties( const std::string& shadowProperties )
1158 if( NULL != mImpl->mEventData )
1160 mImpl->mEventData->mInputStyle.shadowProperties = shadowProperties;
1164 const std::string& Controller::GetInputShadowProperties() const
1166 if( NULL != mImpl->mEventData )
1168 return mImpl->mEventData->mInputStyle.shadowProperties;
1171 return GetDefaultShadowProperties();
1174 void Controller::SetInputUnderlineProperties( const std::string& underlineProperties )
1176 if( NULL != mImpl->mEventData )
1178 mImpl->mEventData->mInputStyle.underlineProperties = underlineProperties;
1182 const std::string& Controller::GetInputUnderlineProperties() const
1184 if( NULL != mImpl->mEventData )
1186 return mImpl->mEventData->mInputStyle.underlineProperties;
1189 return GetDefaultUnderlineProperties();
1192 void Controller::SetInputEmbossProperties( const std::string& embossProperties )
1194 if( NULL != mImpl->mEventData )
1196 mImpl->mEventData->mInputStyle.embossProperties = embossProperties;
1200 const std::string& Controller::GetInputEmbossProperties() const
1202 if( NULL != mImpl->mEventData )
1204 return mImpl->mEventData->mInputStyle.embossProperties;
1207 return GetDefaultEmbossProperties();
1210 void Controller::SetInputOutlineProperties( const std::string& outlineProperties )
1212 if( NULL != mImpl->mEventData )
1214 mImpl->mEventData->mInputStyle.outlineProperties = outlineProperties;
1218 const std::string& Controller::GetInputOutlineProperties() const
1220 if( NULL != mImpl->mEventData )
1222 return mImpl->mEventData->mInputStyle.outlineProperties;
1225 return GetDefaultOutlineProperties();
1228 void Controller::SetEnableCursorBlink( bool enable )
1230 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
1232 if( NULL != mImpl->mEventData )
1234 mImpl->mEventData->mCursorBlinkEnabled = enable;
1237 mImpl->mEventData->mDecorator )
1239 mImpl->mEventData->mDecorator->StopCursorBlink();
1244 bool Controller::GetEnableCursorBlink() const
1246 if( NULL != mImpl->mEventData )
1248 return mImpl->mEventData->mCursorBlinkEnabled;
1254 const Vector2& Controller::GetScrollPosition() const
1256 if( NULL != mImpl->mEventData )
1258 return mImpl->mEventData->mScrollPosition;
1261 return Vector2::ZERO;
1264 const Vector2& Controller::GetAlignmentOffset() const
1266 return mImpl->mAlignmentOffset;
1269 Vector3 Controller::GetNaturalSize()
1271 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
1272 Vector3 naturalSize;
1274 // Make sure the model is up-to-date before layouting
1275 ProcessModifyEvents();
1277 if( mImpl->mRecalculateNaturalSize )
1279 // Operations that can be done only once until the text changes.
1280 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1287 GET_GLYPH_METRICS );
1288 // Make sure the model is up-to-date before layouting
1289 mImpl->UpdateModel( onlyOnceOperations );
1291 // Layout the text for the new width.
1292 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1294 // Set the update info to relayout the whole text.
1295 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1296 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1298 // Store the actual control's size to restore later.
1299 const Size actualControlSize = mImpl->mVisualModel->mControlSize;
1301 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
1302 static_cast<OperationsMask>( onlyOnceOperations |
1304 naturalSize.GetVectorXY() );
1306 // Do not do again the only once operations.
1307 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1309 // Do the size related operations again.
1310 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1313 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1315 // Stores the natural size to avoid recalculate it again
1316 // unless the text/style changes.
1317 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
1319 mImpl->mRecalculateNaturalSize = false;
1321 // Clear the update info. This info will be set the next time the text is updated.
1322 mImpl->mTextUpdateInfo.Clear();
1324 // Restore the actual control's size.
1325 mImpl->mVisualModel->mControlSize = actualControlSize;
1327 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1331 naturalSize = mImpl->mVisualModel->GetNaturalSize();
1333 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1336 naturalSize.x = ConvertToEven( naturalSize.x );
1337 naturalSize.y = ConvertToEven( naturalSize.y );
1342 float Controller::GetHeightForWidth( float width )
1344 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1345 // Make sure the model is up-to-date before layouting
1346 ProcessModifyEvents();
1349 if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
1351 // Operations that can be done only once until the text changes.
1352 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1359 GET_GLYPH_METRICS );
1360 // Make sure the model is up-to-date before layouting
1361 mImpl->UpdateModel( onlyOnceOperations );
1364 // Layout the text for the new width.
1365 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1367 // Set the update info to relayout the whole text.
1368 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1369 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1371 // Store the actual control's width.
1372 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
1374 DoRelayout( Size( width, MAX_FLOAT ),
1375 static_cast<OperationsMask>( onlyOnceOperations |
1379 // Do not do again the only once operations.
1380 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1382 // Do the size related operations again.
1383 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1387 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1389 // Clear the update info. This info will be set the next time the text is updated.
1390 mImpl->mTextUpdateInfo.Clear();
1392 // Restore the actual control's width.
1393 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1395 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1399 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1400 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1403 return layoutSize.height;
1406 bool Controller::Relayout( const Size& size )
1408 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", this, size.width, size.height, (mImpl->mAutoScrollEnabled)?"true":"false" );
1410 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1412 bool glyphsRemoved( false );
1413 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1415 mImpl->mVisualModel->mGlyphPositions.Clear();
1416 glyphsRemoved = true;
1419 // Clear the update info. This info will be set the next time the text is updated.
1420 mImpl->mTextUpdateInfo.Clear();
1422 // Not worth to relayout if width or height is equal to zero.
1423 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1425 return glyphsRemoved;
1428 // Whether a new size has been set.
1429 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1433 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1435 // Layout operations that need to be done if the size changes.
1436 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1439 UPDATE_ACTUAL_SIZE |
1441 // Set the update info to relayout the whole text.
1442 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
1443 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1446 // Whether there are modify events.
1447 if( 0u != mImpl->mModifyEvents.Count() )
1449 // Style operations that need to be done if the text is modified.
1450 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1454 // Make sure the model is up-to-date before layouting.
1455 ProcessModifyEvents();
1456 bool updated = mImpl->UpdateModel( mImpl->mOperationsPending );
1460 updated = DoRelayout( size,
1461 mImpl->mOperationsPending,
1462 layoutSize ) || updated;
1464 // Do not re-do any operation until something changes.
1465 mImpl->mOperationsPending = NO_OPERATION;
1467 // Whether the text control is editable
1468 const bool isEditable = NULL != mImpl->mEventData;
1470 // Keep the current offset and alignment as it will be used to update the decorator's positions (if the size changes).
1472 if( newSize && isEditable )
1474 offset = mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition;
1477 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
1478 CalculateTextAlignment( size );
1484 // If there is a new size, the scroll position needs to be clamped.
1485 mImpl->ClampHorizontalScroll( layoutSize );
1487 // Update the decorator's positions is needed if there is a new size.
1488 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
1491 // Move the cursor, grab handle etc.
1492 updated = mImpl->ProcessInputEvents() || updated;
1495 // Clear the update info. This info will be set the next time the text is updated.
1496 mImpl->mTextUpdateInfo.Clear();
1497 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1502 void Controller::ProcessModifyEvents()
1504 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
1506 if( 0u == events.Count() )
1512 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
1513 endIt = events.End();
1517 const ModifyEvent& event = *it;
1519 if( ModifyEvent::TEXT_REPLACED == event.type )
1521 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
1522 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
1524 TextReplacedEvent();
1526 else if( ModifyEvent::TEXT_INSERTED == event.type )
1528 TextInsertedEvent();
1530 else if( ModifyEvent::TEXT_DELETED == event.type )
1532 // Placeholder-text cannot be deleted
1533 if( !mImpl->IsShowingPlaceholderText() )
1540 if( NULL != mImpl->mEventData )
1542 // When the text is being modified, delay cursor blinking
1543 mImpl->mEventData->mDecorator->DelayCursorBlink();
1546 // Discard temporary text
1550 void Controller::ResetText()
1553 mImpl->mLogicalModel->mText.Clear();
1555 // We have cleared everything including the placeholder-text
1556 mImpl->PlaceholderCleared();
1558 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1559 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1560 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
1562 // Clear any previous text.
1563 mImpl->mTextUpdateInfo.mClearAll = true;
1565 // The natural size needs to be re-calculated.
1566 mImpl->mRecalculateNaturalSize = true;
1568 // Apply modifications to the model
1569 mImpl->mOperationsPending = ALL_OPERATIONS;
1572 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
1574 // Reset the cursor position
1575 if( NULL != mImpl->mEventData )
1577 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
1579 // Update the cursor if it's in editing mode.
1580 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1582 mImpl->mEventData->mUpdateCursorPosition = true;
1587 void Controller::ResetScrollPosition()
1589 if( NULL != mImpl->mEventData )
1591 // Reset the scroll position.
1592 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
1593 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1597 void Controller::TextReplacedEvent()
1599 // The natural size needs to be re-calculated.
1600 mImpl->mRecalculateNaturalSize = true;
1602 // Apply modifications to the model
1603 mImpl->mOperationsPending = ALL_OPERATIONS;
1606 void Controller::TextInsertedEvent()
1608 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
1610 if( NULL == mImpl->mEventData )
1615 // The natural size needs to be re-calculated.
1616 mImpl->mRecalculateNaturalSize = true;
1618 // Apply modifications to the model; TODO - Optimize this
1619 mImpl->mOperationsPending = ALL_OPERATIONS;
1622 void Controller::TextDeletedEvent()
1624 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
1626 if( NULL == mImpl->mEventData )
1631 // The natural size needs to be re-calculated.
1632 mImpl->mRecalculateNaturalSize = true;
1634 // Apply modifications to the model; TODO - Optimize this
1635 mImpl->mOperationsPending = ALL_OPERATIONS;
1638 bool Controller::DoRelayout( const Size& size,
1639 OperationsMask operationsRequired,
1642 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
1643 bool viewUpdated( false );
1645 // Calculate the operations to be done.
1646 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
1648 const CharacterIndex startIndex = mImpl->mTextUpdateInfo.mParagraphCharacterIndex;
1649 const Length requestedNumberOfCharacters = mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters;
1651 if( NO_OPERATION != ( LAYOUT & operations ) )
1653 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
1655 // Some vectors with data needed to layout and reorder may be void
1656 // after the first time the text has been laid out.
1657 // Fill the vectors again.
1659 // Calculate the number of glyphs to layout.
1660 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1661 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1662 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
1663 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
1665 const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
1666 const GlyphIndex startGlyphIndex = mImpl->mTextUpdateInfo.mStartGlyphIndex;
1667 const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
1668 const Length totalNumberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
1670 if( 0u == totalNumberOfGlyphs )
1672 if( NO_OPERATION != ( UPDATE_ACTUAL_SIZE & operations ) )
1674 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
1677 // Nothing else to do if there is no glyphs.
1678 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1682 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1683 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1684 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1685 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1686 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1687 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1688 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1690 // Set the layout parameters.
1691 LayoutParameters layoutParameters( size,
1693 lineBreakInfo.Begin(),
1694 wordBreakInfo.Begin(),
1695 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1697 glyphsToCharactersMap.Begin(),
1698 charactersPerGlyph.Begin(),
1699 charactersToGlyphBuffer,
1700 glyphsPerCharacterBuffer,
1701 totalNumberOfGlyphs );
1703 // Resize the vector of positions to have the same size than the vector of glyphs.
1704 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1705 glyphPositions.Resize( totalNumberOfGlyphs );
1707 // Whether the last character is a new paragraph character.
1708 mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1709 layoutParameters.isLastNewParagraph = mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph;
1711 // The initial glyph and the number of glyphs to layout.
1712 layoutParameters.startGlyphIndex = startGlyphIndex;
1713 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1714 layoutParameters.startLineIndex = mImpl->mTextUpdateInfo.mStartLineIndex;
1715 layoutParameters.estimatedNumberOfLines = mImpl->mTextUpdateInfo.mEstimatedNumberOfLines;
1717 // Update the visual model.
1718 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1720 mImpl->mVisualModel->mLines,
1726 if ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) )
1728 mImpl->mAutoScrollDirectionRTL = false;
1731 // Reorder the lines
1732 if( NO_OPERATION != ( REORDER & operations ) )
1734 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1735 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1737 // Check first if there are paragraphs with bidirectional info.
1738 if( 0u != bidirectionalInfo.Count() )
1741 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1743 // Reorder the lines.
1744 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1745 ReorderLines( bidirectionalInfo,
1747 requestedNumberOfCharacters,
1748 mImpl->mVisualModel->mLines,
1749 bidirectionalLineInfo );
1751 // Set the bidirectional info per line into the layout parameters.
1752 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1753 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1755 // Re-layout the text. Reorder those lines with right to left characters.
1756 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1758 requestedNumberOfCharacters,
1761 if ( ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) ) && ( numberOfLines > 0 ) )
1763 const LineRun* const firstline = mImpl->mVisualModel->mLines.Begin();
1766 mImpl->mAutoScrollDirectionRTL = firstline->direction;
1772 // Sets the actual size.
1773 if( NO_OPERATION != ( UPDATE_ACTUAL_SIZE & operations ) )
1775 mImpl->mVisualModel->SetLayoutSize( layoutSize );
1779 // Store the size used to layout the text.
1780 mImpl->mVisualModel->mControlSize = size;
1784 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1787 if( NO_OPERATION != ( ALIGN & operations ) )
1789 // The laid-out lines.
1790 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1792 mImpl->mLayoutEngine.Align( size,
1794 requestedNumberOfCharacters,
1799 #if defined(DEBUG_ENABLED)
1800 std::string currentText;
1801 GetText( currentText );
1802 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mAutoScrollDirectionRTL[%s] [%s]\n", this, (mImpl->mAutoScrollDirectionRTL)?"true":"false", currentText.c_str() );
1804 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1808 void Controller::SetMultiLineEnabled( bool enable )
1810 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1812 if( layout != mImpl->mLayoutEngine.GetLayout() )
1814 // Set the layout type.
1815 mImpl->mLayoutEngine.SetLayout( layout );
1817 // Set the flags to redo the layout operations
1818 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1819 UPDATE_ACTUAL_SIZE |
1823 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1825 mImpl->RequestRelayout();
1829 bool Controller::IsMultiLineEnabled() const
1831 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1834 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1836 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1838 // Set the alignment.
1839 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1841 // Set the flag to redo the alignment operation.
1842 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1844 mImpl->RequestRelayout();
1848 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1850 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1853 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1855 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1857 // Set the alignment.
1858 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1860 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1862 mImpl->RequestRelayout();
1866 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1868 return mImpl->mLayoutEngine.GetVerticalAlignment();
1871 void Controller::CalculateTextAlignment( const Size& controlSize )
1873 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1875 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1877 // Get the line height of the default font.
1878 layoutSize.height = mImpl->GetDefaultFontLineHeight();
1881 if( LayoutEngine::SINGLE_LINE_BOX == mImpl->mLayoutEngine.GetLayout() )
1883 // Get the direction of the first character.
1884 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1886 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1887 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1888 if( firstParagraphDirection )
1890 switch( horizontalAlignment )
1892 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1894 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1897 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1902 case LayoutEngine::HORIZONTAL_ALIGN_END:
1904 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1910 switch( horizontalAlignment )
1912 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1914 mImpl->mAlignmentOffset.x = 0.f;
1917 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1919 mImpl->mAlignmentOffset.x = floorf( 0.5f * ( controlSize.width - layoutSize.width ) ); // try to avoid pixel alignment.
1922 case LayoutEngine::HORIZONTAL_ALIGN_END:
1924 mImpl->mAlignmentOffset.x = controlSize.width - layoutSize.width;
1930 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1931 switch( verticalAlignment )
1933 case LayoutEngine::VERTICAL_ALIGN_TOP:
1935 mImpl->mAlignmentOffset.y = 0.f;
1938 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1940 mImpl->mAlignmentOffset.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
1943 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1945 mImpl->mAlignmentOffset.y = controlSize.height - layoutSize.height;
1951 LayoutEngine& Controller::GetLayoutEngine()
1953 return mImpl->mLayoutEngine;
1956 View& Controller::GetView()
1958 return mImpl->mView;
1961 void Controller::KeyboardFocusGainEvent()
1963 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1965 if( NULL != mImpl->mEventData )
1967 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1968 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1970 mImpl->ChangeState( EventData::EDITING );
1971 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1974 if( mImpl->IsShowingPlaceholderText() )
1976 // Show alternative placeholder-text when editing
1977 ShowPlaceholderText();
1980 mImpl->RequestRelayout();
1984 void Controller::KeyboardFocusLostEvent()
1986 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1988 if( NULL != mImpl->mEventData )
1990 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1992 mImpl->ChangeState( EventData::INACTIVE );
1994 if( !mImpl->IsShowingRealText() )
1996 // Revert to regular placeholder-text when not editing
1997 ShowPlaceholderText();
2001 mImpl->RequestRelayout();
2004 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
2006 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
2008 bool textChanged( false );
2010 if( ( NULL != mImpl->mEventData ) &&
2011 ( keyEvent.state == KeyEvent::Down ) )
2013 int keyCode = keyEvent.keyCode;
2014 const std::string& keyString = keyEvent.keyPressed;
2016 // Pre-process to separate modifying events from non-modifying input events.
2017 if( Dali::DALI_KEY_ESCAPE == keyCode )
2019 // Escape key is a special case which causes focus loss
2020 KeyboardFocusLostEvent();
2022 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
2023 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
2024 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
2025 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
2027 Event event( Event::CURSOR_KEY_EVENT );
2028 event.p1.mInt = keyCode;
2029 mImpl->mEventData->mEventQueue.push_back( event );
2031 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
2033 textChanged = BackspaceKeyEvent();
2035 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
2037 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
2038 // Avoids calling the InsertText() method which can delete selected text
2040 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
2041 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
2043 mImpl->ChangeState( EventData::INACTIVE );
2044 // Menu/Home key behaviour does not allow edit mode to resume like Power key
2045 // Avoids calling the InsertText() method which can delete selected text
2047 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
2049 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
2050 // and a character is typed after the type of a upper case latin character.
2056 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
2058 // IMF manager is no longer handling key-events
2059 mImpl->ClearPreEditFlag();
2061 InsertText( keyString, COMMIT );
2065 if( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
2066 ( mImpl->mEventData->mState != EventData::INACTIVE ) )
2068 mImpl->ChangeState( EventData::EDITING );
2071 mImpl->RequestRelayout();
2076 // Do this last since it provides callbacks into application code
2077 mImpl->mControlInterface.TextChanged();
2083 void Controller::InsertText( const std::string& text, Controller::InsertType type )
2085 bool removedPrevious( false );
2086 bool maxLengthReached( false );
2088 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
2090 if( NULL == mImpl->mEventData )
2095 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
2096 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
2097 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2099 // TODO: At the moment the underline runs are only for pre-edit.
2100 mImpl->mVisualModel->mUnderlineRuns.Clear();
2102 // Keep the current number of characters.
2103 const Length currentNumberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2105 // Remove the previous IMF pre-edit.
2106 if( mImpl->mEventData->mPreEditFlag && ( 0u != mImpl->mEventData->mPreEditLength ) )
2108 removedPrevious = RemoveText( -static_cast<int>( mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition ),
2109 mImpl->mEventData->mPreEditLength,
2110 DONT_UPDATE_INPUT_STYLE );
2112 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
2113 mImpl->mEventData->mPreEditLength = 0u;
2117 // Remove the previous Selection.
2118 removedPrevious = RemoveSelectedText();
2121 Vector<Character> utf32Characters;
2122 Length characterCount = 0u;
2126 // Convert text into UTF-32
2127 utf32Characters.Resize( text.size() );
2129 // This is a bit horrible but std::string returns a (signed) char*
2130 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
2132 // Transform a text array encoded in utf8 into an array encoded in utf32.
2133 // It returns the actual number of characters.
2134 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
2135 utf32Characters.Resize( characterCount );
2137 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
2138 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
2141 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
2143 // The placeholder text is no longer needed
2144 if( mImpl->IsShowingPlaceholderText() )
2149 mImpl->ChangeState( EventData::EDITING );
2151 // Handle the IMF (predicitive text) state changes
2152 if( COMMIT == type )
2154 // IMF manager is no longer handling key-events
2155 mImpl->ClearPreEditFlag();
2159 if( !mImpl->mEventData->mPreEditFlag )
2161 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
2163 // Record the start of the pre-edit text
2164 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
2167 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
2168 mImpl->mEventData->mPreEditFlag = true;
2170 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2173 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
2175 // Restrict new text to fit within Maximum characters setting.
2176 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
2177 maxLengthReached = ( characterCount > maxSizeOfNewText );
2179 // The cursor position.
2180 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
2182 // Update the text's style.
2184 // Updates the text style runs by adding characters.
2185 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
2187 // Get the character index from the cursor index.
2188 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
2190 // Retrieve the text's style for the given index.
2192 mImpl->RetrieveDefaultInputStyle( style );
2193 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
2195 // Whether to add a new text color run.
2196 const bool addColorRun = ( style.textColor != mImpl->mEventData->mInputStyle.textColor );
2198 // Whether to add a new font run.
2199 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
2200 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
2201 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
2202 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
2203 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
2208 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
2209 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
2211 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
2212 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
2213 colorRun.characterRun.characterIndex = cursorIndex;
2214 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2217 if( addFontNameRun ||
2223 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
2224 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
2226 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
2228 if( addFontNameRun )
2230 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
2231 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
2232 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
2233 fontDescriptionRun.familyDefined = true;
2235 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
2238 if( addFontWeightRun )
2240 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
2241 fontDescriptionRun.weightDefined = true;
2244 if( addFontWidthRun )
2246 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
2247 fontDescriptionRun.widthDefined = true;
2250 if( addFontSlantRun )
2252 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
2253 fontDescriptionRun.slantDefined = true;
2256 if( addFontSizeRun )
2258 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
2259 fontDescriptionRun.sizeDefined = true;
2262 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
2263 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2266 // Insert at current cursor position.
2267 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
2269 if( cursorIndex < numberOfCharactersInModel )
2271 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2275 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2278 // Mark the first paragraph to be updated.
2279 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
2280 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
2282 // Update the cursor index.
2283 cursorIndex += maxSizeOfNewText;
2285 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
2288 const Length numberOfCharacters = mImpl->IsShowingRealText() ? mImpl->mLogicalModel->mText.Count() : 0u;
2290 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
2291 mImpl->IsPlaceholderAvailable() )
2293 // Show place-holder if empty after removing the pre-edit text
2294 ShowPlaceholderText();
2295 mImpl->mEventData->mUpdateCursorPosition = true;
2296 mImpl->ClearPreEditFlag();
2298 else if( removedPrevious ||
2299 ( 0 != utf32Characters.Count() ) )
2301 // Queue an inserted event
2302 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
2304 mImpl->mEventData->mUpdateCursorPosition = true;
2305 if( numberOfCharacters < currentNumberOfCharacters )
2307 mImpl->mEventData->mScrollAfterDelete = true;
2311 mImpl->mEventData->mScrollAfterUpdatePosition = true;
2315 if( maxLengthReached )
2317 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
2319 mImpl->ResetImfManager();
2321 // Do this last since it provides callbacks into application code
2322 mImpl->mControlInterface.MaxLengthReached();
2326 bool Controller::RemoveSelectedText()
2328 bool textRemoved( false );
2330 if( EventData::SELECTING == mImpl->mEventData->mState )
2332 std::string removedString;
2333 mImpl->RetrieveSelection( removedString, true );
2335 if( !removedString.empty() )
2338 mImpl->ChangeState( EventData::EDITING );
2345 void Controller::TapEvent( unsigned int tapCount, float x, float y )
2347 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
2349 if( NULL != mImpl->mEventData )
2351 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
2353 if( 1u == tapCount )
2355 // This is to avoid unnecessary relayouts when tapping an empty text-field
2356 bool relayoutNeeded( false );
2358 if( ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
2359 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
2361 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE ); // If Popup shown hide it here so can be shown again if required.
2364 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
2366 // Already in an active state so show a popup
2367 if( !mImpl->IsClipboardEmpty() )
2369 // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
2370 mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
2374 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2376 relayoutNeeded = true;
2380 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2382 // Hide placeholder text
2386 if( EventData::INACTIVE == mImpl->mEventData->mState )
2388 mImpl->ChangeState( EventData::EDITING );
2390 else if( !mImpl->IsClipboardEmpty() )
2392 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2394 relayoutNeeded = true;
2397 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2398 if( relayoutNeeded )
2400 Event event( Event::TAP_EVENT );
2401 event.p1.mUint = tapCount;
2402 event.p2.mFloat = x;
2403 event.p3.mFloat = y;
2404 mImpl->mEventData->mEventQueue.push_back( event );
2406 mImpl->RequestRelayout();
2409 else if( 2u == tapCount )
2411 if( mImpl->mEventData->mSelectionEnabled &&
2412 mImpl->IsShowingRealText() )
2414 SelectEvent( x, y, false );
2419 // Reset keyboard as tap event has occurred.
2420 mImpl->ResetImfManager();
2423 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2424 // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2426 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2428 if( NULL != mImpl->mEventData )
2430 Event event( Event::PAN_EVENT );
2431 event.p1.mInt = state;
2432 event.p2.mFloat = displacement.x;
2433 event.p3.mFloat = displacement.y;
2434 mImpl->mEventData->mEventQueue.push_back( event );
2436 mImpl->RequestRelayout();
2440 void Controller::LongPressEvent( Gesture::State state, float x, float y )
2442 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2444 if( ( state == Gesture::Started ) &&
2445 ( NULL != mImpl->mEventData ) )
2447 if( !mImpl->IsShowingRealText() )
2449 Event event( Event::LONG_PRESS_EVENT );
2450 event.p1.mInt = state;
2451 mImpl->mEventData->mEventQueue.push_back( event );
2452 mImpl->RequestRelayout();
2456 // The 1st long-press on inactive text-field is treated as tap
2457 if( EventData::INACTIVE == mImpl->mEventData->mState )
2459 mImpl->ChangeState( EventData::EDITING );
2461 Event event( Event::TAP_EVENT );
2463 event.p2.mFloat = x;
2464 event.p3.mFloat = y;
2465 mImpl->mEventData->mEventQueue.push_back( event );
2467 mImpl->RequestRelayout();
2471 // Reset the imf manger to commit the pre-edit before selecting the text.
2472 mImpl->ResetImfManager();
2474 SelectEvent( x, y, false );
2480 void Controller::SelectEvent( float x, float y, bool selectAll )
2482 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2484 if( NULL != mImpl->mEventData )
2486 mImpl->ChangeState( EventData::SELECTING );
2490 Event event( Event::SELECT_ALL );
2491 mImpl->mEventData->mEventQueue.push_back( event );
2495 Event event( Event::SELECT );
2496 event.p2.mFloat = x;
2497 event.p3.mFloat = y;
2498 mImpl->mEventData->mEventQueue.push_back( event );
2501 mImpl->RequestRelayout();
2505 void Controller::GetTargetSize( Vector2& targetSize )
2507 targetSize = mImpl->mVisualModel->mControlSize;
2510 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2512 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2515 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2517 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2519 if( NULL != mImpl->mEventData )
2521 switch( handleType )
2525 Event event( Event::GRAB_HANDLE_EVENT );
2526 event.p1.mUint = state;
2527 event.p2.mFloat = x;
2528 event.p3.mFloat = y;
2530 mImpl->mEventData->mEventQueue.push_back( event );
2533 case LEFT_SELECTION_HANDLE:
2535 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2536 event.p1.mUint = state;
2537 event.p2.mFloat = x;
2538 event.p3.mFloat = y;
2540 mImpl->mEventData->mEventQueue.push_back( event );
2543 case RIGHT_SELECTION_HANDLE:
2545 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2546 event.p1.mUint = state;
2547 event.p2.mFloat = x;
2548 event.p3.mFloat = y;
2550 mImpl->mEventData->mEventQueue.push_back( event );
2553 case LEFT_SELECTION_HANDLE_MARKER:
2554 case RIGHT_SELECTION_HANDLE_MARKER:
2556 // Markers do not move the handles.
2559 case HANDLE_TYPE_COUNT:
2561 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2565 mImpl->RequestRelayout();
2569 void Controller::PasteText( const std::string& stringToPaste )
2571 InsertText( stringToPaste, Text::Controller::COMMIT );
2572 mImpl->ChangeState( EventData::EDITING );
2573 mImpl->RequestRelayout();
2575 // Do this last since it provides callbacks into application code
2576 mImpl->mControlInterface.TextChanged();
2579 void Controller::PasteClipboardItemEvent()
2581 // Retrieve the clipboard contents first
2582 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2583 std::string stringToPaste( notifier.GetContent() );
2585 // Commit the current pre-edit text; the contents of the clipboard should be appended
2586 mImpl->ResetImfManager();
2588 // Temporary disable hiding clipboard
2589 mImpl->SetClipboardHideEnable( false );
2592 PasteText( stringToPaste );
2594 mImpl->SetClipboardHideEnable( true );
2597 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2599 if( NULL == mImpl->mEventData )
2606 case Toolkit::TextSelectionPopup::CUT:
2608 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2609 mImpl->mOperationsPending = ALL_OPERATIONS;
2611 // This is to reset the virtual keyboard to Upper-case
2612 if( 0u == mImpl->mLogicalModel->mText.Count() )
2617 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2618 !mImpl->IsPlaceholderAvailable() )
2620 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2624 ShowPlaceholderText();
2627 mImpl->mEventData->mUpdateCursorPosition = true;
2628 mImpl->mEventData->mScrollAfterDelete = true;
2630 mImpl->RequestRelayout();
2631 mImpl->mControlInterface.TextChanged();
2634 case Toolkit::TextSelectionPopup::COPY:
2636 mImpl->SendSelectionToClipboard( false ); // Text not modified
2637 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
2640 case Toolkit::TextSelectionPopup::PASTE:
2642 std::string stringToPaste("");
2643 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2644 PasteText( stringToPaste );
2647 case Toolkit::TextSelectionPopup::SELECT:
2649 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2651 if( mImpl->mEventData->mSelectionEnabled )
2653 // Creates a SELECT event.
2654 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2658 case Toolkit::TextSelectionPopup::SELECT_ALL:
2660 // Creates a SELECT_ALL event
2661 SelectEvent( 0.f, 0.f, true );
2664 case Toolkit::TextSelectionPopup::CLIPBOARD:
2666 mImpl->ShowClipboard();
2669 case Toolkit::TextSelectionPopup::NONE:
2677 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2679 bool update = false;
2680 bool requestRelayout = false;
2683 unsigned int cursorPosition = 0u;
2685 switch( imfEvent.eventName )
2687 case ImfManager::COMMIT:
2689 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2691 requestRelayout = true;
2694 case ImfManager::PREEDIT:
2696 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2698 requestRelayout = true;
2701 case ImfManager::DELETESURROUNDING:
2703 update = RemoveText( imfEvent.cursorOffset,
2704 imfEvent.numberOfChars,
2705 DONT_UPDATE_INPUT_STYLE );
2709 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2710 !mImpl->IsPlaceholderAvailable() )
2712 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2716 ShowPlaceholderText();
2718 mImpl->mEventData->mUpdateCursorPosition = true;
2719 mImpl->mEventData->mScrollAfterDelete = true;
2721 requestRelayout = true;
2724 case ImfManager::GETSURROUNDING:
2727 cursorPosition = GetLogicalCursorPosition();
2729 imfManager.SetSurroundingText( text );
2730 imfManager.SetCursorPosition( cursorPosition );
2733 case ImfManager::VOID:
2740 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
2743 cursorPosition = GetLogicalCursorPosition();
2746 if( requestRelayout )
2748 mImpl->mOperationsPending = ALL_OPERATIONS;
2749 mImpl->RequestRelayout();
2751 // Do this last since it provides callbacks into application code
2752 mImpl->mControlInterface.TextChanged();
2755 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
2757 return callbackData;
2760 Controller::~Controller()
2765 bool Controller::BackspaceKeyEvent()
2767 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2769 bool removed = false;
2771 if( NULL == mImpl->mEventData )
2776 // IMF manager is no longer handling key-events
2777 mImpl->ClearPreEditFlag();
2779 if( EventData::SELECTING == mImpl->mEventData->mState )
2781 removed = RemoveSelectedText();
2783 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2785 // Remove the character before the current cursor position
2786 removed = RemoveText( -1,
2788 UPDATE_INPUT_STYLE );
2793 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE RemovedText\n", this );
2794 // Notifiy the IMF manager after text changed
2795 // Automatic Upper-case and restarting prediction on an existing word require this.
2798 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2799 !mImpl->IsPlaceholderAvailable() )
2801 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2805 ShowPlaceholderText();
2807 mImpl->mEventData->mUpdateCursorPosition = true;
2808 mImpl->mEventData->mScrollAfterDelete = true;
2814 void Controller::NotifyImfManager()
2816 if( NULL != mImpl->mEventData )
2818 if( mImpl->mEventData->mImfManager )
2820 // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
2823 mImpl->mEventData->mImfManager.SetSurroundingText( text );
2825 mImpl->mEventData->mImfManager.SetCursorPosition( GetLogicalCursorPosition() );
2826 mImpl->mEventData->mImfManager.NotifyCursorPosition();
2831 void Controller::ShowPlaceholderText()
2833 if( mImpl->IsPlaceholderAvailable() )
2835 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2837 if( NULL == mImpl->mEventData )
2842 mImpl->mEventData->mIsShowingPlaceholderText = true;
2844 // Disable handles when showing place-holder text
2845 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2846 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2847 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2849 const char* text( NULL );
2852 // TODO - Switch placeholder text styles when changing state
2853 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2854 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2856 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2857 size = mImpl->mEventData->mPlaceholderTextActive.size();
2861 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2862 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2865 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2866 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2868 // Reset model for showing placeholder.
2869 mImpl->mLogicalModel->mText.Clear();
2870 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2872 // Convert text into UTF-32
2873 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2874 utf32Characters.Resize( size );
2876 // This is a bit horrible but std::string returns a (signed) char*
2877 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2879 // Transform a text array encoded in utf8 into an array encoded in utf32.
2880 // It returns the actual number of characters.
2881 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2882 utf32Characters.Resize( characterCount );
2884 // The characters to be added.
2885 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = characterCount;
2887 // Reset the cursor position
2888 mImpl->mEventData->mPrimaryCursorPosition = 0;
2890 // The natural size needs to be re-calculated.
2891 mImpl->mRecalculateNaturalSize = true;
2893 // Apply modifications to the model
2894 mImpl->mOperationsPending = ALL_OPERATIONS;
2896 // Update the rest of the model during size negotiation
2897 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2901 void Controller::ClearFontData()
2903 if( mImpl->mFontDefaults )
2905 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2908 // Set flags to update the model.
2909 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2910 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2911 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
2913 mImpl->mTextUpdateInfo.mClearAll = true;
2914 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
2915 mImpl->mRecalculateNaturalSize = true;
2917 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2922 UPDATE_ACTUAL_SIZE |
2927 void Controller::ClearStyleData()
2929 mImpl->mLogicalModel->mColorRuns.Clear();
2930 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2933 Controller::Controller( ControlInterface& controlInterface )
2936 mImpl = new Controller::Impl( controlInterface );
2941 } // namespace Toolkit