2 * Copyright (c) 2017 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-impl.h>
22 #include <dali/public-api/adaptor-framework/key.h>
23 #include <dali/integration-api/debug.h>
27 #include <dali-toolkit/internal/text/bidirectional-support.h>
28 #include <dali-toolkit/internal/text/character-set-conversion.h>
29 #include <dali-toolkit/internal/text/color-segmentation.h>
30 #include <dali-toolkit/internal/text/cursor-helper-functions.h>
31 #include <dali-toolkit/internal/text/multi-language-support.h>
32 #include <dali-toolkit/internal/text/segmentation.h>
33 #include <dali-toolkit/internal/text/shaper.h>
34 #include <dali-toolkit/internal/text/text-control-interface.h>
35 #include <dali-toolkit/internal/text/text-run-container.h>
41 * @brief Struct used to calculate the selection box.
43 struct SelectionBoxInfo
51 #if defined(DEBUG_ENABLED)
52 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
55 const float MAX_FLOAT = std::numeric_limits<float>::max();
56 const float MIN_FLOAT = std::numeric_limits<float>::min();
57 const Dali::Toolkit::Text::CharacterDirection LTR = false; ///< Left To Right direction
70 EventData::EventData( DecoratorPtr decorator )
71 : mDecorator( decorator ),
73 mPlaceholderTextActive(),
74 mPlaceholderTextInactive(),
75 mPlaceholderTextColor( 0.8f, 0.8f, 0.8f, 0.8f ),
77 mInputStyleChangedQueue(),
78 mPreviousState( INACTIVE ),
80 mPrimaryCursorPosition( 0u ),
81 mLeftSelectionPosition( 0u ),
82 mRightSelectionPosition( 0u ),
83 mPreEditStartPosition( 0u ),
85 mCursorHookPositionX( 0.f ),
86 mIsShowingPlaceholderText( false ),
87 mPreEditFlag( false ),
88 mDecoratorUpdated( false ),
89 mCursorBlinkEnabled( true ),
90 mGrabHandleEnabled( true ),
91 mGrabHandlePopupEnabled( true ),
92 mSelectionEnabled( true ),
93 mUpdateCursorHookPosition( false ),
94 mUpdateCursorPosition( false ),
95 mUpdateGrabHandlePosition( false ),
96 mUpdateLeftSelectionPosition( false ),
97 mUpdateRightSelectionPosition( false ),
98 mIsLeftHandleSelected( false ),
99 mUpdateHighlightBox( false ),
100 mScrollAfterUpdatePosition( false ),
101 mScrollAfterDelete( false ),
102 mAllTextSelected( false ),
103 mUpdateInputStyle( false )
105 mImfManager = ImfManager::Get();
108 EventData::~EventData()
111 bool Controller::Impl::ProcessInputEvents()
113 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::ProcessInputEvents\n" );
114 if( NULL == mEventData )
116 // Nothing to do if there is no text input.
117 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::ProcessInputEvents no event data\n" );
121 if( mEventData->mDecorator )
123 for( std::vector<Event>::iterator iter = mEventData->mEventQueue.begin();
124 iter != mEventData->mEventQueue.end();
129 case Event::CURSOR_KEY_EVENT:
131 OnCursorKeyEvent( *iter );
134 case Event::TAP_EVENT:
139 case Event::LONG_PRESS_EVENT:
141 OnLongPressEvent( *iter );
144 case Event::PAN_EVENT:
149 case Event::GRAB_HANDLE_EVENT:
150 case Event::LEFT_SELECTION_HANDLE_EVENT:
151 case Event::RIGHT_SELECTION_HANDLE_EVENT: // Fall through
153 OnHandleEvent( *iter );
158 OnSelectEvent( *iter );
161 case Event::SELECT_ALL:
170 if( mEventData->mUpdateCursorPosition ||
171 mEventData->mUpdateHighlightBox )
176 // The cursor must also be repositioned after inserts into the model
177 if( mEventData->mUpdateCursorPosition )
179 // Updates the cursor position and scrolls the text to make it visible.
180 CursorInfo cursorInfo;
181 // Calculate the cursor position from the new cursor index.
182 GetCursorPosition( mEventData->mPrimaryCursorPosition,
185 if( mEventData->mUpdateCursorHookPosition )
187 // Update the cursor hook position. Used to move the cursor with the keys 'up' and 'down'.
188 mEventData->mCursorHookPositionX = cursorInfo.primaryPosition.x;
189 mEventData->mUpdateCursorHookPosition = false;
192 // Scroll first the text after delete ...
193 if( mEventData->mScrollAfterDelete )
195 ScrollTextToMatchCursor( cursorInfo );
198 // ... then, text can be scrolled to make the cursor visible.
199 if( mEventData->mScrollAfterUpdatePosition )
201 const Vector2 currentCursorPosition( cursorInfo.primaryPosition.x, cursorInfo.lineOffset );
202 ScrollToMakePositionVisible( currentCursorPosition, cursorInfo.lineHeight );
204 mEventData->mScrollAfterUpdatePosition = false;
205 mEventData->mScrollAfterDelete = false;
207 UpdateCursorPosition( cursorInfo );
209 mEventData->mDecoratorUpdated = true;
210 mEventData->mUpdateCursorPosition = false;
211 mEventData->mUpdateGrabHandlePosition = false;
215 CursorInfo leftHandleInfo;
216 CursorInfo rightHandleInfo;
218 if( mEventData->mUpdateHighlightBox )
220 GetCursorPosition( mEventData->mLeftSelectionPosition,
223 GetCursorPosition( mEventData->mRightSelectionPosition,
226 if( mEventData->mScrollAfterUpdatePosition && ( mEventData->mIsLeftHandleSelected ? mEventData->mUpdateLeftSelectionPosition : mEventData->mUpdateRightSelectionPosition ) )
228 CursorInfo& info = mEventData->mIsLeftHandleSelected ? leftHandleInfo : rightHandleInfo;
230 const Vector2 currentCursorPosition( info.primaryPosition.x, info.lineOffset );
231 ScrollToMakePositionVisible( currentCursorPosition, info.lineHeight );
235 if( mEventData->mUpdateLeftSelectionPosition )
237 UpdateSelectionHandle( LEFT_SELECTION_HANDLE,
241 mEventData->mDecoratorUpdated = true;
242 mEventData->mUpdateLeftSelectionPosition = false;
245 if( mEventData->mUpdateRightSelectionPosition )
247 UpdateSelectionHandle( RIGHT_SELECTION_HANDLE,
251 mEventData->mDecoratorUpdated = true;
252 mEventData->mUpdateRightSelectionPosition = false;
255 if( mEventData->mUpdateHighlightBox )
257 RepositionSelectionHandles();
259 mEventData->mUpdateLeftSelectionPosition = false;
260 mEventData->mUpdateRightSelectionPosition = false;
261 mEventData->mUpdateHighlightBox = false;
264 mEventData->mScrollAfterUpdatePosition = false;
267 if( mEventData->mUpdateInputStyle )
269 // Keep a copy of the current input style.
270 InputStyle currentInputStyle;
271 currentInputStyle.Copy( mEventData->mInputStyle );
273 // Set the default style first.
274 RetrieveDefaultInputStyle( mEventData->mInputStyle );
276 // Get the character index from the cursor index.
277 const CharacterIndex styleIndex = ( mEventData->mPrimaryCursorPosition > 0u ) ? mEventData->mPrimaryCursorPosition - 1u : 0u;
279 // Retrieve the style from the style runs stored in the logical model.
280 mLogicalModel->RetrieveStyle( styleIndex, mEventData->mInputStyle );
282 // Compare if the input style has changed.
283 const bool hasInputStyleChanged = !currentInputStyle.Equal( mEventData->mInputStyle );
285 if( hasInputStyleChanged )
287 const InputStyle::Mask styleChangedMask = currentInputStyle.GetInputStyleChangeMask( mEventData->mInputStyle );
288 // Queue the input style changed signal.
289 mEventData->mInputStyleChangedQueue.PushBack( styleChangedMask );
292 mEventData->mUpdateInputStyle = false;
295 mEventData->mEventQueue.clear();
297 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::ProcessInputEvents\n" );
299 const bool decoratorUpdated = mEventData->mDecoratorUpdated;
300 mEventData->mDecoratorUpdated = false;
302 return decoratorUpdated;
305 void Controller::Impl::NotifyImfManager()
307 if( mEventData && mEventData->mImfManager )
309 CharacterIndex cursorPosition = GetLogicalCursorPosition();
311 const Length numberOfWhiteSpaces = GetNumberOfWhiteSpaces( 0u );
313 // Update the cursor position by removing the initial white spaces.
314 if( cursorPosition < numberOfWhiteSpaces )
320 cursorPosition -= numberOfWhiteSpaces;
323 mEventData->mImfManager.SetCursorPosition( cursorPosition );
324 mEventData->mImfManager.NotifyCursorPosition();
328 void Controller::Impl::NotifyImfMultiLineStatus()
332 LayoutEngine::Layout layout = mLayoutEngine.GetLayout();
333 mEventData->mImfManager.NotifyTextInputMultiLine( layout == LayoutEngine::MULTI_LINE_BOX );
337 CharacterIndex Controller::Impl::GetLogicalCursorPosition() const
339 CharacterIndex cursorPosition = 0u;
343 if( ( EventData::SELECTING == mEventData->mState ) ||
344 ( EventData::SELECTION_HANDLE_PANNING == mEventData->mState ) )
346 cursorPosition = std::min( mEventData->mRightSelectionPosition, mEventData->mLeftSelectionPosition );
350 cursorPosition = mEventData->mPrimaryCursorPosition;
354 return cursorPosition;
357 Length Controller::Impl::GetNumberOfWhiteSpaces( CharacterIndex index ) const
359 Length numberOfWhiteSpaces = 0u;
361 // Get the buffer to the text.
362 Character* utf32CharacterBuffer = mLogicalModel->mText.Begin();
364 const Length totalNumberOfCharacters = mLogicalModel->mText.Count();
365 for( ; index < totalNumberOfCharacters; ++index, ++numberOfWhiteSpaces )
367 if( !TextAbstraction::IsWhiteSpace( *( utf32CharacterBuffer + index ) ) )
373 return numberOfWhiteSpaces;
376 void Controller::Impl::GetText( CharacterIndex index, std::string& text ) const
378 // Get the total number of characters.
379 Length numberOfCharacters = mLogicalModel->mText.Count();
381 // Retrieve the text.
382 if( 0u != numberOfCharacters )
384 Utf32ToUtf8( mLogicalModel->mText.Begin() + index, numberOfCharacters - index, text );
388 void Controller::Impl::CalculateTextUpdateIndices( Length& numberOfCharacters )
390 mTextUpdateInfo.mParagraphCharacterIndex = 0u;
391 mTextUpdateInfo.mStartGlyphIndex = 0u;
392 mTextUpdateInfo.mStartLineIndex = 0u;
393 numberOfCharacters = 0u;
395 const Length numberOfParagraphs = mLogicalModel->mParagraphInfo.Count();
396 if( 0u == numberOfParagraphs )
398 mTextUpdateInfo.mParagraphCharacterIndex = 0u;
399 numberOfCharacters = 0u;
401 mTextUpdateInfo.mRequestedNumberOfCharacters = mTextUpdateInfo.mNumberOfCharactersToAdd - mTextUpdateInfo.mNumberOfCharactersToRemove;
403 // Nothing else to do if there are no paragraphs.
407 // Find the paragraphs to be updated.
408 Vector<ParagraphRunIndex> paragraphsToBeUpdated;
409 if( mTextUpdateInfo.mCharacterIndex >= mTextUpdateInfo.mPreviousNumberOfCharacters )
411 // Text is being added at the end of the current text.
412 if( mTextUpdateInfo.mIsLastCharacterNewParagraph )
414 // Text is being added in a new paragraph after the last character of the text.
415 mTextUpdateInfo.mParagraphCharacterIndex = mTextUpdateInfo.mPreviousNumberOfCharacters;
416 numberOfCharacters = 0u;
417 mTextUpdateInfo.mRequestedNumberOfCharacters = mTextUpdateInfo.mNumberOfCharactersToAdd - mTextUpdateInfo.mNumberOfCharactersToRemove;
419 mTextUpdateInfo.mStartGlyphIndex = mVisualModel->mGlyphs.Count();
420 mTextUpdateInfo.mStartLineIndex = mVisualModel->mLines.Count() - 1u;
422 // Nothing else to do;
426 paragraphsToBeUpdated.PushBack( numberOfParagraphs - 1u );
430 Length numberOfCharactersToUpdate = 0u;
431 if( mTextUpdateInfo.mFullRelayoutNeeded )
433 numberOfCharactersToUpdate = mTextUpdateInfo.mPreviousNumberOfCharacters;
437 numberOfCharactersToUpdate = ( mTextUpdateInfo.mNumberOfCharactersToRemove > 0u ) ? mTextUpdateInfo.mNumberOfCharactersToRemove : 1u;
439 mLogicalModel->FindParagraphs( mTextUpdateInfo.mCharacterIndex,
440 numberOfCharactersToUpdate,
441 paragraphsToBeUpdated );
444 if( 0u != paragraphsToBeUpdated.Count() )
446 const ParagraphRunIndex firstParagraphIndex = *( paragraphsToBeUpdated.Begin() );
447 const ParagraphRun& firstParagraph = *( mLogicalModel->mParagraphInfo.Begin() + firstParagraphIndex );
448 mTextUpdateInfo.mParagraphCharacterIndex = firstParagraph.characterRun.characterIndex;
450 ParagraphRunIndex lastParagraphIndex = *( paragraphsToBeUpdated.End() - 1u );
451 const ParagraphRun& lastParagraph = *( mLogicalModel->mParagraphInfo.Begin() + lastParagraphIndex );
453 if( ( mTextUpdateInfo.mNumberOfCharactersToRemove > 0u ) && // Some character are removed.
454 ( lastParagraphIndex < numberOfParagraphs - 1u ) && // There is a next paragraph.
455 ( ( lastParagraph.characterRun.characterIndex + lastParagraph.characterRun.numberOfCharacters ) == // The last removed character is the new paragraph character.
456 ( mTextUpdateInfo.mCharacterIndex + mTextUpdateInfo.mNumberOfCharactersToRemove ) ) )
458 // The new paragraph character of the last updated paragraph has been removed so is going to be merged with the next one.
459 const ParagraphRun& lastParagraph = *( mLogicalModel->mParagraphInfo.Begin() + lastParagraphIndex + 1u );
461 numberOfCharacters = lastParagraph.characterRun.characterIndex + lastParagraph.characterRun.numberOfCharacters - mTextUpdateInfo.mParagraphCharacterIndex;
465 numberOfCharacters = lastParagraph.characterRun.characterIndex + lastParagraph.characterRun.numberOfCharacters - mTextUpdateInfo.mParagraphCharacterIndex;
469 mTextUpdateInfo.mRequestedNumberOfCharacters = numberOfCharacters + mTextUpdateInfo.mNumberOfCharactersToAdd - mTextUpdateInfo.mNumberOfCharactersToRemove;
470 mTextUpdateInfo.mStartGlyphIndex = *( mVisualModel->mCharactersToGlyph.Begin() + mTextUpdateInfo.mParagraphCharacterIndex );
473 void Controller::Impl::ClearFullModelData( OperationsMask operations )
475 if( NO_OPERATION != ( GET_LINE_BREAKS & operations ) )
477 mLogicalModel->mLineBreakInfo.Clear();
478 mLogicalModel->mParagraphInfo.Clear();
481 if( NO_OPERATION != ( GET_WORD_BREAKS & operations ) )
483 mLogicalModel->mLineBreakInfo.Clear();
486 if( NO_OPERATION != ( GET_SCRIPTS & operations ) )
488 mLogicalModel->mScriptRuns.Clear();
491 if( NO_OPERATION != ( VALIDATE_FONTS & operations ) )
493 mLogicalModel->mFontRuns.Clear();
496 if( 0u != mLogicalModel->mBidirectionalParagraphInfo.Count() )
498 if( NO_OPERATION != ( BIDI_INFO & operations ) )
500 mLogicalModel->mBidirectionalParagraphInfo.Clear();
501 mLogicalModel->mCharacterDirections.Clear();
504 if( NO_OPERATION != ( REORDER & operations ) )
506 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
507 for( Vector<BidirectionalLineInfoRun>::Iterator it = mLogicalModel->mBidirectionalLineInfo.Begin(),
508 endIt = mLogicalModel->mBidirectionalLineInfo.End();
512 BidirectionalLineInfoRun& bidiLineInfo = *it;
514 free( bidiLineInfo.visualToLogicalMap );
515 bidiLineInfo.visualToLogicalMap = NULL;
517 mLogicalModel->mBidirectionalLineInfo.Clear();
521 if( NO_OPERATION != ( SHAPE_TEXT & operations ) )
523 mVisualModel->mGlyphs.Clear();
524 mVisualModel->mGlyphsToCharacters.Clear();
525 mVisualModel->mCharactersToGlyph.Clear();
526 mVisualModel->mCharactersPerGlyph.Clear();
527 mVisualModel->mGlyphsPerCharacter.Clear();
528 mVisualModel->mGlyphPositions.Clear();
531 if( NO_OPERATION != ( LAYOUT & operations ) )
533 mVisualModel->mLines.Clear();
536 if( NO_OPERATION != ( COLOR & operations ) )
538 mVisualModel->mColorIndices.Clear();
542 void Controller::Impl::ClearCharacterModelData( CharacterIndex startIndex, CharacterIndex endIndex, OperationsMask operations )
544 const CharacterIndex endIndexPlusOne = endIndex + 1u;
546 if( NO_OPERATION != ( GET_LINE_BREAKS & operations ) )
548 // Clear the line break info.
549 LineBreakInfo* lineBreakInfoBuffer = mLogicalModel->mLineBreakInfo.Begin();
551 mLogicalModel->mLineBreakInfo.Erase( lineBreakInfoBuffer + startIndex,
552 lineBreakInfoBuffer + endIndexPlusOne );
554 // Clear the paragraphs.
555 ClearCharacterRuns( startIndex,
557 mLogicalModel->mParagraphInfo );
560 if( NO_OPERATION != ( GET_WORD_BREAKS & operations ) )
562 // Clear the word break info.
563 WordBreakInfo* wordBreakInfoBuffer = mLogicalModel->mWordBreakInfo.Begin();
565 mLogicalModel->mWordBreakInfo.Erase( wordBreakInfoBuffer + startIndex,
566 wordBreakInfoBuffer + endIndexPlusOne );
569 if( NO_OPERATION != ( GET_SCRIPTS & operations ) )
571 // Clear the scripts.
572 ClearCharacterRuns( startIndex,
574 mLogicalModel->mScriptRuns );
577 if( NO_OPERATION != ( VALIDATE_FONTS & operations ) )
580 ClearCharacterRuns( startIndex,
582 mLogicalModel->mFontRuns );
585 if( 0u != mLogicalModel->mBidirectionalParagraphInfo.Count() )
587 if( NO_OPERATION != ( BIDI_INFO & operations ) )
589 // Clear the bidirectional paragraph info.
590 ClearCharacterRuns( startIndex,
592 mLogicalModel->mBidirectionalParagraphInfo );
594 // Clear the character's directions.
595 CharacterDirection* characterDirectionsBuffer = mLogicalModel->mCharacterDirections.Begin();
597 mLogicalModel->mCharacterDirections.Erase( characterDirectionsBuffer + startIndex,
598 characterDirectionsBuffer + endIndexPlusOne );
601 if( NO_OPERATION != ( REORDER & operations ) )
603 uint32_t startRemoveIndex = mLogicalModel->mBidirectionalLineInfo.Count();
604 uint32_t endRemoveIndex = startRemoveIndex;
605 ClearCharacterRuns( startIndex,
607 mLogicalModel->mBidirectionalLineInfo,
611 BidirectionalLineInfoRun* bidirectionalLineInfoBuffer = mLogicalModel->mBidirectionalLineInfo.Begin();
613 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
614 for( Vector<BidirectionalLineInfoRun>::Iterator it = bidirectionalLineInfoBuffer + startRemoveIndex,
615 endIt = bidirectionalLineInfoBuffer + endRemoveIndex;
619 BidirectionalLineInfoRun& bidiLineInfo = *it;
621 free( bidiLineInfo.visualToLogicalMap );
622 bidiLineInfo.visualToLogicalMap = NULL;
625 mLogicalModel->mBidirectionalLineInfo.Erase( bidirectionalLineInfoBuffer + startRemoveIndex,
626 bidirectionalLineInfoBuffer + endRemoveIndex );
631 void Controller::Impl::ClearGlyphModelData( CharacterIndex startIndex, CharacterIndex endIndex, OperationsMask operations )
633 const CharacterIndex endIndexPlusOne = endIndex + 1u;
634 const Length numberOfCharactersRemoved = endIndexPlusOne - startIndex;
636 // Convert the character index to glyph index before deleting the character to glyph and the glyphs per character buffers.
637 GlyphIndex* charactersToGlyphBuffer = mVisualModel->mCharactersToGlyph.Begin();
638 Length* glyphsPerCharacterBuffer = mVisualModel->mGlyphsPerCharacter.Begin();
640 const GlyphIndex endGlyphIndexPlusOne = *( charactersToGlyphBuffer + endIndex ) + *( glyphsPerCharacterBuffer + endIndex );
641 const Length numberOfGlyphsRemoved = endGlyphIndexPlusOne - mTextUpdateInfo.mStartGlyphIndex;
643 if( NO_OPERATION != ( SHAPE_TEXT & operations ) )
645 // Update the character to glyph indices.
646 for( Vector<GlyphIndex>::Iterator it = charactersToGlyphBuffer + endIndexPlusOne,
647 endIt = charactersToGlyphBuffer + mVisualModel->mCharactersToGlyph.Count();
651 CharacterIndex& index = *it;
652 index -= numberOfGlyphsRemoved;
655 // Clear the character to glyph conversion table.
656 mVisualModel->mCharactersToGlyph.Erase( charactersToGlyphBuffer + startIndex,
657 charactersToGlyphBuffer + endIndexPlusOne );
659 // Clear the glyphs per character table.
660 mVisualModel->mGlyphsPerCharacter.Erase( glyphsPerCharacterBuffer + startIndex,
661 glyphsPerCharacterBuffer + endIndexPlusOne );
663 // Clear the glyphs buffer.
664 GlyphInfo* glyphsBuffer = mVisualModel->mGlyphs.Begin();
665 mVisualModel->mGlyphs.Erase( glyphsBuffer + mTextUpdateInfo.mStartGlyphIndex,
666 glyphsBuffer + endGlyphIndexPlusOne );
668 CharacterIndex* glyphsToCharactersBuffer = mVisualModel->mGlyphsToCharacters.Begin();
670 // Update the glyph to character indices.
671 for( Vector<CharacterIndex>::Iterator it = glyphsToCharactersBuffer + endGlyphIndexPlusOne,
672 endIt = glyphsToCharactersBuffer + mVisualModel->mGlyphsToCharacters.Count();
676 CharacterIndex& index = *it;
677 index -= numberOfCharactersRemoved;
680 // Clear the glyphs to characters buffer.
681 mVisualModel->mGlyphsToCharacters.Erase( glyphsToCharactersBuffer + mTextUpdateInfo.mStartGlyphIndex,
682 glyphsToCharactersBuffer + endGlyphIndexPlusOne );
684 // Clear the characters per glyph buffer.
685 Length* charactersPerGlyphBuffer = mVisualModel->mCharactersPerGlyph.Begin();
686 mVisualModel->mCharactersPerGlyph.Erase( charactersPerGlyphBuffer + mTextUpdateInfo.mStartGlyphIndex,
687 charactersPerGlyphBuffer + endGlyphIndexPlusOne );
689 // Clear the positions buffer.
690 Vector2* positionsBuffer = mVisualModel->mGlyphPositions.Begin();
691 mVisualModel->mGlyphPositions.Erase( positionsBuffer + mTextUpdateInfo.mStartGlyphIndex,
692 positionsBuffer + endGlyphIndexPlusOne );
695 if( NO_OPERATION != ( LAYOUT & operations ) )
698 uint32_t startRemoveIndex = mVisualModel->mLines.Count();
699 uint32_t endRemoveIndex = startRemoveIndex;
700 ClearCharacterRuns( startIndex,
702 mVisualModel->mLines,
706 // Will update the glyph runs.
707 startRemoveIndex = mVisualModel->mLines.Count();
708 endRemoveIndex = startRemoveIndex;
709 ClearGlyphRuns( mTextUpdateInfo.mStartGlyphIndex,
710 endGlyphIndexPlusOne - 1u,
711 mVisualModel->mLines,
715 // Set the line index from where to insert the new laid-out lines.
716 mTextUpdateInfo.mStartLineIndex = startRemoveIndex;
718 LineRun* linesBuffer = mVisualModel->mLines.Begin();
719 mVisualModel->mLines.Erase( linesBuffer + startRemoveIndex,
720 linesBuffer + endRemoveIndex );
723 if( NO_OPERATION != ( COLOR & operations ) )
725 if( 0u != mVisualModel->mColorIndices.Count() )
727 ColorIndex* colorIndexBuffer = mVisualModel->mColorIndices.Begin();
728 mVisualModel->mColorIndices.Erase( colorIndexBuffer + mTextUpdateInfo.mStartGlyphIndex,
729 colorIndexBuffer + endGlyphIndexPlusOne );
734 void Controller::Impl::ClearModelData( CharacterIndex startIndex, CharacterIndex endIndex, OperationsMask operations )
736 if( mTextUpdateInfo.mClearAll ||
737 ( ( 0u == startIndex ) &&
738 ( mTextUpdateInfo.mPreviousNumberOfCharacters == endIndex + 1u ) ) )
740 ClearFullModelData( operations );
744 // Clear the model data related with characters.
745 ClearCharacterModelData( startIndex, endIndex, operations );
747 // Clear the model data related with glyphs.
748 ClearGlyphModelData( startIndex, endIndex, operations );
751 // The estimated number of lines. Used to avoid reallocations when layouting.
752 mTextUpdateInfo.mEstimatedNumberOfLines = std::max( mVisualModel->mLines.Count(), mLogicalModel->mParagraphInfo.Count() );
754 mVisualModel->ClearCaches();
757 bool Controller::Impl::UpdateModel( OperationsMask operationsRequired )
759 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::UpdateModel\n" );
761 // Calculate the operations to be done.
762 const OperationsMask operations = static_cast<OperationsMask>( mOperationsPending & operationsRequired );
764 if( NO_OPERATION == operations )
766 // Nothing to do if no operations are pending and required.
770 Vector<Character>& utf32Characters = mLogicalModel->mText;
772 const Length numberOfCharacters = utf32Characters.Count();
774 // Index to the first character of the first paragraph to be updated.
775 CharacterIndex startIndex = 0u;
776 // Number of characters of the paragraphs to be removed.
777 Length paragraphCharacters = 0u;
779 CalculateTextUpdateIndices( paragraphCharacters );
780 startIndex = mTextUpdateInfo.mParagraphCharacterIndex;
782 if( mTextUpdateInfo.mClearAll ||
783 ( 0u != paragraphCharacters ) )
785 ClearModelData( startIndex, startIndex + ( ( paragraphCharacters > 0u ) ? paragraphCharacters - 1u : 0u ), operations );
788 mTextUpdateInfo.mClearAll = false;
790 // Whether the model is updated.
791 bool updated = false;
793 Vector<LineBreakInfo>& lineBreakInfo = mLogicalModel->mLineBreakInfo;
794 const Length requestedNumberOfCharacters = mTextUpdateInfo.mRequestedNumberOfCharacters;
796 if( NO_OPERATION != ( GET_LINE_BREAKS & operations ) )
798 // Retrieves the line break info. The line break info is used to split the text in 'paragraphs' to
799 // calculate the bidirectional info for each 'paragraph'.
800 // It's also used to layout the text (where it should be a new line) or to shape the text (text in different lines
801 // is not shaped together).
802 lineBreakInfo.Resize( numberOfCharacters, TextAbstraction::LINE_NO_BREAK );
804 SetLineBreakInfo( utf32Characters,
806 requestedNumberOfCharacters,
809 // Create the paragraph info.
810 mLogicalModel->CreateParagraphInfo( startIndex,
811 requestedNumberOfCharacters );
815 Vector<WordBreakInfo>& wordBreakInfo = mLogicalModel->mWordBreakInfo;
816 if( NO_OPERATION != ( GET_WORD_BREAKS & operations ) )
818 // Retrieves the word break info. The word break info is used to layout the text (where to wrap the text in lines).
819 wordBreakInfo.Resize( numberOfCharacters, TextAbstraction::WORD_NO_BREAK );
821 SetWordBreakInfo( utf32Characters,
823 requestedNumberOfCharacters,
828 const bool getScripts = NO_OPERATION != ( GET_SCRIPTS & operations );
829 const bool validateFonts = NO_OPERATION != ( VALIDATE_FONTS & operations );
831 Vector<ScriptRun>& scripts = mLogicalModel->mScriptRuns;
832 Vector<FontRun>& validFonts = mLogicalModel->mFontRuns;
834 if( getScripts || validateFonts )
836 // Validates the fonts assigned by the application or assigns default ones.
837 // It makes sure all the characters are going to be rendered by the correct font.
838 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
842 // Retrieves the scripts used in the text.
843 multilanguageSupport.SetScripts( utf32Characters,
845 requestedNumberOfCharacters,
851 // Validate the fonts set through the mark-up string.
852 Vector<FontDescriptionRun>& fontDescriptionRuns = mLogicalModel->mFontDescriptionRuns;
854 // Get the default font's description.
855 TextAbstraction::FontDescription defaultFontDescription;
856 TextAbstraction::PointSize26Dot6 defaultPointSize = TextAbstraction::FontClient::DEFAULT_POINT_SIZE;
857 if( NULL != mFontDefaults )
859 defaultFontDescription = mFontDefaults->mFontDescription;
860 defaultPointSize = mFontDefaults->mDefaultPointSize * 64u;
863 // Validates the fonts. If there is a character with no assigned font it sets a default one.
864 // After this call, fonts are validated.
865 multilanguageSupport.ValidateFonts( utf32Characters,
868 defaultFontDescription,
871 requestedNumberOfCharacters,
877 Vector<Character> mirroredUtf32Characters;
878 bool textMirrored = false;
879 const Length numberOfParagraphs = mLogicalModel->mParagraphInfo.Count();
880 if( NO_OPERATION != ( BIDI_INFO & operations ) )
882 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mLogicalModel->mBidirectionalParagraphInfo;
883 bidirectionalInfo.Reserve( numberOfParagraphs );
885 // Calculates the bidirectional info for the whole paragraph if it contains right to left scripts.
886 SetBidirectionalInfo( utf32Characters,
890 requestedNumberOfCharacters,
893 if( 0u != bidirectionalInfo.Count() )
895 // Only set the character directions if there is right to left characters.
896 Vector<CharacterDirection>& directions = mLogicalModel->mCharacterDirections;
897 GetCharactersDirection( bidirectionalInfo,
900 requestedNumberOfCharacters,
903 // This paragraph has right to left text. Some characters may need to be mirrored.
904 // TODO: consider if the mirrored string can be stored as well.
906 textMirrored = GetMirroredText( utf32Characters,
910 requestedNumberOfCharacters,
911 mirroredUtf32Characters );
915 // There is no right to left characters. Clear the directions vector.
916 mLogicalModel->mCharacterDirections.Clear();
921 Vector<GlyphInfo>& glyphs = mVisualModel->mGlyphs;
922 Vector<CharacterIndex>& glyphsToCharactersMap = mVisualModel->mGlyphsToCharacters;
923 Vector<Length>& charactersPerGlyph = mVisualModel->mCharactersPerGlyph;
924 Vector<GlyphIndex> newParagraphGlyphs;
925 newParagraphGlyphs.Reserve( numberOfParagraphs );
927 const Length currentNumberOfGlyphs = glyphs.Count();
928 if( NO_OPERATION != ( SHAPE_TEXT & operations ) )
930 const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
932 ShapeText( textToShape,
937 mTextUpdateInfo.mStartGlyphIndex,
938 requestedNumberOfCharacters,
940 glyphsToCharactersMap,
942 newParagraphGlyphs );
944 // Create the 'number of glyphs' per character and the glyph to character conversion tables.
945 mVisualModel->CreateGlyphsPerCharacterTable( startIndex, mTextUpdateInfo.mStartGlyphIndex, requestedNumberOfCharacters );
946 mVisualModel->CreateCharacterToGlyphTable( startIndex, mTextUpdateInfo.mStartGlyphIndex, requestedNumberOfCharacters );
950 const Length numberOfGlyphs = glyphs.Count() - currentNumberOfGlyphs;
952 if( NO_OPERATION != ( GET_GLYPH_METRICS & operations ) )
954 GlyphInfo* glyphsBuffer = glyphs.Begin();
955 mMetrics->GetGlyphMetrics( glyphsBuffer + mTextUpdateInfo.mStartGlyphIndex, numberOfGlyphs );
957 // Update the width and advance of all new paragraph characters.
958 for( Vector<GlyphIndex>::ConstIterator it = newParagraphGlyphs.Begin(), endIt = newParagraphGlyphs.End(); it != endIt; ++it )
960 const GlyphIndex index = *it;
961 GlyphInfo& glyph = *( glyphsBuffer + index );
963 glyph.xBearing = 0.f;
970 if( NO_OPERATION != ( COLOR & operations ) )
972 // Set the color runs in glyphs.
973 SetColorSegmentationInfo( mLogicalModel->mColorRuns,
974 mVisualModel->mCharactersToGlyph,
975 mVisualModel->mGlyphsPerCharacter,
977 mTextUpdateInfo.mStartGlyphIndex,
978 requestedNumberOfCharacters,
979 mVisualModel->mColors,
980 mVisualModel->mColorIndices );
985 if( ( NULL != mEventData ) &&
986 mEventData->mPreEditFlag &&
987 ( 0u != mVisualModel->mCharactersToGlyph.Count() ) )
989 // Add the underline for the pre-edit text.
990 const GlyphIndex* const charactersToGlyphBuffer = mVisualModel->mCharactersToGlyph.Begin();
991 const Length* const glyphsPerCharacterBuffer = mVisualModel->mGlyphsPerCharacter.Begin();
993 const GlyphIndex glyphStart = *( charactersToGlyphBuffer + mEventData->mPreEditStartPosition );
994 const CharacterIndex lastPreEditCharacter = mEventData->mPreEditStartPosition + ( ( mEventData->mPreEditLength > 0u ) ? mEventData->mPreEditLength - 1u : 0u );
995 const Length numberOfGlyphsLastCharacter = *( glyphsPerCharacterBuffer + lastPreEditCharacter );
996 const GlyphIndex glyphEnd = *( charactersToGlyphBuffer + lastPreEditCharacter ) + ( numberOfGlyphsLastCharacter > 1u ? numberOfGlyphsLastCharacter - 1u : 0u );
998 GlyphRun underlineRun;
999 underlineRun.glyphIndex = glyphStart;
1000 underlineRun.numberOfGlyphs = 1u + glyphEnd - glyphStart;
1002 // TODO: At the moment the underline runs are only for pre-edit.
1003 mVisualModel->mUnderlineRuns.PushBack( underlineRun );
1006 // The estimated number of lines. Used to avoid reallocations when layouting.
1007 mTextUpdateInfo.mEstimatedNumberOfLines = std::max( mVisualModel->mLines.Count(), mLogicalModel->mParagraphInfo.Count() );
1009 // Set the previous number of characters for the next time the text is updated.
1010 mTextUpdateInfo.mPreviousNumberOfCharacters = numberOfCharacters;
1015 void Controller::Impl::RetrieveDefaultInputStyle( InputStyle& inputStyle )
1017 // Sets the default text's color.
1018 inputStyle.textColor = mTextColor;
1019 inputStyle.isDefaultColor = true;
1021 inputStyle.familyName.clear();
1022 inputStyle.weight = TextAbstraction::FontWeight::NORMAL;
1023 inputStyle.width = TextAbstraction::FontWidth::NORMAL;
1024 inputStyle.slant = TextAbstraction::FontSlant::NORMAL;
1025 inputStyle.size = 0.f;
1027 inputStyle.lineSpacing = 0.f;
1029 inputStyle.underlineProperties.clear();
1030 inputStyle.shadowProperties.clear();
1031 inputStyle.embossProperties.clear();
1032 inputStyle.outlineProperties.clear();
1034 inputStyle.isFamilyDefined = false;
1035 inputStyle.isWeightDefined = false;
1036 inputStyle.isWidthDefined = false;
1037 inputStyle.isSlantDefined = false;
1038 inputStyle.isSizeDefined = false;
1040 inputStyle.isLineSpacingDefined = false;
1042 inputStyle.isUnderlineDefined = false;
1043 inputStyle.isShadowDefined = false;
1044 inputStyle.isEmbossDefined = false;
1045 inputStyle.isOutlineDefined = false;
1047 // Sets the default font's family name, weight, width, slant and size.
1050 if( mFontDefaults->familyDefined )
1052 inputStyle.familyName = mFontDefaults->mFontDescription.family;
1053 inputStyle.isFamilyDefined = true;
1056 if( mFontDefaults->weightDefined )
1058 inputStyle.weight = mFontDefaults->mFontDescription.weight;
1059 inputStyle.isWeightDefined = true;
1062 if( mFontDefaults->widthDefined )
1064 inputStyle.width = mFontDefaults->mFontDescription.width;
1065 inputStyle.isWidthDefined = true;
1068 if( mFontDefaults->slantDefined )
1070 inputStyle.slant = mFontDefaults->mFontDescription.slant;
1071 inputStyle.isSlantDefined = true;
1074 if( mFontDefaults->sizeDefined )
1076 inputStyle.size = mFontDefaults->mDefaultPointSize;
1077 inputStyle.isSizeDefined = true;
1082 float Controller::Impl::GetDefaultFontLineHeight()
1084 FontId defaultFontId = 0u;
1085 if( NULL == mFontDefaults )
1087 TextAbstraction::FontDescription fontDescription;
1088 defaultFontId = mFontClient.GetFontId( fontDescription );
1092 defaultFontId = mFontDefaults->GetFontId( mFontClient );
1095 Text::FontMetrics fontMetrics;
1096 mMetrics->GetFontMetrics( defaultFontId, fontMetrics );
1098 return( fontMetrics.ascender - fontMetrics.descender );
1101 void Controller::Impl::OnCursorKeyEvent( const Event& event )
1103 if( NULL == mEventData )
1105 // Nothing to do if there is no text input.
1109 int keyCode = event.p1.mInt;
1111 if( Dali::DALI_KEY_CURSOR_LEFT == keyCode )
1113 if( mEventData->mPrimaryCursorPosition > 0u )
1115 mEventData->mPrimaryCursorPosition = CalculateNewCursorIndex( mEventData->mPrimaryCursorPosition - 1u );
1118 else if( Dali::DALI_KEY_CURSOR_RIGHT == keyCode )
1120 if( mLogicalModel->mText.Count() > mEventData->mPrimaryCursorPosition )
1122 mEventData->mPrimaryCursorPosition = CalculateNewCursorIndex( mEventData->mPrimaryCursorPosition );
1125 else if( Dali::DALI_KEY_CURSOR_UP == keyCode )
1127 // Get first the line index of the current cursor position index.
1128 CharacterIndex characterIndex = 0u;
1130 if( mEventData->mPrimaryCursorPosition > 0u )
1132 characterIndex = mEventData->mPrimaryCursorPosition - 1u;
1135 const LineIndex lineIndex = mVisualModel->GetLineOfCharacter( characterIndex );
1137 if( lineIndex > 0u )
1139 // Retrieve the cursor position info.
1140 CursorInfo cursorInfo;
1141 GetCursorPosition( mEventData->mPrimaryCursorPosition,
1144 // Get the line above.
1145 const LineRun& line = *( mVisualModel->mLines.Begin() + ( lineIndex - 1u ) );
1147 // Get the next hit 'y' point.
1148 const float hitPointY = cursorInfo.lineOffset - 0.5f * ( line.ascender - line.descender );
1150 // Use the cursor hook position 'x' and the next hit 'y' position to calculate the new cursor index.
1151 mEventData->mPrimaryCursorPosition = Text::GetClosestCursorIndex( mVisualModel,
1154 mEventData->mCursorHookPositionX,
1158 else if( Dali::DALI_KEY_CURSOR_DOWN == keyCode )
1160 // Get first the line index of the current cursor position index.
1161 CharacterIndex characterIndex = 0u;
1163 if( mEventData->mPrimaryCursorPosition > 0u )
1165 characterIndex = mEventData->mPrimaryCursorPosition - 1u;
1168 const LineIndex lineIndex = mVisualModel->GetLineOfCharacter( characterIndex );
1170 if( lineIndex + 1u < mVisualModel->mLines.Count() )
1172 // Retrieve the cursor position info.
1173 CursorInfo cursorInfo;
1174 GetCursorPosition( mEventData->mPrimaryCursorPosition,
1177 // Get the line below.
1178 const LineRun& line = *( mVisualModel->mLines.Begin() + lineIndex + 1u );
1180 // Get the next hit 'y' point.
1181 const float hitPointY = cursorInfo.lineOffset + cursorInfo.lineHeight + 0.5f * ( line.ascender - line.descender );
1183 // Use the cursor hook position 'x' and the next hit 'y' position to calculate the new cursor index.
1184 mEventData->mPrimaryCursorPosition = Text::GetClosestCursorIndex( mVisualModel,
1187 mEventData->mCursorHookPositionX,
1192 mEventData->mUpdateCursorPosition = true;
1193 mEventData->mUpdateInputStyle = true;
1194 mEventData->mScrollAfterUpdatePosition = true;
1197 void Controller::Impl::OnTapEvent( const Event& event )
1199 if( NULL != mEventData )
1201 const unsigned int tapCount = event.p1.mUint;
1203 if( 1u == tapCount )
1205 if( IsShowingRealText() )
1207 // Convert from control's coords to text's coords.
1208 const float xPosition = event.p2.mFloat - mScrollPosition.x;
1209 const float yPosition = event.p3.mFloat - mScrollPosition.y;
1211 // Keep the tap 'x' position. Used to move the cursor.
1212 mEventData->mCursorHookPositionX = xPosition;
1214 mEventData->mPrimaryCursorPosition = Text::GetClosestCursorIndex( mVisualModel,
1220 // When the cursor position is changing, delay cursor blinking
1221 mEventData->mDecorator->DelayCursorBlink();
1225 mEventData->mPrimaryCursorPosition = 0u;
1228 mEventData->mUpdateCursorPosition = true;
1229 mEventData->mUpdateGrabHandlePosition = true;
1230 mEventData->mScrollAfterUpdatePosition = true;
1231 mEventData->mUpdateInputStyle = true;
1233 // Notify the cursor position to the imf manager.
1234 if( mEventData->mImfManager )
1236 mEventData->mImfManager.SetCursorPosition( mEventData->mPrimaryCursorPosition );
1237 mEventData->mImfManager.NotifyCursorPosition();
1243 void Controller::Impl::OnPanEvent( const Event& event )
1245 if( NULL == mEventData )
1247 // Nothing to do if there is no text input.
1251 const bool isHorizontalScrollEnabled = mEventData->mDecorator->IsHorizontalScrollEnabled();
1252 const bool isVerticalScrollEnabled = mEventData->mDecorator->IsVerticalScrollEnabled();
1254 if( !isHorizontalScrollEnabled && !isVerticalScrollEnabled )
1256 // Nothing to do if scrolling is not enabled.
1260 const int state = event.p1.mInt;
1264 case Gesture::Started:
1266 // Will remove the cursor, handles or text's popup, ...
1267 ChangeState( EventData::TEXT_PANNING );
1270 case Gesture::Continuing:
1272 const Vector2& layoutSize = mVisualModel->GetLayoutSize();
1273 const Vector2 currentScroll = mScrollPosition;
1275 if( isHorizontalScrollEnabled )
1277 const float displacementX = event.p2.mFloat;
1278 mScrollPosition.x += displacementX;
1280 ClampHorizontalScroll( layoutSize );
1283 if( isVerticalScrollEnabled )
1285 const float displacementY = event.p3.mFloat;
1286 mScrollPosition.y += displacementY;
1288 ClampVerticalScroll( layoutSize );
1291 mEventData->mDecorator->UpdatePositions( mScrollPosition - currentScroll );
1294 case Gesture::Finished:
1295 case Gesture::Cancelled: // FALLTHROUGH
1297 // Will go back to the previous state to show the cursor, handles, the text's popup, ...
1298 ChangeState( mEventData->mPreviousState );
1306 void Controller::Impl::OnLongPressEvent( const Event& event )
1308 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::OnLongPressEvent\n" );
1310 if( EventData::EDITING == mEventData->mState )
1312 ChangeState ( EventData::EDITING_WITH_POPUP );
1313 mEventData->mDecoratorUpdated = true;
1314 mEventData->mUpdateInputStyle = true;
1318 void Controller::Impl::OnHandleEvent( const Event& event )
1320 if( NULL == mEventData )
1322 // Nothing to do if there is no text input.
1326 const unsigned int state = event.p1.mUint;
1327 const bool handleStopScrolling = ( HANDLE_STOP_SCROLLING == state );
1328 const bool isSmoothHandlePanEnabled = mEventData->mDecorator->IsSmoothHandlePanEnabled();
1330 if( HANDLE_PRESSED == state )
1332 // Convert from decorator's coords to text's coords.
1333 const float xPosition = event.p2.mFloat - mScrollPosition.x;
1334 const float yPosition = event.p3.mFloat - mScrollPosition.y;
1336 // Need to calculate the handle's new position.
1337 const CharacterIndex handleNewPosition = Text::GetClosestCursorIndex( mVisualModel,
1343 if( Event::GRAB_HANDLE_EVENT == event.type )
1345 ChangeState ( EventData::GRAB_HANDLE_PANNING );
1347 if( handleNewPosition != mEventData->mPrimaryCursorPosition )
1349 // Updates the cursor position if the handle's new position is different than the current one.
1350 mEventData->mUpdateCursorPosition = true;
1351 // Does not update the grab handle position if the smooth panning is enabled. (The decorator does it smooth).
1352 mEventData->mUpdateGrabHandlePosition = !isSmoothHandlePanEnabled;
1353 mEventData->mPrimaryCursorPosition = handleNewPosition;
1356 // Updates the decorator if the soft handle panning is enabled. It triggers a relayout in the decorator and the new position of the handle is set.
1357 mEventData->mDecoratorUpdated = isSmoothHandlePanEnabled;
1359 else if( Event::LEFT_SELECTION_HANDLE_EVENT == event.type )
1361 ChangeState ( EventData::SELECTION_HANDLE_PANNING );
1363 if( ( handleNewPosition != mEventData->mLeftSelectionPosition ) &&
1364 ( handleNewPosition != mEventData->mRightSelectionPosition ) )
1366 // Updates the highlight box if the handle's new position is different than the current one.
1367 mEventData->mUpdateHighlightBox = true;
1368 // Does not update the selection handle position if the smooth panning is enabled. (The decorator does it smooth).
1369 mEventData->mUpdateLeftSelectionPosition = !isSmoothHandlePanEnabled;
1370 mEventData->mLeftSelectionPosition = handleNewPosition;
1373 // Updates the decorator if the soft handle panning is enabled. It triggers a relayout in the decorator and the new position of the handle is set.
1374 mEventData->mDecoratorUpdated = isSmoothHandlePanEnabled;
1376 // Will define the order to scroll the text to match the handle position.
1377 mEventData->mIsLeftHandleSelected = true;
1379 else if( Event::RIGHT_SELECTION_HANDLE_EVENT == event.type )
1381 ChangeState ( EventData::SELECTION_HANDLE_PANNING );
1383 if( ( handleNewPosition != mEventData->mRightSelectionPosition ) &&
1384 ( handleNewPosition != mEventData->mLeftSelectionPosition ) )
1386 // Updates the highlight box if the handle's new position is different than the current one.
1387 mEventData->mUpdateHighlightBox = true;
1388 // Does not update the selection handle position if the smooth panning is enabled. (The decorator does it smooth).
1389 mEventData->mUpdateRightSelectionPosition = !isSmoothHandlePanEnabled;
1390 mEventData->mRightSelectionPosition = handleNewPosition;
1393 // Updates the decorator if the soft handle panning is enabled. It triggers a relayout in the decorator and the new position of the handle is set.
1394 mEventData->mDecoratorUpdated = isSmoothHandlePanEnabled;
1396 // Will define the order to scroll the text to match the handle position.
1397 mEventData->mIsLeftHandleSelected = false;
1399 } // end ( HANDLE_PRESSED == state )
1400 else if( ( HANDLE_RELEASED == state ) ||
1401 handleStopScrolling )
1403 CharacterIndex handlePosition = 0u;
1404 if( handleStopScrolling || isSmoothHandlePanEnabled )
1406 // Convert from decorator's coords to text's coords.
1407 const float xPosition = event.p2.mFloat - mScrollPosition.x;
1408 const float yPosition = event.p3.mFloat - mScrollPosition.y;
1410 handlePosition = Text::GetClosestCursorIndex( mVisualModel,
1417 if( Event::GRAB_HANDLE_EVENT == event.type )
1419 mEventData->mUpdateCursorPosition = true;
1420 mEventData->mUpdateGrabHandlePosition = true;
1421 mEventData->mUpdateInputStyle = true;
1423 if( !IsClipboardEmpty() )
1425 ChangeState( EventData::EDITING_WITH_PASTE_POPUP ); // Moving grabhandle will show Paste Popup
1428 if( handleStopScrolling || isSmoothHandlePanEnabled )
1430 mEventData->mScrollAfterUpdatePosition = true;
1431 mEventData->mPrimaryCursorPosition = handlePosition;
1434 else if( Event::LEFT_SELECTION_HANDLE_EVENT == event.type )
1436 ChangeState( EventData::SELECTING );
1438 mEventData->mUpdateHighlightBox = true;
1439 mEventData->mUpdateLeftSelectionPosition = true;
1440 mEventData->mUpdateRightSelectionPosition = true;
1442 if( handleStopScrolling || isSmoothHandlePanEnabled )
1444 mEventData->mScrollAfterUpdatePosition = true;
1446 if( ( handlePosition != mEventData->mRightSelectionPosition ) &&
1447 ( handlePosition != mEventData->mLeftSelectionPosition ) )
1449 mEventData->mLeftSelectionPosition = handlePosition;
1453 else if( Event::RIGHT_SELECTION_HANDLE_EVENT == event.type )
1455 ChangeState( EventData::SELECTING );
1457 mEventData->mUpdateHighlightBox = true;
1458 mEventData->mUpdateRightSelectionPosition = true;
1459 mEventData->mUpdateLeftSelectionPosition = true;
1461 if( handleStopScrolling || isSmoothHandlePanEnabled )
1463 mEventData->mScrollAfterUpdatePosition = true;
1464 if( ( handlePosition != mEventData->mRightSelectionPosition ) &&
1465 ( handlePosition != mEventData->mLeftSelectionPosition ) )
1467 mEventData->mRightSelectionPosition = handlePosition;
1472 mEventData->mDecoratorUpdated = true;
1473 } // end ( ( HANDLE_RELEASED == state ) || ( HANDLE_STOP_SCROLLING == state ) )
1474 else if( HANDLE_SCROLLING == state )
1476 const float xSpeed = event.p2.mFloat;
1477 const float ySpeed = event.p3.mFloat;
1478 const Vector2& layoutSize = mVisualModel->GetLayoutSize();
1479 const Vector2 currentScrollPosition = mScrollPosition;
1481 mScrollPosition.x += xSpeed;
1482 mScrollPosition.y += ySpeed;
1484 ClampHorizontalScroll( layoutSize );
1485 ClampVerticalScroll( layoutSize );
1487 bool endOfScroll = false;
1488 if( Vector2::ZERO == ( currentScrollPosition - mScrollPosition ) )
1490 // Notify the decorator there is no more text to scroll.
1491 // The decorator won't send more scroll events.
1492 mEventData->mDecorator->NotifyEndOfScroll();
1493 // Still need to set the position of the handle.
1497 // Set the position of the handle.
1498 const bool scrollRightDirection = xSpeed > 0.f;
1499 const bool scrollBottomDirection = ySpeed > 0.f;
1500 const bool leftSelectionHandleEvent = Event::LEFT_SELECTION_HANDLE_EVENT == event.type;
1501 const bool rightSelectionHandleEvent = Event::RIGHT_SELECTION_HANDLE_EVENT == event.type;
1503 if( Event::GRAB_HANDLE_EVENT == event.type )
1505 ChangeState( EventData::GRAB_HANDLE_PANNING );
1507 // Get the grab handle position in decorator coords.
1508 Vector2 position = mEventData->mDecorator->GetPosition( GRAB_HANDLE );
1510 if( mEventData->mDecorator->IsHorizontalScrollEnabled() )
1512 // Position the grag handle close to either the left or right edge.
1513 position.x = scrollRightDirection ? 0.f : mVisualModel->mControlSize.width;
1516 if( mEventData->mDecorator->IsVerticalScrollEnabled() )
1518 position.x = mEventData->mCursorHookPositionX;
1520 // Position the grag handle close to either the top or bottom edge.
1521 position.y = scrollBottomDirection ? 0.f : mVisualModel->mControlSize.height;
1524 // Get the new handle position.
1525 // The grab handle's position is in decorator's coords. Need to transforms to text's coords.
1526 const CharacterIndex handlePosition = Text::GetClosestCursorIndex( mVisualModel,
1529 position.x - mScrollPosition.x,
1530 position.y - mScrollPosition.y );
1532 if( mEventData->mPrimaryCursorPosition != handlePosition )
1534 mEventData->mUpdateCursorPosition = true;
1535 mEventData->mUpdateGrabHandlePosition = !isSmoothHandlePanEnabled;
1536 mEventData->mScrollAfterUpdatePosition = true;
1537 mEventData->mPrimaryCursorPosition = handlePosition;
1539 mEventData->mUpdateInputStyle = mEventData->mUpdateCursorPosition;
1541 // Updates the decorator if the soft handle panning is enabled.
1542 mEventData->mDecoratorUpdated = isSmoothHandlePanEnabled;
1544 else if( leftSelectionHandleEvent || rightSelectionHandleEvent )
1546 ChangeState( EventData::SELECTION_HANDLE_PANNING );
1548 // Get the selection handle position in decorator coords.
1549 Vector2 position = mEventData->mDecorator->GetPosition( leftSelectionHandleEvent ? Text::LEFT_SELECTION_HANDLE : Text::RIGHT_SELECTION_HANDLE );
1551 if( mEventData->mDecorator->IsHorizontalScrollEnabled() )
1553 // Position the selection handle close to either the left or right edge.
1554 position.x = scrollRightDirection ? 0.f : mVisualModel->mControlSize.width;
1557 if( mEventData->mDecorator->IsVerticalScrollEnabled() )
1559 position.x = mEventData->mCursorHookPositionX;
1561 // Position the grag handle close to either the top or bottom edge.
1562 position.y = scrollBottomDirection ? 0.f : mVisualModel->mControlSize.height;
1565 // Get the new handle position.
1566 // The selection handle's position is in decorator's coords. Need to transform to text's coords.
1567 const CharacterIndex handlePosition = Text::GetClosestCursorIndex( mVisualModel,
1570 position.x - mScrollPosition.x,
1571 position.y - mScrollPosition.y );
1573 if( leftSelectionHandleEvent )
1575 const bool differentHandles = ( mEventData->mLeftSelectionPosition != handlePosition ) && ( mEventData->mRightSelectionPosition != handlePosition );
1577 if( differentHandles || endOfScroll )
1579 mEventData->mUpdateHighlightBox = true;
1580 mEventData->mUpdateLeftSelectionPosition = !isSmoothHandlePanEnabled;
1581 mEventData->mUpdateRightSelectionPosition = isSmoothHandlePanEnabled;
1582 mEventData->mLeftSelectionPosition = handlePosition;
1587 const bool differentHandles = ( mEventData->mRightSelectionPosition != handlePosition ) && ( mEventData->mLeftSelectionPosition != handlePosition );
1588 if( differentHandles || endOfScroll )
1590 mEventData->mUpdateHighlightBox = true;
1591 mEventData->mUpdateRightSelectionPosition = !isSmoothHandlePanEnabled;
1592 mEventData->mUpdateLeftSelectionPosition = isSmoothHandlePanEnabled;
1593 mEventData->mRightSelectionPosition = handlePosition;
1597 if( mEventData->mUpdateLeftSelectionPosition || mEventData->mUpdateRightSelectionPosition )
1599 RepositionSelectionHandles();
1601 mEventData->mScrollAfterUpdatePosition = !isSmoothHandlePanEnabled;
1604 mEventData->mDecoratorUpdated = true;
1605 } // end ( HANDLE_SCROLLING == state )
1608 void Controller::Impl::OnSelectEvent( const Event& event )
1610 if( NULL == mEventData )
1612 // Nothing to do if there is no text.
1616 if( mEventData->mSelectionEnabled )
1618 // Convert from control's coords to text's coords.
1619 const float xPosition = event.p2.mFloat - mScrollPosition.x;
1620 const float yPosition = event.p3.mFloat - mScrollPosition.y;
1622 // Calculates the logical position from the x,y coords.
1623 RepositionSelectionHandles( xPosition,
1628 void Controller::Impl::OnSelectAllEvent()
1630 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "OnSelectAllEvent mEventData->mSelectionEnabled%s \n", mEventData->mSelectionEnabled?"true":"false");
1632 if( NULL == mEventData )
1634 // Nothing to do if there is no text.
1638 if( mEventData->mSelectionEnabled )
1640 ChangeState( EventData::SELECTING );
1642 mEventData->mLeftSelectionPosition = 0u;
1643 mEventData->mRightSelectionPosition = mLogicalModel->mText.Count();
1645 mEventData->mScrollAfterUpdatePosition = true;
1646 mEventData->mUpdateLeftSelectionPosition = true;
1647 mEventData->mUpdateRightSelectionPosition = true;
1648 mEventData->mUpdateHighlightBox = true;
1652 void Controller::Impl::RetrieveSelection( std::string& selectedText, bool deleteAfterRetrieval )
1654 if( mEventData->mLeftSelectionPosition == mEventData->mRightSelectionPosition )
1656 // Nothing to select if handles are in the same place.
1657 selectedText.clear();
1661 const bool handlesCrossed = mEventData->mLeftSelectionPosition > mEventData->mRightSelectionPosition;
1663 //Get start and end position of selection
1664 const CharacterIndex startOfSelectedText = handlesCrossed ? mEventData->mRightSelectionPosition : mEventData->mLeftSelectionPosition;
1665 const Length lengthOfSelectedText = ( handlesCrossed ? mEventData->mLeftSelectionPosition : mEventData->mRightSelectionPosition ) - startOfSelectedText;
1667 Vector<Character>& utf32Characters = mLogicalModel->mText;
1668 const Length numberOfCharacters = utf32Characters.Count();
1670 // Validate the start and end selection points
1671 if( ( startOfSelectedText + lengthOfSelectedText ) <= numberOfCharacters )
1673 //Get text as a UTF8 string
1674 Utf32ToUtf8( &utf32Characters[startOfSelectedText], lengthOfSelectedText, selectedText );
1676 if( deleteAfterRetrieval ) // Only delete text if copied successfully
1678 // Keep a copy of the current input style.
1679 InputStyle currentInputStyle;
1680 currentInputStyle.Copy( mEventData->mInputStyle );
1682 // Set as input style the style of the first deleted character.
1683 mLogicalModel->RetrieveStyle( startOfSelectedText, mEventData->mInputStyle );
1685 // Compare if the input style has changed.
1686 const bool hasInputStyleChanged = !currentInputStyle.Equal( mEventData->mInputStyle );
1688 if( hasInputStyleChanged )
1690 const InputStyle::Mask styleChangedMask = currentInputStyle.GetInputStyleChangeMask( mEventData->mInputStyle );
1691 // Queue the input style changed signal.
1692 mEventData->mInputStyleChangedQueue.PushBack( styleChangedMask );
1695 mLogicalModel->UpdateTextStyleRuns( startOfSelectedText, -static_cast<int>( lengthOfSelectedText ) );
1697 // Mark the paragraphs to be updated.
1698 if( LayoutEngine::SINGLE_LINE_BOX == mLayoutEngine.GetLayout() )
1700 mTextUpdateInfo.mCharacterIndex = 0;
1701 mTextUpdateInfo.mNumberOfCharactersToRemove = mTextUpdateInfo.mPreviousNumberOfCharacters;
1702 mTextUpdateInfo.mNumberOfCharactersToAdd = mTextUpdateInfo.mPreviousNumberOfCharacters - lengthOfSelectedText;
1703 mTextUpdateInfo.mClearAll = true;
1707 mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1708 mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1711 // Delete text between handles
1712 Vector<Character>::Iterator first = utf32Characters.Begin() + startOfSelectedText;
1713 Vector<Character>::Iterator last = first + lengthOfSelectedText;
1714 utf32Characters.Erase( first, last );
1716 // Will show the cursor at the first character of the selection.
1717 mEventData->mPrimaryCursorPosition = handlesCrossed ? mEventData->mRightSelectionPosition : mEventData->mLeftSelectionPosition;
1721 // Will show the cursor at the last character of the selection.
1722 mEventData->mPrimaryCursorPosition = handlesCrossed ? mEventData->mLeftSelectionPosition : mEventData->mRightSelectionPosition;
1725 mEventData->mDecoratorUpdated = true;
1729 void Controller::Impl::ShowClipboard()
1733 mClipboard.ShowClipboard();
1737 void Controller::Impl::HideClipboard()
1739 if( mClipboard && mClipboardHideEnabled )
1741 mClipboard.HideClipboard();
1745 void Controller::Impl::SetClipboardHideEnable(bool enable)
1747 mClipboardHideEnabled = enable;
1750 bool Controller::Impl::CopyStringToClipboard( std::string& source )
1752 //Send string to clipboard
1753 return ( mClipboard && mClipboard.SetItem( source ) );
1756 void Controller::Impl::SendSelectionToClipboard( bool deleteAfterSending )
1758 std::string selectedText;
1759 RetrieveSelection( selectedText, deleteAfterSending );
1760 CopyStringToClipboard( selectedText );
1761 ChangeState( EventData::EDITING );
1764 void Controller::Impl::RequestGetTextFromClipboard()
1768 mClipboard.RequestItem();
1772 void Controller::Impl::RepositionSelectionHandles()
1774 CharacterIndex selectionStart = mEventData->mLeftSelectionPosition;
1775 CharacterIndex selectionEnd = mEventData->mRightSelectionPosition;
1777 if( selectionStart == selectionEnd )
1779 // Nothing to select if handles are in the same place.
1783 mEventData->mDecorator->ClearHighlights();
1785 const GlyphIndex* const charactersToGlyphBuffer = mVisualModel->mCharactersToGlyph.Begin();
1786 const Length* const glyphsPerCharacterBuffer = mVisualModel->mGlyphsPerCharacter.Begin();
1787 const GlyphInfo* const glyphsBuffer = mVisualModel->mGlyphs.Begin();
1788 const Vector2* const positionsBuffer = mVisualModel->mGlyphPositions.Begin();
1789 const Length* const charactersPerGlyphBuffer = mVisualModel->mCharactersPerGlyph.Begin();
1790 const CharacterIndex* const glyphToCharacterBuffer = mVisualModel->mGlyphsToCharacters.Begin();
1791 const CharacterDirection* const modelCharacterDirectionsBuffer = ( 0u != mLogicalModel->mCharacterDirections.Count() ) ? mLogicalModel->mCharacterDirections.Begin() : NULL;
1793 const bool isLastCharacter = selectionEnd >= mLogicalModel->mText.Count();
1794 const CharacterDirection startDirection = ( ( NULL == modelCharacterDirectionsBuffer ) ? false : *( modelCharacterDirectionsBuffer + selectionStart ) );
1795 const CharacterDirection endDirection = ( ( NULL == modelCharacterDirectionsBuffer ) ? false : *( modelCharacterDirectionsBuffer + ( selectionEnd - ( isLastCharacter ? 1u : 0u ) ) ) );
1797 // Swap the indices if the start is greater than the end.
1798 const bool indicesSwapped = selectionStart > selectionEnd;
1800 // Tell the decorator to flip the selection handles if needed.
1801 mEventData->mDecorator->SetSelectionHandleFlipState( indicesSwapped, startDirection, endDirection );
1803 if( indicesSwapped )
1805 std::swap( selectionStart, selectionEnd );
1808 // Get the indices to the first and last selected glyphs.
1809 const CharacterIndex selectionEndMinusOne = selectionEnd - 1u;
1810 const GlyphIndex glyphStart = *( charactersToGlyphBuffer + selectionStart );
1811 const Length numberOfGlyphs = *( glyphsPerCharacterBuffer + selectionEndMinusOne );
1812 const GlyphIndex glyphEnd = *( charactersToGlyphBuffer + selectionEndMinusOne ) + ( ( numberOfGlyphs > 0 ) ? numberOfGlyphs - 1u : 0u );
1814 // Get the lines where the glyphs are laid-out.
1815 const LineRun* lineRun = mVisualModel->mLines.Begin();
1817 LineIndex lineIndex = 0u;
1818 Length numberOfLines = 0u;
1819 mVisualModel->GetNumberOfLines( glyphStart,
1820 1u + glyphEnd - glyphStart,
1823 const LineIndex firstLineIndex = lineIndex;
1825 // Create the structure to store some selection box info.
1826 Vector<SelectionBoxInfo> selectionBoxLinesInfo;
1827 selectionBoxLinesInfo.Resize( numberOfLines );
1829 SelectionBoxInfo* selectionBoxInfo = selectionBoxLinesInfo.Begin();
1830 selectionBoxInfo->minX = MAX_FLOAT;
1831 selectionBoxInfo->maxX = MIN_FLOAT;
1833 // Keep the min and max 'x' position to calculate the size and position of the highlighed text.
1834 float minHighlightX = std::numeric_limits<float>::max();
1835 float maxHighlightX = std::numeric_limits<float>::min();
1837 Vector2 highLightPosition; // The highlight position in decorator's coords.
1839 // Retrieve the first line and get the line's vertical offset, the line's height and the index to the last glyph.
1841 // The line's vertical offset of all the lines before the line where the first glyph is laid-out.
1842 selectionBoxInfo->lineOffset = CalculateLineOffset( mVisualModel->mLines,
1845 // Transform to decorator's (control) coords.
1846 selectionBoxInfo->lineOffset += mScrollPosition.y;
1848 lineRun += firstLineIndex;
1850 // The line height is the addition of the line ascender and the line descender.
1851 // However, the line descender has a negative value, hence the subtraction.
1852 selectionBoxInfo->lineHeight = lineRun->ascender - lineRun->descender;
1854 GlyphIndex lastGlyphOfLine = lineRun->glyphRun.glyphIndex + lineRun->glyphRun.numberOfGlyphs - 1u;
1856 // Check if the first glyph is a ligature that must be broken like Latin ff, fi, or Arabic ﻻ, etc which needs special code.
1857 const Length numberOfCharactersStart = *( charactersPerGlyphBuffer + glyphStart );
1858 bool splitStartGlyph = ( numberOfCharactersStart > 1u ) && HasLigatureMustBreak( mLogicalModel->GetScript( selectionStart ) );
1860 // Check if the last glyph is a ligature that must be broken like Latin ff, fi, or Arabic ﻻ, etc which needs special code.
1861 const Length numberOfCharactersEnd = *( charactersPerGlyphBuffer + glyphEnd );
1862 bool splitEndGlyph = ( glyphStart != glyphEnd ) && ( numberOfCharactersEnd > 1u ) && HasLigatureMustBreak( mLogicalModel->GetScript( selectionEndMinusOne ) );
1864 // The number of quads of the selection box.
1865 const unsigned int numberOfQuads = 1u + ( glyphEnd - glyphStart ) + ( ( numberOfLines > 1u ) ? 2u * numberOfLines : 0u );
1866 mEventData->mDecorator->ResizeHighlightQuads( numberOfQuads );
1868 // Count the actual number of quads.
1869 unsigned int actualNumberOfQuads = 0u;
1872 // Traverse the glyphs.
1873 for( GlyphIndex index = glyphStart; index <= glyphEnd; ++index )
1875 const GlyphInfo& glyph = *( glyphsBuffer + index );
1876 const Vector2& position = *( positionsBuffer + index );
1878 if( splitStartGlyph )
1880 // If the first glyph is a ligature that must be broken it may be needed to add only part of the glyph to the highlight box.
1882 const float glyphAdvance = glyph.advance / static_cast<float>( numberOfCharactersStart );
1883 const CharacterIndex interGlyphIndex = selectionStart - *( glyphToCharacterBuffer + glyphStart );
1884 // Get the direction of the character.
1885 CharacterDirection isCurrentRightToLeft = false;
1886 if( NULL != modelCharacterDirectionsBuffer ) // If modelCharacterDirectionsBuffer is NULL, it means the whole text is left to right.
1888 isCurrentRightToLeft = *( modelCharacterDirectionsBuffer + selectionStart );
1891 // The end point could be in the middle of the ligature.
1892 // Calculate the number of characters selected.
1893 const Length numberOfCharacters = ( glyphStart == glyphEnd ) ? ( selectionEnd - selectionStart ) : ( numberOfCharactersStart - interGlyphIndex );
1895 quad.x = lineRun->alignmentOffset + position.x - glyph.xBearing + mScrollPosition.x + glyphAdvance * static_cast<float>( isCurrentRightToLeft ? ( numberOfCharactersStart - interGlyphIndex - numberOfCharacters ) : interGlyphIndex );
1896 quad.y = selectionBoxInfo->lineOffset;
1897 quad.z = quad.x + static_cast<float>( numberOfCharacters ) * glyphAdvance;
1898 quad.w = selectionBoxInfo->lineOffset + selectionBoxInfo->lineHeight;
1900 // Store the min and max 'x' for each line.
1901 selectionBoxInfo->minX = std::min( selectionBoxInfo->minX, quad.x );
1902 selectionBoxInfo->maxX = std::max( selectionBoxInfo->maxX, quad.z );
1904 mEventData->mDecorator->AddHighlight( actualNumberOfQuads, quad );
1905 ++actualNumberOfQuads;
1907 splitStartGlyph = false;
1911 if( splitEndGlyph && ( index == glyphEnd ) )
1913 // Equally, if the last glyph is a ligature that must be broken it may be needed to add only part of the glyph to the highlight box.
1915 const float glyphAdvance = glyph.advance / static_cast<float>( numberOfCharactersEnd );
1916 const CharacterIndex interGlyphIndex = selectionEnd - *( glyphToCharacterBuffer + glyphEnd );
1917 // Get the direction of the character.
1918 CharacterDirection isCurrentRightToLeft = false;
1919 if( NULL != modelCharacterDirectionsBuffer ) // If modelCharacterDirectionsBuffer is NULL, it means the whole text is left to right.
1921 isCurrentRightToLeft = *( modelCharacterDirectionsBuffer + selectionEnd );
1924 const Length numberOfCharacters = numberOfCharactersEnd - interGlyphIndex;
1926 quad.x = lineRun->alignmentOffset + position.x - glyph.xBearing + mScrollPosition.x + ( isCurrentRightToLeft ? ( glyphAdvance * static_cast<float>( numberOfCharacters ) ) : 0.f );
1927 quad.y = selectionBoxInfo->lineOffset;
1928 quad.z = quad.x + static_cast<float>( interGlyphIndex ) * glyphAdvance;
1929 quad.w = quad.y + selectionBoxInfo->lineHeight;
1931 // Store the min and max 'x' for each line.
1932 selectionBoxInfo->minX = std::min( selectionBoxInfo->minX, quad.x );
1933 selectionBoxInfo->maxX = std::max( selectionBoxInfo->maxX, quad.z );
1935 mEventData->mDecorator->AddHighlight( actualNumberOfQuads,
1937 ++actualNumberOfQuads;
1939 splitEndGlyph = false;
1943 quad.x = lineRun->alignmentOffset + position.x - glyph.xBearing + mScrollPosition.x;
1944 quad.y = selectionBoxInfo->lineOffset;
1945 quad.z = quad.x + glyph.advance;
1946 quad.w = quad.y + selectionBoxInfo->lineHeight;
1948 // Store the min and max 'x' for each line.
1949 selectionBoxInfo->minX = std::min( selectionBoxInfo->minX, quad.x );
1950 selectionBoxInfo->maxX = std::max( selectionBoxInfo->maxX, quad.z );
1952 mEventData->mDecorator->AddHighlight( actualNumberOfQuads,
1954 ++actualNumberOfQuads;
1956 // Whether to retrieve the next line.
1957 if( index == lastGlyphOfLine )
1959 // Retrieve the next line.
1962 // Get the last glyph of the new line.
1963 lastGlyphOfLine = lineRun->glyphRun.glyphIndex + lineRun->glyphRun.numberOfGlyphs - 1u;
1966 if( lineIndex < firstLineIndex + numberOfLines )
1968 // Keep the offset and height of the current selection box.
1969 const float currentLineOffset = selectionBoxInfo->lineOffset;
1970 const float currentLineHeight = selectionBoxInfo->lineHeight;
1972 // Get the selection box info for the next line.
1975 selectionBoxInfo->minX = MAX_FLOAT;
1976 selectionBoxInfo->maxX = MIN_FLOAT;
1978 // Update the line's vertical offset.
1979 selectionBoxInfo->lineOffset = currentLineOffset + currentLineHeight;
1981 // The line height is the addition of the line ascender and the line descender.
1982 // However, the line descender has a negative value, hence the subtraction.
1983 selectionBoxInfo->lineHeight = lineRun->ascender - lineRun->descender;
1988 // Traverses all the lines and updates the min and max 'x' positions and the total height.
1989 // The final width is calculated after 'boxifying' the selection.
1990 for( Vector<SelectionBoxInfo>::ConstIterator it = selectionBoxLinesInfo.Begin(),
1991 endIt = selectionBoxLinesInfo.End();
1995 const SelectionBoxInfo& info = *it;
1997 // Update the size of the highlighted text.
1998 highLightSize.height += info.lineHeight;
1999 minHighlightX = std::min( minHighlightX, info.minX );
2000 maxHighlightX = std::max( maxHighlightX, info.maxX );
2003 // Add extra geometry to 'boxify' the selection.
2005 if( 1u < numberOfLines )
2007 // Boxify the first line.
2008 lineRun = mVisualModel->mLines.Begin() + firstLineIndex;
2009 const SelectionBoxInfo& firstSelectionBoxLineInfo = *( selectionBoxLinesInfo.Begin() );
2011 bool boxifyBegin = ( LTR != lineRun->direction ) && ( LTR != startDirection );
2012 bool boxifyEnd = ( LTR == lineRun->direction ) && ( LTR == startDirection );
2017 quad.y = firstSelectionBoxLineInfo.lineOffset;
2018 quad.z = firstSelectionBoxLineInfo.minX;
2019 quad.w = firstSelectionBoxLineInfo.lineOffset + firstSelectionBoxLineInfo.lineHeight;
2021 // Boxify at the beginning of the line.
2022 mEventData->mDecorator->AddHighlight( actualNumberOfQuads,
2024 ++actualNumberOfQuads;
2026 // Update the size of the highlighted text.
2027 minHighlightX = 0.f;
2032 quad.x = firstSelectionBoxLineInfo.maxX;
2033 quad.y = firstSelectionBoxLineInfo.lineOffset;
2034 quad.z = mVisualModel->mControlSize.width;
2035 quad.w = firstSelectionBoxLineInfo.lineOffset + firstSelectionBoxLineInfo.lineHeight;
2037 // Boxify at the end of the line.
2038 mEventData->mDecorator->AddHighlight( actualNumberOfQuads,
2040 ++actualNumberOfQuads;
2042 // Update the size of the highlighted text.
2043 maxHighlightX = mVisualModel->mControlSize.width;
2046 // Boxify the central lines.
2047 if( 2u < numberOfLines )
2049 for( Vector<SelectionBoxInfo>::ConstIterator it = selectionBoxLinesInfo.Begin() + 1u,
2050 endIt = selectionBoxLinesInfo.End() - 1u;
2054 const SelectionBoxInfo& info = *it;
2057 quad.y = info.lineOffset;
2059 quad.w = info.lineOffset + info.lineHeight;
2061 mEventData->mDecorator->AddHighlight( actualNumberOfQuads,
2063 ++actualNumberOfQuads;
2066 quad.y = info.lineOffset;
2067 quad.z = mVisualModel->mControlSize.width;
2068 quad.w = info.lineOffset + info.lineHeight;
2070 mEventData->mDecorator->AddHighlight( actualNumberOfQuads,
2072 ++actualNumberOfQuads;
2075 // Update the size of the highlighted text.
2076 minHighlightX = 0.f;
2077 maxHighlightX = mVisualModel->mControlSize.width;
2080 // Boxify the last line.
2081 lineRun = mVisualModel->mLines.Begin() + firstLineIndex + numberOfLines - 1u;
2082 const SelectionBoxInfo& lastSelectionBoxLineInfo = *( selectionBoxLinesInfo.End() - 1u );
2084 boxifyBegin = ( LTR == lineRun->direction ) && ( LTR == endDirection );
2085 boxifyEnd = ( LTR != lineRun->direction ) && ( LTR != endDirection );
2090 quad.y = lastSelectionBoxLineInfo.lineOffset;
2091 quad.z = lastSelectionBoxLineInfo.minX;
2092 quad.w = lastSelectionBoxLineInfo.lineOffset + lastSelectionBoxLineInfo.lineHeight;
2094 // Boxify at the beginning of the line.
2095 mEventData->mDecorator->AddHighlight( actualNumberOfQuads,
2097 ++actualNumberOfQuads;
2099 // Update the size of the highlighted text.
2100 minHighlightX = 0.f;
2105 quad.x = lastSelectionBoxLineInfo.maxX;
2106 quad.y = lastSelectionBoxLineInfo.lineOffset;
2107 quad.z = mVisualModel->mControlSize.width;
2108 quad.w = lastSelectionBoxLineInfo.lineOffset + lastSelectionBoxLineInfo.lineHeight;
2110 // Boxify at the end of the line.
2111 mEventData->mDecorator->AddHighlight( actualNumberOfQuads,
2113 ++actualNumberOfQuads;
2115 // Update the size of the highlighted text.
2116 maxHighlightX = mVisualModel->mControlSize.width;
2120 // Set the actual number of quads.
2121 mEventData->mDecorator->ResizeHighlightQuads( actualNumberOfQuads );
2123 // Sets the highlight's size and position. In decorator's coords.
2124 // The highlight's height has been calculated above (before 'boxifying' the highlight).
2125 highLightSize.width = maxHighlightX - minHighlightX;
2127 highLightPosition.x = minHighlightX;
2128 const SelectionBoxInfo& firstSelectionBoxLineInfo = *( selectionBoxLinesInfo.Begin() );
2129 highLightPosition.y = firstSelectionBoxLineInfo.lineOffset;
2131 mEventData->mDecorator->SetHighLightBox( highLightPosition, highLightSize );
2133 if( !mEventData->mDecorator->IsSmoothHandlePanEnabled() )
2135 CursorInfo primaryCursorInfo;
2136 GetCursorPosition( mEventData->mLeftSelectionPosition,
2137 primaryCursorInfo );
2139 const Vector2 primaryPosition = primaryCursorInfo.primaryPosition + mScrollPosition;
2141 mEventData->mDecorator->SetPosition( LEFT_SELECTION_HANDLE,
2143 primaryCursorInfo.lineOffset + mScrollPosition.y,
2144 primaryCursorInfo.lineHeight );
2146 CursorInfo secondaryCursorInfo;
2147 GetCursorPosition( mEventData->mRightSelectionPosition,
2148 secondaryCursorInfo );
2150 const Vector2 secondaryPosition = secondaryCursorInfo.primaryPosition + mScrollPosition;
2152 mEventData->mDecorator->SetPosition( RIGHT_SELECTION_HANDLE,
2153 secondaryPosition.x,
2154 secondaryCursorInfo.lineOffset + mScrollPosition.y,
2155 secondaryCursorInfo.lineHeight );
2158 // Cursor to be positioned at end of selection so if selection interrupted and edit mode restarted the cursor will be at end of selection
2159 mEventData->mPrimaryCursorPosition = ( indicesSwapped ) ? mEventData->mLeftSelectionPosition : mEventData->mRightSelectionPosition;
2161 // Set the flag to update the decorator.
2162 mEventData->mDecoratorUpdated = true;
2165 void Controller::Impl::RepositionSelectionHandles( float visualX, float visualY )
2167 if( NULL == mEventData )
2169 // Nothing to do if there is no text input.
2173 if( IsShowingPlaceholderText() )
2175 // Nothing to do if there is the place-holder text.
2179 const Length numberOfGlyphs = mVisualModel->mGlyphs.Count();
2180 const Length numberOfLines = mVisualModel->mLines.Count();
2181 if( ( 0 == numberOfGlyphs ) ||
2182 ( 0 == numberOfLines ) )
2184 // Nothing to do if there is no text.
2188 // Find which word was selected
2189 CharacterIndex selectionStart( 0 );
2190 CharacterIndex selectionEnd( 0 );
2191 const bool indicesFound = FindSelectionIndices( mVisualModel,
2198 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "%p selectionStart %d selectionEnd %d\n", this, selectionStart, selectionEnd );
2202 ChangeState( EventData::SELECTING );
2204 mEventData->mLeftSelectionPosition = selectionStart;
2205 mEventData->mRightSelectionPosition = selectionEnd;
2207 mEventData->mUpdateLeftSelectionPosition = true;
2208 mEventData->mUpdateRightSelectionPosition = true;
2209 mEventData->mUpdateHighlightBox = true;
2211 // It may happen an IMF commit event arrives before the selection event
2212 // if the IMF manager is in pre-edit state. The commit event will set the
2213 // mEventData->mUpdateCursorPosition flag to true. If it's not set back
2214 // to false, the highlight box won't be updated.
2215 mEventData->mUpdateCursorPosition = false;
2217 mEventData->mScrollAfterUpdatePosition = ( mEventData->mLeftSelectionPosition != mEventData->mRightSelectionPosition );
2221 // Nothing to select. i.e. a white space, out of bounds
2222 ChangeState( EventData::EDITING );
2224 mEventData->mPrimaryCursorPosition = selectionEnd;
2226 mEventData->mUpdateCursorPosition = true;
2227 mEventData->mUpdateGrabHandlePosition = true;
2228 mEventData->mScrollAfterUpdatePosition = true;
2229 mEventData->mUpdateInputStyle = true;
2233 void Controller::Impl::SetPopupButtons()
2236 * Sets the Popup buttons to be shown depending on State.
2238 * If SELECTING : CUT & COPY + ( PASTE & CLIPBOARD if content available to paste )
2240 * If EDITING_WITH_POPUP : SELECT & SELECT_ALL
2243 TextSelectionPopup::Buttons buttonsToShow = TextSelectionPopup::NONE;
2245 if( EventData::SELECTING == mEventData->mState )
2247 buttonsToShow = TextSelectionPopup::Buttons( TextSelectionPopup::CUT | TextSelectionPopup::COPY );
2249 if( !IsClipboardEmpty() )
2251 buttonsToShow = TextSelectionPopup::Buttons ( ( buttonsToShow | TextSelectionPopup::PASTE ) );
2252 buttonsToShow = TextSelectionPopup::Buttons ( ( buttonsToShow | TextSelectionPopup::CLIPBOARD ) );
2255 if( !mEventData->mAllTextSelected )
2257 buttonsToShow = TextSelectionPopup::Buttons ( ( buttonsToShow | TextSelectionPopup::SELECT_ALL ) );
2260 else if( EventData::EDITING_WITH_POPUP == mEventData->mState )
2262 if( mLogicalModel->mText.Count() && !IsShowingPlaceholderText() )
2264 buttonsToShow = TextSelectionPopup::Buttons( TextSelectionPopup::SELECT | TextSelectionPopup::SELECT_ALL );
2267 if( !IsClipboardEmpty() )
2269 buttonsToShow = TextSelectionPopup::Buttons ( ( buttonsToShow | TextSelectionPopup::PASTE ) );
2270 buttonsToShow = TextSelectionPopup::Buttons ( ( buttonsToShow | TextSelectionPopup::CLIPBOARD ) );
2273 else if( EventData::EDITING_WITH_PASTE_POPUP == mEventData->mState )
2275 if ( !IsClipboardEmpty() )
2277 buttonsToShow = TextSelectionPopup::Buttons ( ( buttonsToShow | TextSelectionPopup::PASTE ) );
2278 buttonsToShow = TextSelectionPopup::Buttons ( ( buttonsToShow | TextSelectionPopup::CLIPBOARD ) );
2282 mEventData->mDecorator->SetEnabledPopupButtons( buttonsToShow );
2285 void Controller::Impl::ChangeState( EventData::State newState )
2287 if( NULL == mEventData )
2289 // Nothing to do if there is no text input.
2293 DALI_LOG_INFO( gLogFilter, Debug::General, "ChangeState state:%d newstate:%d\n", mEventData->mState, newState );
2295 if( mEventData->mState != newState )
2297 mEventData->mPreviousState = mEventData->mState;
2298 mEventData->mState = newState;
2300 switch( mEventData->mState )
2302 case EventData::INACTIVE:
2304 mEventData->mDecorator->SetActiveCursor( ACTIVE_CURSOR_NONE );
2305 mEventData->mDecorator->StopCursorBlink();
2306 mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2307 mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2308 mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2309 mEventData->mDecorator->SetHighlightActive( false );
2310 mEventData->mDecorator->SetPopupActive( false );
2311 mEventData->mDecoratorUpdated = true;
2314 case EventData::INTERRUPTED:
2316 mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2317 mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2318 mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2319 mEventData->mDecorator->SetHighlightActive( false );
2320 mEventData->mDecorator->SetPopupActive( false );
2321 mEventData->mDecoratorUpdated = true;
2324 case EventData::SELECTING:
2326 mEventData->mDecorator->SetActiveCursor( ACTIVE_CURSOR_NONE );
2327 mEventData->mDecorator->StopCursorBlink();
2328 mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2329 mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, true );
2330 mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, true );
2331 mEventData->mDecorator->SetHighlightActive( true );
2332 if( mEventData->mGrabHandlePopupEnabled )
2335 mEventData->mDecorator->SetPopupActive( true );
2337 mEventData->mDecoratorUpdated = true;
2340 case EventData::EDITING:
2342 mEventData->mDecorator->SetActiveCursor( ACTIVE_CURSOR_PRIMARY );
2343 if( mEventData->mCursorBlinkEnabled )
2345 mEventData->mDecorator->StartCursorBlink();
2347 // Grab handle is not shown until a tap is received whilst EDITING
2348 mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2349 mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2350 mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2351 mEventData->mDecorator->SetHighlightActive( false );
2352 if( mEventData->mGrabHandlePopupEnabled )
2354 mEventData->mDecorator->SetPopupActive( false );
2356 mEventData->mDecoratorUpdated = true;
2359 case EventData::EDITING_WITH_POPUP:
2361 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "EDITING_WITH_POPUP \n", newState );
2363 mEventData->mDecorator->SetActiveCursor( ACTIVE_CURSOR_PRIMARY );
2364 if( mEventData->mCursorBlinkEnabled )
2366 mEventData->mDecorator->StartCursorBlink();
2368 if( mEventData->mSelectionEnabled )
2370 mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2371 mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2372 mEventData->mDecorator->SetHighlightActive( false );
2376 mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, true );
2378 if( mEventData->mGrabHandlePopupEnabled )
2381 mEventData->mDecorator->SetPopupActive( true );
2383 mEventData->mDecoratorUpdated = true;
2386 case EventData::EDITING_WITH_GRAB_HANDLE:
2388 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "EDITING_WITH_GRAB_HANDLE \n", newState );
2390 mEventData->mDecorator->SetActiveCursor( ACTIVE_CURSOR_PRIMARY );
2391 if( mEventData->mCursorBlinkEnabled )
2393 mEventData->mDecorator->StartCursorBlink();
2395 // Grab handle is not shown until a tap is received whilst EDITING
2396 mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, true );
2397 mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2398 mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2399 mEventData->mDecorator->SetHighlightActive( false );
2400 if( mEventData->mGrabHandlePopupEnabled )
2402 mEventData->mDecorator->SetPopupActive( false );
2404 mEventData->mDecoratorUpdated = true;
2407 case EventData::SELECTION_HANDLE_PANNING:
2409 mEventData->mDecorator->SetActiveCursor( ACTIVE_CURSOR_NONE );
2410 mEventData->mDecorator->StopCursorBlink();
2411 mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2412 mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, true );
2413 mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, true );
2414 mEventData->mDecorator->SetHighlightActive( true );
2415 if( mEventData->mGrabHandlePopupEnabled )
2417 mEventData->mDecorator->SetPopupActive( false );
2419 mEventData->mDecoratorUpdated = true;
2422 case EventData::GRAB_HANDLE_PANNING:
2424 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GRAB_HANDLE_PANNING \n", newState );
2426 mEventData->mDecorator->SetActiveCursor( ACTIVE_CURSOR_PRIMARY );
2427 if( mEventData->mCursorBlinkEnabled )
2429 mEventData->mDecorator->StartCursorBlink();
2431 mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, true );
2432 mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2433 mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2434 mEventData->mDecorator->SetHighlightActive( false );
2435 if( mEventData->mGrabHandlePopupEnabled )
2437 mEventData->mDecorator->SetPopupActive( false );
2439 mEventData->mDecoratorUpdated = true;
2442 case EventData::EDITING_WITH_PASTE_POPUP:
2444 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "EDITING_WITH_PASTE_POPUP \n", newState );
2446 mEventData->mDecorator->SetActiveCursor( ACTIVE_CURSOR_PRIMARY );
2447 if( mEventData->mCursorBlinkEnabled )
2449 mEventData->mDecorator->StartCursorBlink();
2452 mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, true );
2453 mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2454 mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2455 mEventData->mDecorator->SetHighlightActive( false );
2457 if( mEventData->mGrabHandlePopupEnabled )
2460 mEventData->mDecorator->SetPopupActive( true );
2462 mEventData->mDecoratorUpdated = true;
2465 case EventData::TEXT_PANNING:
2467 mEventData->mDecorator->SetActiveCursor( ACTIVE_CURSOR_NONE );
2468 mEventData->mDecorator->StopCursorBlink();
2469 mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2470 if( mEventData->mDecorator->IsHandleActive( LEFT_SELECTION_HANDLE ) ||
2471 mEventData->mDecorator->IsHandleActive( RIGHT_SELECTION_HANDLE ) )
2473 mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2474 mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2475 mEventData->mDecorator->SetHighlightActive( true );
2478 if( mEventData->mGrabHandlePopupEnabled )
2480 mEventData->mDecorator->SetPopupActive( false );
2483 mEventData->mDecoratorUpdated = true;
2490 void Controller::Impl::GetCursorPosition( CharacterIndex logical,
2491 CursorInfo& cursorInfo )
2493 if( !IsShowingRealText() )
2495 // Do not want to use the place-holder text to set the cursor position.
2497 // Use the line's height of the font's family set to set the cursor's size.
2498 // If there is no font's family set, use the default font.
2499 // Use the current alignment to place the cursor at the beginning, center or end of the box.
2501 cursorInfo.lineOffset = 0.f;
2502 cursorInfo.lineHeight = GetDefaultFontLineHeight();
2503 cursorInfo.primaryCursorHeight = cursorInfo.lineHeight;
2505 switch( mLayoutEngine.GetHorizontalAlignment() )
2507 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
2509 cursorInfo.primaryPosition.x = 0.f;
2512 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
2514 cursorInfo.primaryPosition.x = floorf( 0.5f * mVisualModel->mControlSize.width );
2517 case LayoutEngine::HORIZONTAL_ALIGN_END:
2519 cursorInfo.primaryPosition.x = mVisualModel->mControlSize.width - mEventData->mDecorator->GetCursorWidth();
2524 // Nothing else to do.
2528 const bool isMultiLine = ( LayoutEngine::MULTI_LINE_BOX == mLayoutEngine.GetLayout() );
2529 GetCursorPositionParameters parameters;
2530 parameters.visualModel = mVisualModel;
2531 parameters.logicalModel = mLogicalModel;
2532 parameters.metrics = mMetrics;
2533 parameters.logical = logical;
2534 parameters.isMultiline = isMultiLine;
2536 Text::GetCursorPosition( parameters,
2541 // If the text is editable and multi-line, the cursor position after a white space shouldn't exceed the boundaries of the text control.
2543 // Note the white spaces laid-out at the end of the line might exceed the boundaries of the control.
2544 // The reason is a wrapped line must not start with a white space so they are laid-out at the end of the line.
2546 if( 0.f > cursorInfo.primaryPosition.x )
2548 cursorInfo.primaryPosition.x = 0.f;
2551 const float edgeWidth = mVisualModel->mControlSize.width - static_cast<float>( mEventData->mDecorator->GetCursorWidth() );
2552 if( cursorInfo.primaryPosition.x > edgeWidth )
2554 cursorInfo.primaryPosition.x = edgeWidth;
2559 CharacterIndex Controller::Impl::CalculateNewCursorIndex( CharacterIndex index ) const
2561 if( NULL == mEventData )
2563 // Nothing to do if there is no text input.
2567 CharacterIndex cursorIndex = mEventData->mPrimaryCursorPosition;
2569 const GlyphIndex* const charactersToGlyphBuffer = mVisualModel->mCharactersToGlyph.Begin();
2570 const Length* const charactersPerGlyphBuffer = mVisualModel->mCharactersPerGlyph.Begin();
2572 GlyphIndex glyphIndex = *( charactersToGlyphBuffer + index );
2573 Length numberOfCharacters = *( charactersPerGlyphBuffer + glyphIndex );
2575 if( numberOfCharacters > 1u )
2577 const Script script = mLogicalModel->GetScript( index );
2578 if( HasLigatureMustBreak( script ) )
2580 // Prevents to jump the whole Latin ligatures like fi, ff, or Arabic ﻻ, ...
2581 numberOfCharacters = 1u;
2586 while( 0u == numberOfCharacters )
2589 numberOfCharacters = *( charactersPerGlyphBuffer + glyphIndex );
2593 if( index < mEventData->mPrimaryCursorPosition )
2595 cursorIndex -= numberOfCharacters;
2599 cursorIndex += numberOfCharacters;
2602 // Will update the cursor hook position.
2603 mEventData->mUpdateCursorHookPosition = true;
2608 void Controller::Impl::UpdateCursorPosition( const CursorInfo& cursorInfo )
2610 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::UpdateCursorPosition %p\n", this );
2611 if( NULL == mEventData )
2613 // Nothing to do if there is no text input.
2614 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::UpdateCursorPosition no event data\n" );
2618 const Vector2 cursorPosition = cursorInfo.primaryPosition + mScrollPosition;
2620 // Sets the cursor position.
2621 mEventData->mDecorator->SetPosition( PRIMARY_CURSOR,
2624 cursorInfo.primaryCursorHeight,
2625 cursorInfo.lineHeight );
2626 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Primary cursor position: %f,%f\n", cursorPosition.x, cursorPosition.y );
2628 if( mEventData->mUpdateGrabHandlePosition )
2630 // Sets the grab handle position.
2631 mEventData->mDecorator->SetPosition( GRAB_HANDLE,
2633 cursorInfo.lineOffset + mScrollPosition.y,
2634 cursorInfo.lineHeight );
2637 if( cursorInfo.isSecondaryCursor )
2639 mEventData->mDecorator->SetPosition( SECONDARY_CURSOR,
2640 cursorInfo.secondaryPosition.x + mScrollPosition.x,
2641 cursorInfo.secondaryPosition.y + mScrollPosition.y,
2642 cursorInfo.secondaryCursorHeight,
2643 cursorInfo.lineHeight );
2644 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Secondary cursor position: %f,%f\n", cursorInfo.secondaryPosition.x + mScrollPosition.x, cursorInfo.secondaryPosition.y + mScrollPosition.y );
2647 // Set which cursors are active according the state.
2648 if( EventData::IsEditingState( mEventData->mState ) || ( EventData::GRAB_HANDLE_PANNING == mEventData->mState ) )
2650 if( cursorInfo.isSecondaryCursor )
2652 mEventData->mDecorator->SetActiveCursor( ACTIVE_CURSOR_BOTH );
2656 mEventData->mDecorator->SetActiveCursor( ACTIVE_CURSOR_PRIMARY );
2661 mEventData->mDecorator->SetActiveCursor( ACTIVE_CURSOR_NONE );
2664 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::UpdateCursorPosition\n" );
2667 void Controller::Impl::UpdateSelectionHandle( HandleType handleType,
2668 const CursorInfo& cursorInfo )
2670 if( ( LEFT_SELECTION_HANDLE != handleType ) &&
2671 ( RIGHT_SELECTION_HANDLE != handleType ) )
2676 const Vector2 cursorPosition = cursorInfo.primaryPosition + mScrollPosition;
2678 // Sets the handle's position.
2679 mEventData->mDecorator->SetPosition( handleType,
2681 cursorInfo.lineOffset + mScrollPosition.y,
2682 cursorInfo.lineHeight );
2684 // If selection handle at start of the text and other at end of the text then all text is selected.
2685 const CharacterIndex startOfSelection = std::min( mEventData->mLeftSelectionPosition, mEventData->mRightSelectionPosition );
2686 const CharacterIndex endOfSelection = std::max ( mEventData->mLeftSelectionPosition, mEventData->mRightSelectionPosition );
2687 mEventData->mAllTextSelected = ( startOfSelection == 0 ) && ( endOfSelection == mLogicalModel->mText.Count() );
2690 void Controller::Impl::ClampHorizontalScroll( const Vector2& layoutSize )
2692 // Clamp between -space & -alignment offset.
2694 if( layoutSize.width > mVisualModel->mControlSize.width )
2696 const float space = ( layoutSize.width - mVisualModel->mControlSize.width ) + mAlignmentOffset;
2697 mScrollPosition.x = ( mScrollPosition.x < -space ) ? -space : mScrollPosition.x;
2698 mScrollPosition.x = ( mScrollPosition.x > -mAlignmentOffset ) ? -mAlignmentOffset : mScrollPosition.x;
2700 mEventData->mDecoratorUpdated = true;
2704 mScrollPosition.x = 0.f;
2708 void Controller::Impl::ClampVerticalScroll( const Vector2& layoutSize )
2710 if( LayoutEngine::SINGLE_LINE_BOX == mLayoutEngine.GetLayout() )
2712 // Nothing to do if the text is single line.
2716 // Clamp between -space & 0.
2717 if( layoutSize.height > mVisualModel->mControlSize.height )
2719 const float space = ( layoutSize.height - mVisualModel->mControlSize.height );
2720 mScrollPosition.y = ( mScrollPosition.y < -space ) ? -space : mScrollPosition.y;
2721 mScrollPosition.y = ( mScrollPosition.y > 0.f ) ? 0.f : mScrollPosition.y;
2723 mEventData->mDecoratorUpdated = true;
2727 mScrollPosition.y = 0.f;
2731 void Controller::Impl::ScrollToMakePositionVisible( const Vector2& position, float lineHeight )
2733 const float cursorWidth = mEventData->mDecorator ? static_cast<float>( mEventData->mDecorator->GetCursorWidth() ) : 0.f;
2735 // position is in actor's coords.
2736 const float positionEndX = position.x + cursorWidth;
2737 const float positionEndY = position.y + lineHeight;
2739 // Transform the position to decorator coords.
2740 const float decoratorPositionBeginX = position.x + mScrollPosition.x;
2741 const float decoratorPositionEndX = positionEndX + mScrollPosition.x;
2743 const float decoratorPositionBeginY = position.y + mScrollPosition.y;
2744 const float decoratorPositionEndY = positionEndY + mScrollPosition.y;
2746 if( decoratorPositionBeginX < 0.f )
2748 mScrollPosition.x = -position.x;
2750 else if( decoratorPositionEndX > mVisualModel->mControlSize.width )
2752 mScrollPosition.x = mVisualModel->mControlSize.width - positionEndX;
2755 if( LayoutEngine::MULTI_LINE_BOX == mLayoutEngine.GetLayout() )
2757 if( decoratorPositionBeginY < 0.f )
2759 mScrollPosition.y = -position.y;
2761 else if( decoratorPositionEndY > mVisualModel->mControlSize.height )
2763 mScrollPosition.y = mVisualModel->mControlSize.height - positionEndY;
2768 void Controller::Impl::ScrollTextToMatchCursor( const CursorInfo& cursorInfo )
2770 // Get the current cursor position in decorator coords.
2771 const Vector2& currentCursorPosition = mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2773 // Calculate the offset to match the cursor position before the character was deleted.
2774 mScrollPosition.x = currentCursorPosition.x - cursorInfo.primaryPosition.x;
2775 ClampHorizontalScroll( mVisualModel->GetLayoutSize() );
2777 if( LayoutEngine::MULTI_LINE_BOX == mLayoutEngine.GetLayout() )
2779 mScrollPosition.y = currentCursorPosition.y - cursorInfo.lineOffset;
2780 ClampVerticalScroll( mVisualModel->GetLayoutSize() );
2783 // Makes the new cursor position visible if needed.
2784 ScrollToMakePositionVisible( cursorInfo.primaryPosition, cursorInfo.lineHeight );
2787 void Controller::Impl::RequestRelayout()
2789 if( NULL != mControlInterface )
2791 mControlInterface->RequestTextRelayout();
2797 } // namespace Toolkit