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 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfCharacters );
625 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
627 if( NULL != mImpl->mEventData )
629 mImpl->mEventData->mPlaceholderTextColor = textColor;
632 if( mImpl->IsShowingPlaceholderText() )
634 mImpl->mVisualModel->SetTextColor( textColor );
635 mImpl->RequestRelayout();
639 const Vector4& Controller::GetPlaceholderTextColor() const
641 if( NULL != mImpl->mEventData )
643 return mImpl->mEventData->mPlaceholderTextColor;
649 void Controller::SetShadowOffset( const Vector2& shadowOffset )
651 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
653 mImpl->RequestRelayout();
656 const Vector2& Controller::GetShadowOffset() const
658 return mImpl->mVisualModel->GetShadowOffset();
661 void Controller::SetShadowColor( const Vector4& shadowColor )
663 mImpl->mVisualModel->SetShadowColor( shadowColor );
665 mImpl->RequestRelayout();
668 const Vector4& Controller::GetShadowColor() const
670 return mImpl->mVisualModel->GetShadowColor();
673 void Controller::SetUnderlineColor( const Vector4& color )
675 mImpl->mVisualModel->SetUnderlineColor( color );
677 mImpl->RequestRelayout();
680 const Vector4& Controller::GetUnderlineColor() const
682 return mImpl->mVisualModel->GetUnderlineColor();
685 void Controller::SetUnderlineEnabled( bool enabled )
687 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
689 mImpl->RequestRelayout();
692 bool Controller::IsUnderlineEnabled() const
694 return mImpl->mVisualModel->IsUnderlineEnabled();
697 void Controller::SetUnderlineHeight( float height )
699 mImpl->mVisualModel->SetUnderlineHeight( height );
701 mImpl->RequestRelayout();
704 float Controller::GetUnderlineHeight() const
706 return mImpl->mVisualModel->GetUnderlineHeight();
709 void Controller::SetInputColor( const Vector4& color )
711 if( NULL != mImpl->mEventData )
713 mImpl->mEventData->mInputStyle.textColor = color;
714 mImpl->mEventData->mInputStyle.isDefaultColor = false;
716 if( EventData::SELECTING == mImpl->mEventData->mState )
718 const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
720 // Get start and end position of selection
721 const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
722 const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
724 // Add the color run.
725 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
726 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
728 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
729 colorRun.color = color;
730 colorRun.characterRun.characterIndex = startOfSelectedText;
731 colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
733 // Request to relayout.
734 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
735 mImpl->RequestRelayout();
737 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
738 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
739 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
744 const Vector4& Controller::GetInputColor() const
746 if( NULL != mImpl->mEventData )
748 return mImpl->mEventData->mInputStyle.textColor;
751 // Return the default text's color if there is no EventData.
752 return mImpl->mTextColor;
756 void Controller::SetInputFontFamily( const std::string& fontFamily )
758 if( NULL != mImpl->mEventData )
760 mImpl->mEventData->mInputStyle.familyName = fontFamily;
761 mImpl->mEventData->mInputStyle.familyDefined = true;
763 if( EventData::SELECTING == mImpl->mEventData->mState )
765 CharacterIndex startOfSelectedText = 0u;
766 Length lengthOfSelectedText = 0u;
767 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
768 mImpl->mLogicalModel,
770 lengthOfSelectedText );
772 fontDescriptionRun.familyLength = fontFamily.size();
773 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
774 memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
775 fontDescriptionRun.familyDefined = true;
777 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
779 // Request to relayout.
780 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
788 mImpl->mRecalculateNaturalSize = true;
789 mImpl->RequestRelayout();
791 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
792 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
793 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
795 // As the font changes, recalculate the handle positions is needed.
796 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
797 mImpl->mEventData->mUpdateRightSelectionPosition = true;
798 mImpl->mEventData->mScrollAfterUpdatePosition = true;
803 const std::string& Controller::GetInputFontFamily() const
805 if( NULL != mImpl->mEventData )
807 return mImpl->mEventData->mInputStyle.familyName;
810 // Return the default font's family if there is no EventData.
811 return GetDefaultFontFamily();
814 void Controller::SetInputFontStyle( const std::string& fontStyle )
816 if( NULL != mImpl->mEventData )
818 mImpl->mEventData->mInputStyle.fontStyle = fontStyle;
822 const std::string& Controller::GetInputFontStyle() const
824 if( NULL != mImpl->mEventData )
826 return mImpl->mEventData->mInputStyle.fontStyle;
829 // Return the default font's style if there is no EventData.
830 return GetDefaultFontStyle();
833 void Controller::SetInputFontWeight( FontWeight weight )
835 if( NULL != mImpl->mEventData )
837 mImpl->mEventData->mInputStyle.weight = weight;
838 mImpl->mEventData->mInputStyle.weightDefined = true;
840 if( EventData::SELECTING == mImpl->mEventData->mState )
842 CharacterIndex startOfSelectedText = 0u;
843 Length lengthOfSelectedText = 0u;
844 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
845 mImpl->mLogicalModel,
847 lengthOfSelectedText );
849 fontDescriptionRun.weight = weight;
850 fontDescriptionRun.weightDefined = true;
852 // Request to relayout.
853 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
861 mImpl->mRecalculateNaturalSize = true;
862 mImpl->RequestRelayout();
864 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
865 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
866 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
868 // As the font might change, recalculate the handle positions is needed.
869 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
870 mImpl->mEventData->mUpdateRightSelectionPosition = true;
871 mImpl->mEventData->mScrollAfterUpdatePosition = true;
876 FontWeight Controller::GetInputFontWeight() const
878 if( NULL != mImpl->mEventData )
880 return mImpl->mEventData->mInputStyle.weight;
883 return GetDefaultFontWeight();
886 void Controller::SetInputFontWidth( FontWidth width )
888 if( NULL != mImpl->mEventData )
890 mImpl->mEventData->mInputStyle.width = width;
891 mImpl->mEventData->mInputStyle.widthDefined = true;
893 if( EventData::SELECTING == mImpl->mEventData->mState )
895 CharacterIndex startOfSelectedText = 0u;
896 Length lengthOfSelectedText = 0u;
897 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
898 mImpl->mLogicalModel,
900 lengthOfSelectedText );
902 fontDescriptionRun.width = width;
903 fontDescriptionRun.widthDefined = true;
905 // Request to relayout.
906 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
914 mImpl->mRecalculateNaturalSize = true;
915 mImpl->RequestRelayout();
917 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
918 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
919 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
921 // As the font might change, recalculate the handle positions is needed.
922 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
923 mImpl->mEventData->mUpdateRightSelectionPosition = true;
924 mImpl->mEventData->mScrollAfterUpdatePosition = true;
929 FontWidth Controller::GetInputFontWidth() const
931 if( NULL != mImpl->mEventData )
933 return mImpl->mEventData->mInputStyle.width;
936 return GetDefaultFontWidth();
939 void Controller::SetInputFontSlant( FontSlant slant )
941 if( NULL != mImpl->mEventData )
943 mImpl->mEventData->mInputStyle.slant = slant;
944 mImpl->mEventData->mInputStyle.slantDefined = true;
946 if( EventData::SELECTING == mImpl->mEventData->mState )
948 CharacterIndex startOfSelectedText = 0u;
949 Length lengthOfSelectedText = 0u;
950 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
951 mImpl->mLogicalModel,
953 lengthOfSelectedText );
955 fontDescriptionRun.slant = slant;
956 fontDescriptionRun.slantDefined = true;
958 // Request to relayout.
959 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
967 mImpl->mRecalculateNaturalSize = true;
968 mImpl->RequestRelayout();
970 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
971 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
972 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
974 // As the font might change, recalculate the handle positions is needed.
975 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
976 mImpl->mEventData->mUpdateRightSelectionPosition = true;
977 mImpl->mEventData->mScrollAfterUpdatePosition = true;
982 FontSlant Controller::GetInputFontSlant() const
984 if( NULL != mImpl->mEventData )
986 return mImpl->mEventData->mInputStyle.slant;
989 return GetDefaultFontSlant();
992 void Controller::SetInputFontPointSize( float size )
994 if( NULL != mImpl->mEventData )
996 mImpl->mEventData->mInputStyle.size = size;
998 if( EventData::SELECTING == mImpl->mEventData->mState )
1000 CharacterIndex startOfSelectedText = 0u;
1001 Length lengthOfSelectedText = 0u;
1002 FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1003 mImpl->mLogicalModel,
1004 startOfSelectedText,
1005 lengthOfSelectedText );
1007 fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
1008 fontDescriptionRun.sizeDefined = true;
1010 // Request to relayout.
1011 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1016 UPDATE_ACTUAL_SIZE |
1019 mImpl->mRecalculateNaturalSize = true;
1020 mImpl->RequestRelayout();
1022 mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1023 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1024 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1026 // As the font might change, recalculate the handle positions is needed.
1027 mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1028 mImpl->mEventData->mUpdateRightSelectionPosition = true;
1029 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1034 float Controller::GetInputFontPointSize() const
1036 if( NULL != mImpl->mEventData )
1038 return mImpl->mEventData->mInputStyle.size;
1041 // Return the default font's point size if there is no EventData.
1042 return GetDefaultPointSize();
1045 void Controller::SetEnableCursorBlink( bool enable )
1047 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
1049 if( NULL != mImpl->mEventData )
1051 mImpl->mEventData->mCursorBlinkEnabled = enable;
1054 mImpl->mEventData->mDecorator )
1056 mImpl->mEventData->mDecorator->StopCursorBlink();
1061 bool Controller::GetEnableCursorBlink() const
1063 if( NULL != mImpl->mEventData )
1065 return mImpl->mEventData->mCursorBlinkEnabled;
1071 const Vector2& Controller::GetScrollPosition() const
1073 if( NULL != mImpl->mEventData )
1075 return mImpl->mEventData->mScrollPosition;
1078 return Vector2::ZERO;
1081 const Vector2& Controller::GetAlignmentOffset() const
1083 return mImpl->mAlignmentOffset;
1086 Vector3 Controller::GetNaturalSize()
1088 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
1089 Vector3 naturalSize;
1091 // Make sure the model is up-to-date before layouting
1092 ProcessModifyEvents();
1094 if( mImpl->mRecalculateNaturalSize )
1096 // Operations that can be done only once until the text changes.
1097 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1104 GET_GLYPH_METRICS );
1105 // Make sure the model is up-to-date before layouting
1106 mImpl->UpdateModel( onlyOnceOperations );
1108 // Layout the text for the new width.
1109 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1111 // Set the update info to relayout the whole text.
1112 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1113 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1115 // Store the actual control's size to restore later.
1116 const Size actualControlSize = mImpl->mVisualModel->mControlSize;
1118 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
1119 static_cast<OperationsMask>( onlyOnceOperations |
1121 naturalSize.GetVectorXY() );
1123 // Do not do again the only once operations.
1124 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1126 // Do the size related operations again.
1127 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1130 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1132 // Stores the natural size to avoid recalculate it again
1133 // unless the text/style changes.
1134 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
1136 mImpl->mRecalculateNaturalSize = false;
1138 // Clear the update info. This info will be set the next time the text is updated.
1139 mImpl->mTextUpdateInfo.Clear();
1141 // Restore the actual control's size.
1142 mImpl->mVisualModel->mControlSize = actualControlSize;
1144 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1148 naturalSize = mImpl->mVisualModel->GetNaturalSize();
1150 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
1153 naturalSize.x = ConvertToEven( naturalSize.x );
1154 naturalSize.y = ConvertToEven( naturalSize.y );
1159 float Controller::GetHeightForWidth( float width )
1161 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1162 // Make sure the model is up-to-date before layouting
1163 ProcessModifyEvents();
1166 if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
1168 // Operations that can be done only once until the text changes.
1169 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
1176 GET_GLYPH_METRICS );
1177 // Make sure the model is up-to-date before layouting
1178 mImpl->UpdateModel( onlyOnceOperations );
1181 // Layout the text for the new width.
1182 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
1184 // Set the update info to relayout the whole text.
1185 mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
1186 mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1188 // Store the actual control's width.
1189 const float actualControlWidth = mImpl->mVisualModel->mControlSize.width;
1191 DoRelayout( Size( width, MAX_FLOAT ),
1192 static_cast<OperationsMask>( onlyOnceOperations |
1196 // Do not do again the only once operations.
1197 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1199 // Do the size related operations again.
1200 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
1204 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1206 // Clear the update info. This info will be set the next time the text is updated.
1207 mImpl->mTextUpdateInfo.Clear();
1209 // Restore the actual control's width.
1210 mImpl->mVisualModel->mControlSize.width = actualControlWidth;
1212 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1216 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1217 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1220 return layoutSize.height;
1223 bool Controller::Relayout( const Size& size )
1225 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", this, size.width, size.height, (mImpl->mAutoScrollEnabled)?"true":"false" );
1227 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1229 bool glyphsRemoved( false );
1230 if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1232 mImpl->mVisualModel->mGlyphPositions.Clear();
1233 glyphsRemoved = true;
1236 // Clear the update info. This info will be set the next time the text is updated.
1237 mImpl->mTextUpdateInfo.Clear();
1239 // Not worth to relayout if width or height is equal to zero.
1240 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1242 return glyphsRemoved;
1245 // Whether a new size has been set.
1246 const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1250 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1252 // Layout operations that need to be done if the size changes.
1253 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1256 UPDATE_ACTUAL_SIZE |
1258 // Set the update info to relayout the whole text.
1259 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
1260 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1263 // Whether there are modify events.
1264 if( 0u != mImpl->mModifyEvents.Count() )
1266 // Style operations that need to be done if the text is modified.
1267 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1271 // Make sure the model is up-to-date before layouting.
1272 ProcessModifyEvents();
1273 bool updated = mImpl->UpdateModel( mImpl->mOperationsPending );
1277 updated = DoRelayout( size,
1278 mImpl->mOperationsPending,
1279 layoutSize ) || updated;
1281 // Do not re-do any operation until something changes.
1282 mImpl->mOperationsPending = NO_OPERATION;
1284 // Whether the text control is editable
1285 const bool isEditable = NULL != mImpl->mEventData;
1287 // Keep the current offset and alignment as it will be used to update the decorator's positions (if the size changes).
1289 if( newSize && isEditable )
1291 offset = mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition;
1294 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
1295 CalculateTextAlignment( size );
1301 // If there is a new size, the scroll position needs to be clamped.
1302 mImpl->ClampHorizontalScroll( layoutSize );
1304 // Update the decorator's positions is needed if there is a new size.
1305 mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
1308 // Move the cursor, grab handle etc.
1309 updated = mImpl->ProcessInputEvents() || updated;
1312 // Clear the update info. This info will be set the next time the text is updated.
1313 mImpl->mTextUpdateInfo.Clear();
1314 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1319 void Controller::ProcessModifyEvents()
1321 Vector<ModifyEvent>& events = mImpl->mModifyEvents;
1323 if( 0u == events.Count() )
1329 for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
1330 endIt = events.End();
1334 const ModifyEvent& event = *it;
1336 if( ModifyEvent::TEXT_REPLACED == event.type )
1338 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
1339 DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
1341 TextReplacedEvent();
1343 else if( ModifyEvent::TEXT_INSERTED == event.type )
1345 TextInsertedEvent();
1347 else if( ModifyEvent::TEXT_DELETED == event.type )
1349 // Placeholder-text cannot be deleted
1350 if( !mImpl->IsShowingPlaceholderText() )
1357 if( NULL != mImpl->mEventData )
1359 // When the text is being modified, delay cursor blinking
1360 mImpl->mEventData->mDecorator->DelayCursorBlink();
1363 // Discard temporary text
1367 void Controller::ResetText()
1370 mImpl->mLogicalModel->mText.Clear();
1372 // We have cleared everything including the placeholder-text
1373 mImpl->PlaceholderCleared();
1375 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
1376 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1377 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
1379 // Clear any previous text.
1380 mImpl->mTextUpdateInfo.mClearAll = true;
1382 // The natural size needs to be re-calculated.
1383 mImpl->mRecalculateNaturalSize = true;
1385 // Apply modifications to the model
1386 mImpl->mOperationsPending = ALL_OPERATIONS;
1389 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
1391 // Reset the cursor position
1392 if( NULL != mImpl->mEventData )
1394 mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
1396 // Update the cursor if it's in editing mode.
1397 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1399 mImpl->mEventData->mUpdateCursorPosition = true;
1404 void Controller::ResetScrollPosition()
1406 if( NULL != mImpl->mEventData )
1408 // Reset the scroll position.
1409 mImpl->mEventData->mScrollPosition = Vector2::ZERO;
1410 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1414 void Controller::TextReplacedEvent()
1416 // The natural size needs to be re-calculated.
1417 mImpl->mRecalculateNaturalSize = true;
1419 // Apply modifications to the model
1420 mImpl->mOperationsPending = ALL_OPERATIONS;
1423 void Controller::TextInsertedEvent()
1425 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
1427 if( NULL == mImpl->mEventData )
1432 // The natural size needs to be re-calculated.
1433 mImpl->mRecalculateNaturalSize = true;
1435 // Apply modifications to the model; TODO - Optimize this
1436 mImpl->mOperationsPending = ALL_OPERATIONS;
1438 // Queue a cursor reposition event; this must wait until after DoRelayout()
1439 if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1441 mImpl->mEventData->mUpdateCursorPosition = true;
1442 mImpl->mEventData->mScrollAfterUpdatePosition = true;
1446 void Controller::TextDeletedEvent()
1448 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
1450 if( NULL == mImpl->mEventData )
1455 // The natural size needs to be re-calculated.
1456 mImpl->mRecalculateNaturalSize = true;
1458 // Apply modifications to the model; TODO - Optimize this
1459 mImpl->mOperationsPending = ALL_OPERATIONS;
1461 // Queue a cursor reposition event; this must wait until after DoRelayout()
1462 mImpl->mEventData->mUpdateCursorPosition = true;
1463 if( 0u != mImpl->mLogicalModel->mText.Count() )
1465 mImpl->mEventData->mScrollAfterDelete = true;
1469 bool Controller::DoRelayout( const Size& size,
1470 OperationsMask operationsRequired,
1473 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
1474 bool viewUpdated( false );
1476 // Calculate the operations to be done.
1477 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
1479 const CharacterIndex startIndex = mImpl->mTextUpdateInfo.mParagraphCharacterIndex;
1480 const Length requestedNumberOfCharacters = mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters;
1482 if( NO_OPERATION != ( LAYOUT & operations ) )
1484 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
1486 // Some vectors with data needed to layout and reorder may be void
1487 // after the first time the text has been laid out.
1488 // Fill the vectors again.
1490 // Calculate the number of glyphs to layout.
1491 const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1492 const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1493 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
1494 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
1496 const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
1497 const GlyphIndex startGlyphIndex = mImpl->mTextUpdateInfo.mStartGlyphIndex;
1498 const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
1499 const Length totalNumberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
1501 if( 0u == totalNumberOfGlyphs )
1503 if( NO_OPERATION != ( UPDATE_ACTUAL_SIZE & operations ) )
1505 mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
1508 // Nothing else to do if there is no glyphs.
1509 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1513 const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1514 const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1515 const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1516 const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1517 const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1518 const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1519 const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1521 // Set the layout parameters.
1522 LayoutParameters layoutParameters( size,
1524 lineBreakInfo.Begin(),
1525 wordBreakInfo.Begin(),
1526 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1528 glyphsToCharactersMap.Begin(),
1529 charactersPerGlyph.Begin(),
1530 charactersToGlyphBuffer,
1531 glyphsPerCharacterBuffer,
1532 totalNumberOfGlyphs );
1534 // Resize the vector of positions to have the same size than the vector of glyphs.
1535 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1536 glyphPositions.Resize( totalNumberOfGlyphs );
1538 // Whether the last character is a new paragraph character.
1539 mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1540 layoutParameters.isLastNewParagraph = mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph;
1542 // The initial glyph and the number of glyphs to layout.
1543 layoutParameters.startGlyphIndex = startGlyphIndex;
1544 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1545 layoutParameters.startLineIndex = mImpl->mTextUpdateInfo.mStartLineIndex;
1546 layoutParameters.estimatedNumberOfLines = mImpl->mTextUpdateInfo.mEstimatedNumberOfLines;
1548 // Update the visual model.
1549 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1551 mImpl->mVisualModel->mLines,
1557 if ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) )
1559 mImpl->mAutoScrollDirectionRTL = false;
1562 // Reorder the lines
1563 if( NO_OPERATION != ( REORDER & operations ) )
1565 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1566 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1568 // Check first if there are paragraphs with bidirectional info.
1569 if( 0u != bidirectionalInfo.Count() )
1572 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1574 // Reorder the lines.
1575 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1576 ReorderLines( bidirectionalInfo,
1578 requestedNumberOfCharacters,
1579 mImpl->mVisualModel->mLines,
1580 bidirectionalLineInfo );
1582 // Set the bidirectional info per line into the layout parameters.
1583 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1584 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1586 // Re-layout the text. Reorder those lines with right to left characters.
1587 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1589 requestedNumberOfCharacters,
1592 if ( ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) ) && ( numberOfLines > 0 ) )
1594 const LineRun* const firstline = mImpl->mVisualModel->mLines.Begin();
1597 mImpl->mAutoScrollDirectionRTL = firstline->direction;
1603 // Sets the actual size.
1604 if( NO_OPERATION != ( UPDATE_ACTUAL_SIZE & operations ) )
1606 mImpl->mVisualModel->SetLayoutSize( layoutSize );
1610 // Store the size used to layout the text.
1611 mImpl->mVisualModel->mControlSize = size;
1615 layoutSize = mImpl->mVisualModel->GetLayoutSize();
1618 if( NO_OPERATION != ( ALIGN & operations ) )
1620 // The laid-out lines.
1621 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1623 mImpl->mLayoutEngine.Align( size,
1625 requestedNumberOfCharacters,
1630 #if defined(DEBUG_ENABLED)
1631 std::string currentText;
1632 GetText( currentText );
1633 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mAutoScrollDirectionRTL[%s] [%s]\n", this, (mImpl->mAutoScrollDirectionRTL)?"true":"false", currentText.c_str() );
1635 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1639 void Controller::SetMultiLineEnabled( bool enable )
1641 const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1643 if( layout != mImpl->mLayoutEngine.GetLayout() )
1645 // Set the layout type.
1646 mImpl->mLayoutEngine.SetLayout( layout );
1648 // Set the flags to redo the layout operations
1649 const OperationsMask layoutOperations = static_cast<OperationsMask>( LAYOUT |
1650 UPDATE_ACTUAL_SIZE |
1654 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1656 mImpl->RequestRelayout();
1660 bool Controller::IsMultiLineEnabled() const
1662 return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1665 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1667 if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1669 // Set the alignment.
1670 mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1672 // Set the flag to redo the alignment operation.
1673 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1675 mImpl->RequestRelayout();
1679 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1681 return mImpl->mLayoutEngine.GetHorizontalAlignment();
1684 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1686 if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1688 // Set the alignment.
1689 mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1691 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1693 mImpl->RequestRelayout();
1697 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1699 return mImpl->mLayoutEngine.GetVerticalAlignment();
1702 void Controller::CalculateTextAlignment( const Size& controlSize )
1704 Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1706 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1708 // Get the line height of the default font.
1709 layoutSize.height = mImpl->GetDefaultFontLineHeight();
1712 if( LayoutEngine::SINGLE_LINE_BOX == mImpl->mLayoutEngine.GetLayout() )
1714 // Get the direction of the first character.
1715 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1717 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1718 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1719 if( firstParagraphDirection )
1721 switch( horizontalAlignment )
1723 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1725 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1728 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1733 case LayoutEngine::HORIZONTAL_ALIGN_END:
1735 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1741 switch( horizontalAlignment )
1743 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1745 mImpl->mAlignmentOffset.x = 0.f;
1748 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1750 mImpl->mAlignmentOffset.x = floorf( 0.5f * ( controlSize.width - layoutSize.width ) ); // try to avoid pixel alignment.
1753 case LayoutEngine::HORIZONTAL_ALIGN_END:
1755 mImpl->mAlignmentOffset.x = controlSize.width - layoutSize.width;
1761 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1762 switch( verticalAlignment )
1764 case LayoutEngine::VERTICAL_ALIGN_TOP:
1766 mImpl->mAlignmentOffset.y = 0.f;
1769 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1771 mImpl->mAlignmentOffset.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
1774 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1776 mImpl->mAlignmentOffset.y = controlSize.height - layoutSize.height;
1782 LayoutEngine& Controller::GetLayoutEngine()
1784 return mImpl->mLayoutEngine;
1787 View& Controller::GetView()
1789 return mImpl->mView;
1792 void Controller::KeyboardFocusGainEvent()
1794 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1796 if( NULL != mImpl->mEventData )
1798 if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1799 ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1801 mImpl->ChangeState( EventData::EDITING );
1802 mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1805 if( mImpl->IsShowingPlaceholderText() )
1807 // Show alternative placeholder-text when editing
1808 ShowPlaceholderText();
1811 mImpl->RequestRelayout();
1815 void Controller::KeyboardFocusLostEvent()
1817 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1819 if( NULL != mImpl->mEventData )
1821 if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1823 mImpl->ChangeState( EventData::INACTIVE );
1825 if( !mImpl->IsShowingRealText() )
1827 // Revert to regular placeholder-text when not editing
1828 ShowPlaceholderText();
1832 mImpl->RequestRelayout();
1835 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1837 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1839 bool textChanged( false );
1841 if( ( NULL != mImpl->mEventData ) &&
1842 ( keyEvent.state == KeyEvent::Down ) )
1844 int keyCode = keyEvent.keyCode;
1845 const std::string& keyString = keyEvent.keyPressed;
1847 // Pre-process to separate modifying events from non-modifying input events.
1848 if( Dali::DALI_KEY_ESCAPE == keyCode )
1850 // Escape key is a special case which causes focus loss
1851 KeyboardFocusLostEvent();
1853 else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) ||
1854 ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1855 ( Dali::DALI_KEY_CURSOR_UP == keyCode ) ||
1856 ( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) )
1858 Event event( Event::CURSOR_KEY_EVENT );
1859 event.p1.mInt = keyCode;
1860 mImpl->mEventData->mEventQueue.push_back( event );
1862 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1864 textChanged = BackspaceKeyEvent();
1866 else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) )
1868 mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1869 // Avoids calling the InsertText() method which can delete selected text
1871 else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1872 IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1874 mImpl->ChangeState( EventData::INACTIVE );
1875 // Menu/Home key behaviour does not allow edit mode to resume like Power key
1876 // Avoids calling the InsertText() method which can delete selected text
1878 else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1880 // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1881 // and a character is typed after the type of a upper case latin character.
1887 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1889 // IMF manager is no longer handling key-events
1890 mImpl->ClearPreEditFlag();
1892 InsertText( keyString, COMMIT );
1896 if( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1897 ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1899 mImpl->ChangeState( EventData::EDITING );
1902 mImpl->RequestRelayout();
1907 // Do this last since it provides callbacks into application code
1908 mImpl->mControlInterface.TextChanged();
1914 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1916 bool removedPrevious( false );
1917 bool maxLengthReached( false );
1919 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1921 if( NULL == mImpl->mEventData )
1926 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1927 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1928 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1930 // TODO: At the moment the underline runs are only for pre-edit.
1931 mImpl->mVisualModel->mUnderlineRuns.Clear();
1933 Vector<Character> utf32Characters;
1934 Length characterCount( 0u );
1936 // Remove the previous IMF pre-edit (predicitive text)
1937 if( mImpl->mEventData->mPreEditFlag &&
1938 ( 0u != mImpl->mEventData->mPreEditLength ) )
1940 const CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1942 removedPrevious = RemoveText( -static_cast<int>( offset ),
1943 mImpl->mEventData->mPreEditLength,
1944 DONT_UPDATE_INPUT_STYLE );
1946 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1947 mImpl->mEventData->mPreEditLength = 0u;
1951 // Remove the previous Selection
1952 removedPrevious = RemoveSelectedText();
1957 // Convert text into UTF-32
1958 utf32Characters.Resize( text.size() );
1960 // This is a bit horrible but std::string returns a (signed) char*
1961 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1963 // Transform a text array encoded in utf8 into an array encoded in utf32.
1964 // It returns the actual number of characters.
1965 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1966 utf32Characters.Resize( characterCount );
1968 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1969 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1972 if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1974 // The placeholder text is no longer needed
1975 if( mImpl->IsShowingPlaceholderText() )
1980 mImpl->ChangeState( EventData::EDITING );
1982 // Handle the IMF (predicitive text) state changes
1983 if( COMMIT == type )
1985 // IMF manager is no longer handling key-events
1986 mImpl->ClearPreEditFlag();
1990 if( !mImpl->mEventData->mPreEditFlag )
1992 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1994 // Record the start of the pre-edit text
1995 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1998 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1999 mImpl->mEventData->mPreEditFlag = true;
2001 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
2004 const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
2006 // Restrict new text to fit within Maximum characters setting.
2007 Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
2008 maxLengthReached = ( characterCount > maxSizeOfNewText );
2010 // The cursor position.
2011 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
2013 // Update the text's style.
2015 // Updates the text style runs by adding characters.
2016 mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
2018 // Get the character index from the cursor index.
2019 const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
2021 // Retrieve the text's style for the given index.
2023 mImpl->RetrieveDefaultInputStyle( style );
2024 mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
2026 // Whether to add a new text color run.
2027 const bool addColorRun = ( style.textColor != mImpl->mEventData->mInputStyle.textColor );
2029 // Whether to add a new font run.
2030 const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
2031 const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
2032 const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
2033 const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
2034 const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
2039 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
2040 mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
2042 ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
2043 colorRun.color = mImpl->mEventData->mInputStyle.textColor;
2044 colorRun.characterRun.characterIndex = cursorIndex;
2045 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2048 if( addFontNameRun ||
2054 const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
2055 mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
2057 FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
2059 if( addFontNameRun )
2061 fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
2062 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
2063 memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
2064 fontDescriptionRun.familyDefined = true;
2066 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
2069 if( addFontWeightRun )
2071 fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
2072 fontDescriptionRun.weightDefined = true;
2075 if( addFontWidthRun )
2077 fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
2078 fontDescriptionRun.widthDefined = true;
2081 if( addFontSlantRun )
2083 fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
2084 fontDescriptionRun.slantDefined = true;
2087 if( addFontSizeRun )
2089 fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
2090 fontDescriptionRun.sizeDefined = true;
2093 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
2094 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
2097 // Insert at current cursor position.
2098 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
2100 if( cursorIndex < numberOfCharactersInModel )
2102 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2106 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
2109 // Mark the first paragraph to be updated.
2110 mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
2111 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
2113 // Update the cursor index.
2114 cursorIndex += maxSizeOfNewText;
2116 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
2119 if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
2120 mImpl->IsPlaceholderAvailable() )
2122 // Show place-holder if empty after removing the pre-edit text
2123 ShowPlaceholderText();
2124 mImpl->mEventData->mUpdateCursorPosition = true;
2125 mImpl->ClearPreEditFlag();
2127 else if( removedPrevious ||
2128 ( 0 != utf32Characters.Count() ) )
2130 // Queue an inserted event
2131 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
2134 if( maxLengthReached )
2136 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
2138 mImpl->ResetImfManager();
2140 // Do this last since it provides callbacks into application code
2141 mImpl->mControlInterface.MaxLengthReached();
2145 bool Controller::RemoveSelectedText()
2147 bool textRemoved( false );
2149 if( EventData::SELECTING == mImpl->mEventData->mState )
2151 std::string removedString;
2152 mImpl->RetrieveSelection( removedString, true );
2154 if( !removedString.empty() )
2157 mImpl->ChangeState( EventData::EDITING );
2164 void Controller::TapEvent( unsigned int tapCount, float x, float y )
2166 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
2168 if( NULL != mImpl->mEventData )
2170 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
2172 if( 1u == tapCount )
2174 // This is to avoid unnecessary relayouts when tapping an empty text-field
2175 bool relayoutNeeded( false );
2177 if( ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
2178 ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
2180 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE ); // If Popup shown hide it here so can be shown again if required.
2183 if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
2185 // Already in an active state so show a popup
2186 if( !mImpl->IsClipboardEmpty() )
2188 // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
2189 mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
2193 mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2195 relayoutNeeded = true;
2199 if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2201 // Hide placeholder text
2205 if( EventData::INACTIVE == mImpl->mEventData->mState )
2207 mImpl->ChangeState( EventData::EDITING );
2209 else if( !mImpl->IsClipboardEmpty() )
2211 mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2213 relayoutNeeded = true;
2216 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2217 if( relayoutNeeded )
2219 Event event( Event::TAP_EVENT );
2220 event.p1.mUint = tapCount;
2221 event.p2.mFloat = x;
2222 event.p3.mFloat = y;
2223 mImpl->mEventData->mEventQueue.push_back( event );
2225 mImpl->RequestRelayout();
2228 else if( 2u == tapCount )
2230 if( mImpl->mEventData->mSelectionEnabled &&
2231 mImpl->IsShowingRealText() )
2233 SelectEvent( x, y, false );
2238 // Reset keyboard as tap event has occurred.
2239 mImpl->ResetImfManager();
2242 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2243 // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2245 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2247 if( NULL != mImpl->mEventData )
2249 Event event( Event::PAN_EVENT );
2250 event.p1.mInt = state;
2251 event.p2.mFloat = displacement.x;
2252 event.p3.mFloat = displacement.y;
2253 mImpl->mEventData->mEventQueue.push_back( event );
2255 mImpl->RequestRelayout();
2259 void Controller::LongPressEvent( Gesture::State state, float x, float y )
2261 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2263 if( ( state == Gesture::Started ) &&
2264 ( NULL != mImpl->mEventData ) )
2266 if( !mImpl->IsShowingRealText() )
2268 Event event( Event::LONG_PRESS_EVENT );
2269 event.p1.mInt = state;
2270 mImpl->mEventData->mEventQueue.push_back( event );
2271 mImpl->RequestRelayout();
2275 // The 1st long-press on inactive text-field is treated as tap
2276 if( EventData::INACTIVE == mImpl->mEventData->mState )
2278 mImpl->ChangeState( EventData::EDITING );
2280 Event event( Event::TAP_EVENT );
2282 event.p2.mFloat = x;
2283 event.p3.mFloat = y;
2284 mImpl->mEventData->mEventQueue.push_back( event );
2286 mImpl->RequestRelayout();
2290 // Reset the imf manger to commit the pre-edit before selecting the text.
2291 mImpl->ResetImfManager();
2293 SelectEvent( x, y, false );
2299 void Controller::SelectEvent( float x, float y, bool selectAll )
2301 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2303 if( NULL != mImpl->mEventData )
2305 mImpl->ChangeState( EventData::SELECTING );
2309 Event event( Event::SELECT_ALL );
2310 mImpl->mEventData->mEventQueue.push_back( event );
2314 Event event( Event::SELECT );
2315 event.p2.mFloat = x;
2316 event.p3.mFloat = y;
2317 mImpl->mEventData->mEventQueue.push_back( event );
2320 mImpl->RequestRelayout();
2324 void Controller::GetTargetSize( Vector2& targetSize )
2326 targetSize = mImpl->mVisualModel->mControlSize;
2329 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2331 mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2334 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2336 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2338 if( NULL != mImpl->mEventData )
2340 switch( handleType )
2344 Event event( Event::GRAB_HANDLE_EVENT );
2345 event.p1.mUint = state;
2346 event.p2.mFloat = x;
2347 event.p3.mFloat = y;
2349 mImpl->mEventData->mEventQueue.push_back( event );
2352 case LEFT_SELECTION_HANDLE:
2354 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2355 event.p1.mUint = state;
2356 event.p2.mFloat = x;
2357 event.p3.mFloat = y;
2359 mImpl->mEventData->mEventQueue.push_back( event );
2362 case RIGHT_SELECTION_HANDLE:
2364 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2365 event.p1.mUint = state;
2366 event.p2.mFloat = x;
2367 event.p3.mFloat = y;
2369 mImpl->mEventData->mEventQueue.push_back( event );
2372 case LEFT_SELECTION_HANDLE_MARKER:
2373 case RIGHT_SELECTION_HANDLE_MARKER:
2375 // Markers do not move the handles.
2378 case HANDLE_TYPE_COUNT:
2380 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2384 mImpl->RequestRelayout();
2388 void Controller::PasteText( const std::string& stringToPaste )
2390 InsertText( stringToPaste, Text::Controller::COMMIT );
2391 mImpl->ChangeState( EventData::EDITING );
2392 mImpl->RequestRelayout();
2394 // Do this last since it provides callbacks into application code
2395 mImpl->mControlInterface.TextChanged();
2398 void Controller::PasteClipboardItemEvent()
2400 // Retrieve the clipboard contents first
2401 ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2402 std::string stringToPaste( notifier.GetContent() );
2404 // Commit the current pre-edit text; the contents of the clipboard should be appended
2405 mImpl->ResetImfManager();
2407 // Temporary disable hiding clipboard
2408 mImpl->SetClipboardHideEnable( false );
2411 PasteText( stringToPaste );
2413 mImpl->SetClipboardHideEnable( true );
2416 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2418 if( NULL == mImpl->mEventData )
2425 case Toolkit::TextSelectionPopup::CUT:
2427 mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2428 mImpl->mOperationsPending = ALL_OPERATIONS;
2430 // This is to reset the virtual keyboard to Upper-case
2431 if( 0u == mImpl->mLogicalModel->mText.Count() )
2436 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2437 !mImpl->IsPlaceholderAvailable() )
2439 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2443 ShowPlaceholderText();
2444 mImpl->mEventData->mUpdateCursorPosition = true;
2446 mImpl->RequestRelayout();
2447 mImpl->mControlInterface.TextChanged();
2450 case Toolkit::TextSelectionPopup::COPY:
2452 mImpl->SendSelectionToClipboard( false ); // Text not modified
2453 mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
2456 case Toolkit::TextSelectionPopup::PASTE:
2458 std::string stringToPaste("");
2459 mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2460 PasteText( stringToPaste );
2463 case Toolkit::TextSelectionPopup::SELECT:
2465 const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2467 if( mImpl->mEventData->mSelectionEnabled )
2469 // Creates a SELECT event.
2470 SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2474 case Toolkit::TextSelectionPopup::SELECT_ALL:
2476 // Creates a SELECT_ALL event
2477 SelectEvent( 0.f, 0.f, true );
2480 case Toolkit::TextSelectionPopup::CLIPBOARD:
2482 mImpl->ShowClipboard();
2485 case Toolkit::TextSelectionPopup::NONE:
2493 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2495 bool update = false;
2496 bool requestRelayout = false;
2499 unsigned int cursorPosition = 0u;
2501 switch( imfEvent.eventName )
2503 case ImfManager::COMMIT:
2505 InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2507 requestRelayout = true;
2510 case ImfManager::PREEDIT:
2512 InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2514 requestRelayout = true;
2517 case ImfManager::DELETESURROUNDING:
2519 update = RemoveText( imfEvent.cursorOffset,
2520 imfEvent.numberOfChars,
2521 DONT_UPDATE_INPUT_STYLE );
2525 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2526 !mImpl->IsPlaceholderAvailable() )
2528 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2532 ShowPlaceholderText();
2533 mImpl->mEventData->mUpdateCursorPosition = true;
2536 requestRelayout = true;
2539 case ImfManager::GETSURROUNDING:
2542 cursorPosition = GetLogicalCursorPosition();
2544 imfManager.SetSurroundingText( text );
2545 imfManager.SetCursorPosition( cursorPosition );
2548 case ImfManager::VOID:
2555 if( ImfManager::GETSURROUNDING != imfEvent.eventName )
2558 cursorPosition = GetLogicalCursorPosition();
2561 if( requestRelayout )
2563 mImpl->mOperationsPending = ALL_OPERATIONS;
2564 mImpl->RequestRelayout();
2566 // Do this last since it provides callbacks into application code
2567 mImpl->mControlInterface.TextChanged();
2570 ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
2572 return callbackData;
2575 Controller::~Controller()
2580 bool Controller::BackspaceKeyEvent()
2582 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2584 bool removed = false;
2586 if( NULL == mImpl->mEventData )
2591 // IMF manager is no longer handling key-events
2592 mImpl->ClearPreEditFlag();
2594 if( EventData::SELECTING == mImpl->mEventData->mState )
2596 removed = RemoveSelectedText();
2598 else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2600 // Remove the character before the current cursor position
2601 removed = RemoveText( -1,
2603 UPDATE_INPUT_STYLE );
2608 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE RemovedText\n", this );
2609 // Notifiy the IMF manager after text changed
2610 // Automatic Upper-case and restarting prediction on an existing word require this.
2613 if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2614 !mImpl->IsPlaceholderAvailable() )
2616 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2620 ShowPlaceholderText();
2621 mImpl->mEventData->mUpdateCursorPosition = true;
2628 void Controller::NotifyImfManager()
2630 if( NULL != mImpl->mEventData )
2632 if( mImpl->mEventData->mImfManager )
2634 // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
2637 mImpl->mEventData->mImfManager.SetSurroundingText( text );
2639 mImpl->mEventData->mImfManager.SetCursorPosition( GetLogicalCursorPosition() );
2640 mImpl->mEventData->mImfManager.NotifyCursorPosition();
2645 void Controller::ShowPlaceholderText()
2647 if( mImpl->IsPlaceholderAvailable() )
2649 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2651 if( NULL == mImpl->mEventData )
2656 mImpl->mEventData->mIsShowingPlaceholderText = true;
2658 // Disable handles when showing place-holder text
2659 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2660 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2661 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2663 const char* text( NULL );
2666 // TODO - Switch placeholder text styles when changing state
2667 if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2668 ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2670 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2671 size = mImpl->mEventData->mPlaceholderTextActive.size();
2675 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2676 size = mImpl->mEventData->mPlaceholderTextInactive.size();
2679 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2680 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2682 // Reset model for showing placeholder.
2683 mImpl->mLogicalModel->mText.Clear();
2684 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2686 // Convert text into UTF-32
2687 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2688 utf32Characters.Resize( size );
2690 // This is a bit horrible but std::string returns a (signed) char*
2691 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2693 // Transform a text array encoded in utf8 into an array encoded in utf32.
2694 // It returns the actual number of characters.
2695 const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2696 utf32Characters.Resize( characterCount );
2698 // The characters to be added.
2699 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = characterCount;
2701 // Reset the cursor position
2702 mImpl->mEventData->mPrimaryCursorPosition = 0;
2704 // The natural size needs to be re-calculated.
2705 mImpl->mRecalculateNaturalSize = true;
2707 // Apply modifications to the model
2708 mImpl->mOperationsPending = ALL_OPERATIONS;
2710 // Update the rest of the model during size negotiation
2711 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2715 void Controller::ClearFontData()
2717 if( mImpl->mFontDefaults )
2719 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2722 // Set flags to update the model.
2723 mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2724 mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2725 mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mLogicalModel->mText.Count();
2727 mImpl->mTextUpdateInfo.mClearAll = true;
2728 mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
2729 mImpl->mRecalculateNaturalSize = true;
2731 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2736 UPDATE_ACTUAL_SIZE |
2741 void Controller::ClearStyleData()
2743 mImpl->mLogicalModel->mColorRuns.Clear();
2744 mImpl->mLogicalModel->ClearFontDescriptionRuns();
2747 Controller::Controller( ControlInterface& controlInterface )
2750 mImpl = new Controller::Impl( controlInterface );
2755 } // namespace Toolkit