fix issue with text size with negative line spacing
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller-impl-event-handler.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/text-controller-impl-event-handler.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/adaptor-framework/key.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/text/cursor-helper-functions.h>
27 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
28
29 using namespace Dali;
30
31 namespace
32 {
33 #if defined(DEBUG_ENABLED)
34 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
35 #endif
36
37 } // namespace
38
39 namespace Dali
40 {
41 namespace Toolkit
42 {
43 namespace Text
44 {
45 bool ControllerImplEventHandler::ProcessInputEvents(Controller::Impl& impl)
46 {
47   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::ProcessInputEvents\n");
48
49   EventData*& eventData = impl.mEventData;
50   if(NULL == eventData)
51   {
52     // Nothing to do if there is no text input.
53     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::ProcessInputEvents no event data\n");
54     return false;
55   }
56
57   unsigned int oldPos = eventData->mPrimaryCursorPosition;
58
59   if(eventData->mDecorator)
60   {
61     for(std::vector<Event>::iterator iter = eventData->mEventQueue.begin();
62         iter != eventData->mEventQueue.end();
63         ++iter)
64     {
65       switch(iter->type)
66       {
67         case Event::CURSOR_KEY_EVENT:
68         {
69           OnCursorKeyEvent(impl, *iter);
70           break;
71         }
72         case Event::TAP_EVENT:
73         {
74           OnTapEvent(impl, *iter);
75           break;
76         }
77         case Event::LONG_PRESS_EVENT:
78         {
79           OnLongPressEvent(impl, *iter);
80           break;
81         }
82         case Event::PAN_EVENT:
83         {
84           OnPanEvent(impl, *iter);
85           break;
86         }
87         case Event::GRAB_HANDLE_EVENT:
88         case Event::LEFT_SELECTION_HANDLE_EVENT:
89         case Event::RIGHT_SELECTION_HANDLE_EVENT: // Fall through
90         {
91           OnHandleEvent(impl, *iter);
92           break;
93         }
94         case Event::SELECT:
95         {
96           OnSelectEvent(impl, *iter);
97           break;
98         }
99         case Event::SELECT_ALL:
100         {
101           OnSelectAllEvent(impl);
102           break;
103         }
104         case Event::SELECT_NONE:
105         {
106           OnSelectNoneEvent(impl);
107           break;
108         }
109         case Event::SELECT_RANGE:
110         {
111           OnSelectRangeEvent(impl, *iter);
112           break;
113         }
114       }
115     }
116   }
117
118   if(eventData->mUpdateCursorPosition ||
119      eventData->mUpdateHighlightBox)
120   {
121     impl.NotifyInputMethodContext();
122   }
123
124   // The cursor must also be repositioned after inserts into the model
125   if(eventData->mUpdateCursorPosition)
126   {
127     // Updates the cursor position and scrolls the text to make it visible.
128     CursorInfo cursorInfo;
129
130     // Calculate the cursor position from the new cursor index.
131     impl.GetCursorPosition(eventData->mPrimaryCursorPosition, cursorInfo);
132
133     //only emit the event if the cursor is moved in current function.
134     if(nullptr != impl.mEditableControlInterface && eventData->mEventQueue.size() > 0)
135     {
136       impl.mEditableControlInterface->CursorPositionChanged(oldPos, eventData->mPrimaryCursorPosition);
137     }
138
139     if(eventData->mUpdateCursorHookPosition)
140     {
141       // Update the cursor hook position. Used to move the cursor with the keys 'up' and 'down'.
142       eventData->mCursorHookPositionX      = cursorInfo.primaryPosition.x;
143       eventData->mUpdateCursorHookPosition = false;
144     }
145
146     // Scroll first the text after delete ...
147     if(eventData->mScrollAfterDelete)
148     {
149       impl.ScrollTextToMatchCursor(cursorInfo);
150     }
151
152     // ... then, text can be scrolled to make the cursor visible.
153     if(eventData->mScrollAfterUpdatePosition)
154     {
155       const Vector2 currentCursorPosition(cursorInfo.primaryPosition.x, cursorInfo.lineOffset);
156       impl.ScrollToMakePositionVisible(currentCursorPosition, cursorInfo.lineHeight);
157     }
158     eventData->mScrollAfterUpdatePosition = false;
159     eventData->mScrollAfterDelete         = false;
160
161     impl.UpdateCursorPosition(cursorInfo);
162
163     eventData->mDecoratorUpdated         = true;
164     eventData->mUpdateCursorPosition     = false;
165     eventData->mUpdateGrabHandlePosition = false;
166   }
167
168   if(eventData->mUpdateHighlightBox ||
169      eventData->mUpdateLeftSelectionPosition ||
170      eventData->mUpdateRightSelectionPosition)
171   {
172     CursorInfo leftHandleInfo;
173     CursorInfo rightHandleInfo;
174
175     if(eventData->mUpdateHighlightBox)
176     {
177       impl.GetCursorPosition(eventData->mLeftSelectionPosition, leftHandleInfo);
178
179       impl.GetCursorPosition(eventData->mRightSelectionPosition, rightHandleInfo);
180
181       if(eventData->mScrollAfterUpdatePosition && (eventData->mIsLeftHandleSelected ? eventData->mUpdateLeftSelectionPosition : eventData->mUpdateRightSelectionPosition))
182       {
183         if(eventData->mIsLeftHandleSelected && eventData->mIsRightHandleSelected)
184         {
185           CursorInfo& infoLeft = leftHandleInfo;
186
187           const Vector2 currentCursorPositionLeft(infoLeft.primaryPosition.x, infoLeft.lineOffset);
188           impl.ScrollToMakePositionVisible(currentCursorPositionLeft, infoLeft.lineHeight);
189
190           CursorInfo& infoRight = rightHandleInfo;
191
192           const Vector2 currentCursorPositionRight(infoRight.primaryPosition.x, infoRight.lineOffset);
193           impl.ScrollToMakePositionVisible(currentCursorPositionRight, infoRight.lineHeight);
194         }
195         else
196         {
197           CursorInfo& info = eventData->mIsLeftHandleSelected ? leftHandleInfo : rightHandleInfo;
198
199           const Vector2 currentCursorPosition(info.primaryPosition.x, info.lineOffset);
200           impl.ScrollToMakePositionVisible(currentCursorPosition, info.lineHeight);
201         }
202       }
203     }
204
205     if(eventData->mUpdateLeftSelectionPosition)
206     {
207       impl.UpdateSelectionHandle(LEFT_SELECTION_HANDLE, leftHandleInfo);
208
209       impl.SetPopupButtons();
210       eventData->mDecoratorUpdated            = true;
211       eventData->mUpdateLeftSelectionPosition = false;
212     }
213
214     if(eventData->mUpdateRightSelectionPosition)
215     {
216       impl.UpdateSelectionHandle(RIGHT_SELECTION_HANDLE, rightHandleInfo);
217
218       impl.SetPopupButtons();
219       eventData->mDecoratorUpdated             = true;
220       eventData->mUpdateRightSelectionPosition = false;
221     }
222
223     if(eventData->mUpdateHighlightBox)
224     {
225       impl.RepositionSelectionHandles();
226
227       eventData->mUpdateLeftSelectionPosition  = false;
228       eventData->mUpdateRightSelectionPosition = false;
229       eventData->mUpdateHighlightBox           = false;
230       eventData->mIsLeftHandleSelected         = false;
231       eventData->mIsRightHandleSelected        = false;
232     }
233
234     eventData->mScrollAfterUpdatePosition = false;
235   }
236
237   if(eventData->mUpdateInputStyle)
238   {
239     // Keep a copy of the current input style.
240     InputStyle currentInputStyle;
241     currentInputStyle.Copy(eventData->mInputStyle);
242
243     // Set the default style first.
244     impl.RetrieveDefaultInputStyle(eventData->mInputStyle);
245
246     // Get the character index from the cursor index.
247     const CharacterIndex styleIndex = (eventData->mPrimaryCursorPosition > 0u) ? eventData->mPrimaryCursorPosition - 1u : 0u;
248
249     // Retrieve the style from the style runs stored in the logical model.
250     impl.mModel->mLogicalModel->RetrieveStyle(styleIndex, eventData->mInputStyle);
251
252     // Compare if the input style has changed.
253     const bool hasInputStyleChanged = !currentInputStyle.Equal(eventData->mInputStyle);
254
255     if(hasInputStyleChanged)
256     {
257       const InputStyle::Mask styleChangedMask = currentInputStyle.GetInputStyleChangeMask(eventData->mInputStyle);
258       // Queue the input style changed signal.
259       eventData->mInputStyleChangedQueue.PushBack(styleChangedMask);
260     }
261
262     eventData->mUpdateInputStyle = false;
263   }
264
265   eventData->mEventQueue.clear();
266
267   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::ProcessInputEvents\n");
268
269   const bool decoratorUpdated  = eventData->mDecoratorUpdated;
270   eventData->mDecoratorUpdated = false;
271
272   return decoratorUpdated;
273 }
274
275 void ControllerImplEventHandler::OnCursorKeyEvent(Controller::Impl& impl, const Event& event)
276 {
277   if(NULL == impl.mEventData || !impl.IsShowingRealText())
278   {
279     // Nothing to do if there is no text input.
280     return;
281   }
282
283   int              keyCode         = event.p1.mInt;
284   bool             isShiftModifier = event.p2.mBool;
285   EventData&       eventData       = *impl.mEventData;
286   ModelPtr&        model           = impl.mModel;
287   LogicalModelPtr& logicalModel    = model->mLogicalModel;
288   VisualModelPtr&  visualModel     = model->mVisualModel;
289   uint32_t         oldSelStart     = eventData.mLeftSelectionPosition;
290   uint32_t         oldSelEnd       = eventData.mRightSelectionPosition;
291
292   CharacterIndex& primaryCursorPosition         = eventData.mPrimaryCursorPosition;
293   CharacterIndex  previousPrimaryCursorPosition = primaryCursorPosition;
294
295   if(Dali::DALI_KEY_CURSOR_LEFT == keyCode)
296   {
297     if(primaryCursorPosition > 0u)
298     {
299       if(!isShiftModifier && eventData.mDecorator->IsHighlightVisible())
300       {
301         primaryCursorPosition = std::min(eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
302       }
303       else
304       {
305         primaryCursorPosition = impl.CalculateNewCursorIndex(primaryCursorPosition - 1u);
306       }
307     }
308   }
309   else if(Dali::DALI_KEY_CURSOR_RIGHT == keyCode)
310   {
311     if(logicalModel->mText.Count() > primaryCursorPosition)
312     {
313       if(!isShiftModifier && eventData.mDecorator->IsHighlightVisible())
314       {
315         primaryCursorPosition = std::max(eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
316       }
317       else
318       {
319         primaryCursorPosition = impl.CalculateNewCursorIndex(primaryCursorPosition);
320       }
321     }
322   }
323   else if(Dali::DALI_KEY_CURSOR_UP == keyCode && !isShiftModifier)
324   {
325     // Ignore Shift-Up for text selection for now.
326
327     // Get first the line index of the current cursor position index.
328     CharacterIndex characterIndex = 0u;
329
330     if(primaryCursorPosition > 0u)
331     {
332       characterIndex = primaryCursorPosition - 1u;
333     }
334
335     const LineIndex lineIndex         = visualModel->GetLineOfCharacter(characterIndex);
336     const LineIndex previousLineIndex = (lineIndex > 0 ? lineIndex - 1u : lineIndex);
337     const LineIndex lastLineIndex     = (visualModel->mLines.Size() > 0 ? visualModel->mLines.Size() - 1u : 0);
338     const bool      isLastLine        = (previousLineIndex == lastLineIndex);
339
340     // Retrieve the cursor position info.
341     CursorInfo cursorInfo;
342     impl.GetCursorPosition(primaryCursorPosition,
343                            cursorInfo);
344
345     // Get the line above.
346     const LineRun& line = *(visualModel->mLines.Begin() + previousLineIndex);
347
348     // Get the next hit 'y' point.
349     const float hitPointY = cursorInfo.lineOffset - 0.5f * GetLineHeight(line, isLastLine);
350
351     // Use the cursor hook position 'x' and the next hit 'y' position to calculate the new cursor index.
352     bool matchedCharacter = false;
353     primaryCursorPosition = Text::GetClosestCursorIndex(visualModel,
354                                                         logicalModel,
355                                                         impl.mMetrics,
356                                                         eventData.mCursorHookPositionX,
357                                                         hitPointY,
358                                                         CharacterHitTest::TAP,
359                                                         matchedCharacter);
360   }
361   else if(Dali::DALI_KEY_CURSOR_DOWN == keyCode && !isShiftModifier)
362   {
363     // Ignore Shift-Down for text selection for now.
364
365     // Get first the line index of the current cursor position index.
366     CharacterIndex characterIndex = 0u;
367
368     if(primaryCursorPosition > 0u)
369     {
370       characterIndex = primaryCursorPosition - 1u;
371     }
372
373     const LineIndex lineIndex = visualModel->GetLineOfCharacter(characterIndex);
374
375     if(lineIndex + 1u < visualModel->mLines.Count())
376     {
377       // Retrieve the cursor position info.
378       CursorInfo cursorInfo;
379       impl.GetCursorPosition(primaryCursorPosition, cursorInfo);
380
381       // Get the line below.
382       const LineRun& line = *(visualModel->mLines.Begin() + lineIndex + 1u);
383
384       // Get last line index
385       const LineIndex lastLineIndex = (visualModel->mLines.Size() > 0 ? visualModel->mLines.Size() - 1u : 0);
386       const bool      isLastLine    = (lineIndex + 1u == lastLineIndex);
387
388       // Get the next hit 'y' point.
389       const float hitPointY = cursorInfo.lineOffset + cursorInfo.lineHeight + 0.5f * GetLineHeight(line, isLastLine);
390
391       // Use the cursor hook position 'x' and the next hit 'y' position to calculate the new cursor index.
392       bool matchedCharacter = false;
393       primaryCursorPosition = Text::GetClosestCursorIndex(visualModel,
394                                                           logicalModel,
395                                                           impl.mMetrics,
396                                                           eventData.mCursorHookPositionX,
397                                                           hitPointY,
398                                                           CharacterHitTest::TAP,
399                                                           matchedCharacter);
400     }
401   }
402
403   if(!isShiftModifier && eventData.mState != EventData::SELECTING)
404   {
405     // Update selection position after moving the cursor
406     eventData.mLeftSelectionPosition  = primaryCursorPosition;
407     eventData.mRightSelectionPosition = primaryCursorPosition;
408
409     if(impl.mSelectableControlInterface != nullptr && eventData.mDecorator->IsHighlightVisible())
410     {
411       impl.mSelectableControlInterface->SelectionChanged(oldSelStart, oldSelEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
412     }
413   }
414
415   if(isShiftModifier && impl.IsShowingRealText() && eventData.mShiftSelectionFlag)
416   {
417     // Handle text selection
418     bool selecting = false;
419
420     if(Dali::DALI_KEY_CURSOR_LEFT == keyCode || Dali::DALI_KEY_CURSOR_RIGHT == keyCode)
421     {
422       // Shift-Left/Right to select the text
423       int cursorPositionDelta = primaryCursorPosition - previousPrimaryCursorPosition;
424       if(cursorPositionDelta > 0 || eventData.mRightSelectionPosition > 0u) // Check the boundary
425       {
426         eventData.mRightSelectionPosition += cursorPositionDelta;
427         eventData.mPrimaryCursorPosition = eventData.mRightSelectionPosition;
428
429         if(impl.mSelectableControlInterface != nullptr)
430         {
431           impl.mSelectableControlInterface->SelectionChanged(oldSelStart, oldSelEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
432         }
433       }
434
435       if(impl.mSelectableControlInterface != nullptr && eventData.mLeftSelectionPosition == eventData.mRightSelectionPosition)
436       {
437         // If left selection position and right selection position are the same, the selection is canceled.
438         selecting = false;
439       }
440       else
441       {
442         selecting = true;
443       }
444     }
445     else if(eventData.mLeftSelectionPosition != eventData.mRightSelectionPosition)
446     {
447       // Show no grab handles and text highlight if Shift-Up/Down pressed but no selected text
448       selecting = true;
449     }
450
451     if(selecting)
452     {
453       // Notify the cursor position to the InputMethodContext.
454       if(eventData.mInputMethodContext)
455       {
456         eventData.mInputMethodContext.SetCursorPosition(primaryCursorPosition);
457         eventData.mInputMethodContext.NotifyCursorPosition();
458       }
459
460       impl.ChangeState(EventData::SELECTING);
461
462       eventData.mUpdateLeftSelectionPosition  = true;
463       eventData.mUpdateRightSelectionPosition = true;
464       eventData.mUpdateGrabHandlePosition     = true;
465       eventData.mUpdateHighlightBox           = true;
466
467       // Hide the text selection popup if select the text using keyboard instead of moving grab handles
468       if(eventData.mGrabHandlePopupEnabled)
469       {
470         eventData.mDecorator->SetPopupActive(false);
471       }
472     }
473     else
474     {
475       // If no selection, set a normal cursor.
476       impl.ChangeState(EventData::EDITING);
477       eventData.mUpdateCursorPosition = true;
478     }
479   }
480   else
481   {
482     // Handle normal cursor move
483     impl.ChangeState(EventData::EDITING);
484     eventData.mUpdateCursorPosition = true;
485   }
486
487   eventData.mUpdateInputStyle          = true;
488   eventData.mScrollAfterUpdatePosition = true;
489 }
490
491 void ControllerImplEventHandler::OnTapEvent(Controller::Impl& impl, const Event& event)
492 {
493   if(impl.mEventData)
494   {
495     const unsigned int tapCount     = event.p1.mUint;
496     EventData&         eventData    = *impl.mEventData;
497     ModelPtr&          model        = impl.mModel;
498     LogicalModelPtr&   logicalModel = model->mLogicalModel;
499     VisualModelPtr&    visualModel  = model->mVisualModel;
500
501     if(1u == tapCount)
502     {
503       if(impl.IsShowingRealText())
504       {
505         // Convert from control's coords to text's coords.
506         const float xPosition   = event.p2.mFloat - model->mScrollPosition.x;
507         const float yPosition   = event.p3.mFloat - model->mScrollPosition.y;
508         uint32_t    oldSelStart = eventData.mLeftSelectionPosition;
509         uint32_t    oldSelEnd   = eventData.mRightSelectionPosition;
510
511         // Keep the tap 'x' position. Used to move the cursor.
512         eventData.mCursorHookPositionX = xPosition;
513
514         // Whether to touch point hits on a glyph.
515         bool matchedCharacter            = false;
516         eventData.mPrimaryCursorPosition = Text::GetClosestCursorIndex(visualModel,
517                                                                        logicalModel,
518                                                                        impl.mMetrics,
519                                                                        xPosition,
520                                                                        yPosition,
521                                                                        CharacterHitTest::TAP,
522                                                                        matchedCharacter);
523
524         if(impl.mSelectableControlInterface != nullptr && eventData.mDecorator->IsHighlightVisible())
525         {
526           impl.mSelectableControlInterface->SelectionChanged(oldSelStart, oldSelEnd, eventData.mPrimaryCursorPosition, eventData.mPrimaryCursorPosition);
527         }
528
529         // When the cursor position is changing, delay cursor blinking
530         eventData.mDecorator->DelayCursorBlink();
531       }
532       else
533       {
534         eventData.mPrimaryCursorPosition = 0u;
535       }
536
537       // Update selection position after tapping
538       eventData.mLeftSelectionPosition  = eventData.mPrimaryCursorPosition;
539       eventData.mRightSelectionPosition = eventData.mPrimaryCursorPosition;
540
541       eventData.mUpdateCursorPosition      = true;
542       eventData.mUpdateGrabHandlePosition  = true;
543       eventData.mScrollAfterUpdatePosition = true;
544       eventData.mUpdateInputStyle          = true;
545
546       // Notify the cursor position to the InputMethodContext.
547       if(eventData.mInputMethodContext)
548       {
549         eventData.mInputMethodContext.SetCursorPosition(eventData.mPrimaryCursorPosition);
550         eventData.mInputMethodContext.NotifyCursorPosition();
551       }
552     }
553     else if(2u == tapCount)
554     {
555       if(eventData.mSelectionEnabled)
556       {
557         // Convert from control's coords to text's coords.
558         const float xPosition = event.p2.mFloat - model->mScrollPosition.x;
559         const float yPosition = event.p3.mFloat - model->mScrollPosition.y;
560
561         // Calculates the logical position from the x,y coords.
562         impl.RepositionSelectionHandles(xPosition, yPosition, eventData.mDoubleTapAction);
563       }
564     }
565   }
566 }
567
568 void ControllerImplEventHandler::OnPanEvent(Controller::Impl& impl, const Event& event)
569 {
570   if(impl.mEventData)
571   {
572     EventData&    eventData = *impl.mEventData;
573     DecoratorPtr& decorator = eventData.mDecorator;
574
575     const bool isHorizontalScrollEnabled = decorator->IsHorizontalScrollEnabled();
576     const bool isVerticalScrollEnabled   = decorator->IsVerticalScrollEnabled();
577
578     if(!isHorizontalScrollEnabled && !isVerticalScrollEnabled)
579     {
580       // Nothing to do if scrolling is not enabled.
581       return;
582     }
583
584     const GestureState state = static_cast<GestureState>(event.p1.mInt);
585     switch(state)
586     {
587       case GestureState::STARTED:
588       {
589         // Will remove the cursor, handles or text's popup, ...
590         impl.ChangeState(EventData::TEXT_PANNING);
591         break;
592       }
593       case GestureState::CONTINUING:
594       {
595         ModelPtr& model = impl.mModel;
596
597         const Vector2& layoutSize     = model->mVisualModel->GetLayoutSize();
598         Vector2&       scrollPosition = model->mScrollPosition;
599         const Vector2  currentScroll  = scrollPosition;
600
601         if(isHorizontalScrollEnabled)
602         {
603           const float displacementX = event.p2.mFloat;
604           scrollPosition.x += displacementX;
605
606           impl.ClampHorizontalScroll(layoutSize);
607         }
608
609         if(isVerticalScrollEnabled)
610         {
611           const float displacementY = event.p3.mFloat;
612           scrollPosition.y += displacementY;
613
614           impl.ClampVerticalScroll(layoutSize);
615         }
616
617         decorator->UpdatePositions(scrollPosition - currentScroll);
618         break;
619       }
620       case GestureState::FINISHED:
621       case GestureState::CANCELLED: // FALLTHROUGH
622       {
623         // Will go back to the previous state to show the cursor, handles, the text's popup, ...
624         impl.ChangeState(eventData.mPreviousState);
625         break;
626       }
627       default:
628         break;
629     }
630   }
631 }
632
633 void ControllerImplEventHandler::OnLongPressEvent(Controller::Impl& impl, const Event& event)
634 {
635   DALI_LOG_INFO(gLogFilter, Debug::General, "Controller::OnLongPressEvent\n");
636
637   if(impl.mEventData)
638   {
639     EventData& eventData = *impl.mEventData;
640
641     if(!impl.IsShowingRealText() && (EventData::EDITING == eventData.mState))
642     {
643       impl.ChangeState(EventData::EDITING_WITH_POPUP);
644       eventData.mDecoratorUpdated = true;
645       eventData.mUpdateInputStyle = true;
646     }
647     else
648     {
649       if(eventData.mSelectionEnabled)
650       {
651         ModelPtr& model = impl.mModel;
652
653         // Convert from control's coords to text's coords.
654         const float xPosition = event.p2.mFloat - model->mScrollPosition.x;
655         const float yPosition = event.p3.mFloat - model->mScrollPosition.y;
656
657         // Calculates the logical position from the x,y coords.
658         impl.RepositionSelectionHandles(xPosition, yPosition, eventData.mLongPressAction);
659       }
660     }
661   }
662 }
663
664 void ControllerImplEventHandler::OnHandleEvent(Controller::Impl& impl, const Event& event)
665 {
666   if(impl.mEventData)
667   {
668     const unsigned int state                    = event.p1.mUint;
669     const bool         handleStopScrolling      = (HANDLE_STOP_SCROLLING == state);
670     const bool         isSmoothHandlePanEnabled = impl.mEventData->mDecorator->IsSmoothHandlePanEnabled();
671
672     if(HANDLE_PRESSED == state)
673     {
674       OnHandlePressed(impl, event, isSmoothHandlePanEnabled);
675     } // end ( HANDLE_PRESSED == state )
676     else if((HANDLE_RELEASED == state) ||
677             handleStopScrolling)
678     {
679       OnHandleReleased(impl, event, isSmoothHandlePanEnabled, handleStopScrolling);
680     } // end ( ( HANDLE_RELEASED == state ) || ( HANDLE_STOP_SCROLLING == state ) )
681     else if(HANDLE_SCROLLING == state)
682     {
683       OnHandleScrolling(impl, event, isSmoothHandlePanEnabled);
684     } // end ( HANDLE_SCROLLING == state )
685   }
686 }
687
688 void ControllerImplEventHandler::OnSelectEvent(Controller::Impl& impl, const Event& event)
689 {
690   if(impl.mEventData && impl.mEventData->mSelectionEnabled)
691   {
692     ModelPtr&      model          = impl.mModel;
693     const Vector2& scrollPosition = model->mScrollPosition;
694
695     // Convert from control's coords to text's coords.
696     const float xPosition = event.p2.mFloat - scrollPosition.x;
697     const float yPosition = event.p3.mFloat - scrollPosition.y;
698
699     // Calculates the logical position from the x,y coords.
700     impl.RepositionSelectionHandles(xPosition, yPosition, Controller::NoTextTap::HIGHLIGHT);
701   }
702 }
703
704 void ControllerImplEventHandler::OnSelectAllEvent(Controller::Impl& impl)
705 {
706   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "OnSelectAllEvent mEventData->mSelectionEnabled%s \n", impl.mEventData->mSelectionEnabled ? "true" : "false");
707
708   if(impl.mEventData)
709   {
710     EventData& eventData = *impl.mEventData;
711     if(eventData.mSelectionEnabled && eventData.mState != EventData::INACTIVE)
712     {
713       ModelPtr&      model          = impl.mModel;
714       const Vector2& scrollPosition = model->mScrollPosition;
715
716       // Calculates the logical position from the start.
717       impl.RepositionSelectionHandles(0.f - scrollPosition.x,
718                                       0.f - scrollPosition.y,
719                                       Controller::NoTextTap::HIGHLIGHT);
720
721       uint32_t oldStart = eventData.mLeftSelectionPosition;
722       uint32_t oldEnd   = eventData.mRightSelectionPosition;
723
724       eventData.mLeftSelectionPosition  = 0u;
725       eventData.mRightSelectionPosition = model->mLogicalModel->mText.Count();
726       eventData.mPrimaryCursorPosition  = eventData.mRightSelectionPosition;
727
728       if(impl.mSelectableControlInterface != nullptr)
729       {
730         impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
731       }
732     }
733   }
734 }
735
736 void ControllerImplEventHandler::OnSelectNoneEvent(Controller::Impl& impl)
737 {
738   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "OnSelectNoneEvent mEventData->mSelectionEnabled%s \n", impl.mEventData->mSelectionEnabled ? "true" : "false");
739
740   if(impl.mEventData)
741   {
742     EventData& eventData = *impl.mEventData;
743     if(eventData.mSelectionEnabled && eventData.mState == EventData::SELECTING)
744     {
745       uint32_t oldStart = eventData.mLeftSelectionPosition;
746       uint32_t oldEnd   = eventData.mRightSelectionPosition;
747
748       eventData.mLeftSelectionPosition = eventData.mRightSelectionPosition = eventData.mPrimaryCursorPosition;
749       impl.ChangeState(EventData::EDITING);
750       eventData.mUpdateCursorPosition      = true;
751       eventData.mUpdateInputStyle          = true;
752       eventData.mScrollAfterUpdatePosition = true;
753
754       if(impl.mSelectableControlInterface != nullptr)
755       {
756         impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
757       }
758     }
759   }
760 }
761
762 void ControllerImplEventHandler::OnSelectRangeEvent(Controller::Impl& impl, const Event& event)
763 {
764   if(impl.mEventData && impl.mEventData->mSelectionEnabled && impl.mEventData->mState != EventData::INACTIVE)
765   {
766     ModelPtr&      model          = impl.mModel;
767     const Vector2& scrollPosition = model->mScrollPosition;
768
769     // Calculate the selection index.
770     const uint32_t length = static_cast<uint32_t>(model->mLogicalModel->mText.Count());
771     const uint32_t start  = std::min(event.p2.mUint, length);
772     const uint32_t end    = std::min(event.p3.mUint, length);
773
774     if(start != end)
775     {
776       uint32_t oldStart = impl.mEventData->mLeftSelectionPosition;
777       uint32_t oldEnd   = impl.mEventData->mRightSelectionPosition;
778
779       // Calculates the logical position from the x,y coords.
780       impl.RepositionSelectionHandles(0.f - scrollPosition.x, 0.f - scrollPosition.y, Controller::NoTextTap::HIGHLIGHT);
781
782       impl.mEventData->mLeftSelectionPosition  = start;
783       impl.mEventData->mRightSelectionPosition = end;
784       impl.mEventData->mPrimaryCursorPosition  = end;
785
786       if(impl.mSelectableControlInterface != nullptr)
787       {
788         impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, start, end);
789       }
790     }
791   }
792 }
793
794 void ControllerImplEventHandler::OnHandlePressed(Controller::Impl& impl, const Event& event, const bool isSmoothHandlePanEnabled)
795 {
796   ModelPtr&      model          = impl.mModel;
797   const Vector2& scrollPosition = model->mScrollPosition;
798
799   // Convert from decorator's coords to text's coords.
800   const float xPosition = event.p2.mFloat - scrollPosition.x;
801   const float yPosition = event.p3.mFloat - scrollPosition.y;
802
803   // Need to calculate the handle's new position.
804   bool                 matchedCharacter  = false;
805   const CharacterIndex handleNewPosition = Text::GetClosestCursorIndex(model->mVisualModel,
806                                                                        model->mLogicalModel,
807                                                                        impl.mMetrics,
808                                                                        xPosition,
809                                                                        yPosition,
810                                                                        CharacterHitTest::SCROLL,
811                                                                        matchedCharacter);
812
813   EventData& eventData = *impl.mEventData;
814   uint32_t   oldStart  = eventData.mLeftSelectionPosition;
815   uint32_t   oldEnd    = eventData.mRightSelectionPosition;
816
817   if(Event::GRAB_HANDLE_EVENT == event.type)
818   {
819     impl.ChangeState(EventData::GRAB_HANDLE_PANNING);
820
821     if(handleNewPosition != eventData.mPrimaryCursorPosition)
822     {
823       // Updates the cursor position if the handle's new position is different than the current one.
824       eventData.mUpdateCursorPosition = true;
825       // Does not update the grab handle position if the smooth panning is enabled. (The decorator does it smooth).
826       eventData.mUpdateGrabHandlePosition = !isSmoothHandlePanEnabled;
827       eventData.mPrimaryCursorPosition    = handleNewPosition;
828     }
829
830     // 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.
831     eventData.mDecoratorUpdated = isSmoothHandlePanEnabled;
832   }
833   else if(Event::LEFT_SELECTION_HANDLE_EVENT == event.type)
834   {
835     impl.ChangeState(EventData::SELECTION_HANDLE_PANNING);
836
837     if((handleNewPosition != eventData.mLeftSelectionPosition) &&
838        (handleNewPosition != eventData.mRightSelectionPosition))
839     {
840       // Updates the highlight box if the handle's new position is different than the current one.
841       eventData.mUpdateHighlightBox = true;
842       // Does not update the selection handle position if the smooth panning is enabled. (The decorator does it smooth).
843       eventData.mUpdateLeftSelectionPosition = !isSmoothHandlePanEnabled;
844       eventData.mLeftSelectionPosition       = handleNewPosition;
845     }
846
847     // 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.
848     eventData.mDecoratorUpdated = isSmoothHandlePanEnabled;
849
850     // Will define the order to scroll the text to match the handle position.
851     eventData.mIsLeftHandleSelected  = true;
852     eventData.mIsRightHandleSelected = false;
853   }
854   else if(Event::RIGHT_SELECTION_HANDLE_EVENT == event.type)
855   {
856     impl.ChangeState(EventData::SELECTION_HANDLE_PANNING);
857
858     if((handleNewPosition != eventData.mRightSelectionPosition) &&
859        (handleNewPosition != eventData.mLeftSelectionPosition))
860     {
861       // Updates the highlight box if the handle's new position is different than the current one.
862       eventData.mUpdateHighlightBox = true;
863       // Does not update the selection handle position if the smooth panning is enabled. (The decorator does it smooth).
864       eventData.mUpdateRightSelectionPosition = !isSmoothHandlePanEnabled;
865       eventData.mRightSelectionPosition       = handleNewPosition;
866     }
867
868     // 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.
869     eventData.mDecoratorUpdated = isSmoothHandlePanEnabled;
870
871     // Will define the order to scroll the text to match the handle position.
872     eventData.mIsLeftHandleSelected  = false;
873     eventData.mIsRightHandleSelected = true;
874   }
875
876   if((impl.mSelectableControlInterface != nullptr) && ((oldStart != eventData.mLeftSelectionPosition) || (oldEnd != eventData.mRightSelectionPosition)))
877   {
878     impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
879   }
880 }
881
882 void ControllerImplEventHandler::OnHandleReleased(Controller::Impl& impl, const Event& event, const bool isSmoothHandlePanEnabled, const bool handleStopScrolling)
883 {
884   CharacterIndex handlePosition = 0u;
885   if(handleStopScrolling || isSmoothHandlePanEnabled)
886   {
887     ModelPtr&      model          = impl.mModel;
888     const Vector2& scrollPosition = model->mScrollPosition;
889
890     // Convert from decorator's coords to text's coords.
891     const float xPosition = event.p2.mFloat - scrollPosition.x;
892     const float yPosition = event.p3.mFloat - scrollPosition.y;
893
894     bool matchedCharacter = false;
895     handlePosition        = Text::GetClosestCursorIndex(model->mVisualModel,
896                                                  model->mLogicalModel,
897                                                  impl.mMetrics,
898                                                  xPosition,
899                                                  yPosition,
900                                                  CharacterHitTest::SCROLL,
901                                                  matchedCharacter);
902   }
903
904   EventData& eventData = *impl.mEventData;
905   uint32_t   oldStart  = eventData.mLeftSelectionPosition;
906   uint32_t   oldEnd    = eventData.mRightSelectionPosition;
907
908   if(Event::GRAB_HANDLE_EVENT == event.type)
909   {
910     eventData.mUpdateCursorPosition     = true;
911     eventData.mUpdateGrabHandlePosition = true;
912     eventData.mUpdateInputStyle         = true;
913
914     if(!impl.IsClipboardEmpty())
915     {
916       impl.ChangeState(EventData::EDITING_WITH_PASTE_POPUP); // Moving grabhandle will show Paste Popup
917     }
918
919     if(handleStopScrolling || isSmoothHandlePanEnabled)
920     {
921       eventData.mScrollAfterUpdatePosition = true;
922       eventData.mPrimaryCursorPosition     = handlePosition;
923     }
924   }
925   else if(Event::LEFT_SELECTION_HANDLE_EVENT == event.type)
926   {
927     impl.ChangeState(EventData::SELECTING);
928
929     eventData.mUpdateHighlightBox           = true;
930     eventData.mUpdateLeftSelectionPosition  = true;
931     eventData.mUpdateRightSelectionPosition = true;
932
933     if(handleStopScrolling || isSmoothHandlePanEnabled)
934     {
935       eventData.mScrollAfterUpdatePosition = true;
936
937       if((handlePosition != eventData.mRightSelectionPosition) &&
938          (handlePosition != eventData.mLeftSelectionPosition))
939       {
940         eventData.mLeftSelectionPosition = handlePosition;
941       }
942     }
943   }
944   else if(Event::RIGHT_SELECTION_HANDLE_EVENT == event.type)
945   {
946     impl.ChangeState(EventData::SELECTING);
947
948     eventData.mUpdateHighlightBox           = true;
949     eventData.mUpdateRightSelectionPosition = true;
950     eventData.mUpdateLeftSelectionPosition  = true;
951
952     if(handleStopScrolling || isSmoothHandlePanEnabled)
953     {
954       eventData.mScrollAfterUpdatePosition = true;
955       if((handlePosition != eventData.mRightSelectionPosition) &&
956          (handlePosition != eventData.mLeftSelectionPosition))
957       {
958         eventData.mRightSelectionPosition = handlePosition;
959       }
960     }
961   }
962
963   if((impl.mSelectableControlInterface != nullptr) && ((oldStart != eventData.mLeftSelectionPosition) || (oldEnd != eventData.mRightSelectionPosition)))
964   {
965     impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
966   }
967
968   eventData.mDecoratorUpdated = true;
969 }
970
971 void ControllerImplEventHandler::OnHandleScrolling(Controller::Impl& impl, const Event& event, const bool isSmoothHandlePanEnabled)
972 {
973   ModelPtr&       model          = impl.mModel;
974   Vector2&        scrollPosition = model->mScrollPosition;
975   VisualModelPtr& visualModel    = model->mVisualModel;
976
977   const float    xSpeed                = event.p2.mFloat;
978   const float    ySpeed                = event.p3.mFloat;
979   const Vector2& layoutSize            = visualModel->GetLayoutSize();
980   const Vector2  currentScrollPosition = scrollPosition;
981
982   scrollPosition.x += xSpeed;
983   scrollPosition.y += ySpeed;
984
985   impl.ClampHorizontalScroll(layoutSize);
986   impl.ClampVerticalScroll(layoutSize);
987
988   EventData&    eventData = *impl.mEventData;
989   DecoratorPtr& decorator = eventData.mDecorator;
990
991   bool endOfScroll = false;
992   if(Vector2::ZERO == (currentScrollPosition - scrollPosition))
993   {
994     // Notify the decorator there is no more text to scroll.
995     // The decorator won't send more scroll events.
996     decorator->NotifyEndOfScroll();
997     // Still need to set the position of the handle.
998     endOfScroll = true;
999   }
1000
1001   // Set the position of the handle.
1002   const bool scrollRightDirection      = xSpeed > 0.f;
1003   const bool scrollBottomDirection     = ySpeed > 0.f;
1004   const bool leftSelectionHandleEvent  = Event::LEFT_SELECTION_HANDLE_EVENT == event.type;
1005   const bool rightSelectionHandleEvent = Event::RIGHT_SELECTION_HANDLE_EVENT == event.type;
1006
1007   if(Event::GRAB_HANDLE_EVENT == event.type)
1008   {
1009     impl.ChangeState(EventData::GRAB_HANDLE_PANNING);
1010
1011     // Get the grab handle position in decorator coords.
1012     Vector2 position = decorator->GetPosition(GRAB_HANDLE);
1013
1014     if(decorator->IsHorizontalScrollEnabled())
1015     {
1016       // Position the grag handle close to either the left or right edge.
1017       position.x = scrollRightDirection ? 0.f : visualModel->mControlSize.width;
1018     }
1019
1020     if(decorator->IsVerticalScrollEnabled())
1021     {
1022       position.x = eventData.mCursorHookPositionX;
1023
1024       // Position the grag handle close to either the top or bottom edge.
1025       position.y = scrollBottomDirection ? 0.f : visualModel->mControlSize.height;
1026     }
1027
1028     // Get the new handle position.
1029     // The grab handle's position is in decorator's coords. Need to transforms to text's coords.
1030     bool                 matchedCharacter = false;
1031     const CharacterIndex handlePosition   = Text::GetClosestCursorIndex(visualModel,
1032                                                                       impl.mModel->mLogicalModel,
1033                                                                       impl.mMetrics,
1034                                                                       position.x - scrollPosition.x,
1035                                                                       position.y - scrollPosition.y,
1036                                                                       CharacterHitTest::SCROLL,
1037                                                                       matchedCharacter);
1038
1039     if(eventData.mPrimaryCursorPosition != handlePosition)
1040     {
1041       eventData.mUpdateCursorPosition      = true;
1042       eventData.mUpdateGrabHandlePosition  = !isSmoothHandlePanEnabled;
1043       eventData.mScrollAfterUpdatePosition = true;
1044       eventData.mPrimaryCursorPosition     = handlePosition;
1045     }
1046     eventData.mUpdateInputStyle = eventData.mUpdateCursorPosition;
1047
1048     // Updates the decorator if the soft handle panning is enabled.
1049     eventData.mDecoratorUpdated = isSmoothHandlePanEnabled;
1050   }
1051   else if(leftSelectionHandleEvent || rightSelectionHandleEvent)
1052   {
1053     impl.ChangeState(EventData::SELECTION_HANDLE_PANNING);
1054
1055     // Get the selection handle position in decorator coords.
1056     Vector2 position = decorator->GetPosition(leftSelectionHandleEvent ? Text::LEFT_SELECTION_HANDLE : Text::RIGHT_SELECTION_HANDLE);
1057
1058     if(decorator->IsHorizontalScrollEnabled())
1059     {
1060       // Position the selection handle close to either the left or right edge.
1061       position.x = scrollRightDirection ? 0.f : visualModel->mControlSize.width;
1062     }
1063
1064     if(decorator->IsVerticalScrollEnabled())
1065     {
1066       position.x = eventData.mCursorHookPositionX;
1067
1068       // Position the grag handle close to either the top or bottom edge.
1069       position.y = scrollBottomDirection ? 0.f : visualModel->mControlSize.height;
1070     }
1071
1072     // Get the new handle position.
1073     // The selection handle's position is in decorator's coords. Need to transform to text's coords.
1074     bool                 matchedCharacter = false;
1075     const CharacterIndex handlePosition   = Text::GetClosestCursorIndex(visualModel,
1076                                                                       impl.mModel->mLogicalModel,
1077                                                                       impl.mMetrics,
1078                                                                       position.x - scrollPosition.x,
1079                                                                       position.y - scrollPosition.y,
1080                                                                       CharacterHitTest::SCROLL,
1081                                                                       matchedCharacter);
1082     uint32_t             oldStart         = eventData.mLeftSelectionPosition;
1083     uint32_t             oldEnd           = eventData.mRightSelectionPosition;
1084
1085     if(leftSelectionHandleEvent)
1086     {
1087       const bool differentHandles = (eventData.mLeftSelectionPosition != handlePosition) && (eventData.mRightSelectionPosition != handlePosition);
1088
1089       if(differentHandles || endOfScroll)
1090       {
1091         eventData.mUpdateHighlightBox           = true;
1092         eventData.mUpdateLeftSelectionPosition  = !isSmoothHandlePanEnabled;
1093         eventData.mUpdateRightSelectionPosition = isSmoothHandlePanEnabled;
1094         eventData.mLeftSelectionPosition        = handlePosition;
1095       }
1096     }
1097     else
1098     {
1099       const bool differentHandles = (eventData.mRightSelectionPosition != handlePosition) && (eventData.mLeftSelectionPosition != handlePosition);
1100       if(differentHandles || endOfScroll)
1101       {
1102         eventData.mUpdateHighlightBox           = true;
1103         eventData.mUpdateRightSelectionPosition = !isSmoothHandlePanEnabled;
1104         eventData.mUpdateLeftSelectionPosition  = isSmoothHandlePanEnabled;
1105         eventData.mRightSelectionPosition       = handlePosition;
1106       }
1107     }
1108
1109     if(eventData.mUpdateLeftSelectionPosition || eventData.mUpdateRightSelectionPosition)
1110     {
1111       impl.RepositionSelectionHandles();
1112
1113       eventData.mScrollAfterUpdatePosition = !isSmoothHandlePanEnabled;
1114
1115       if(impl.mSelectableControlInterface != nullptr)
1116       {
1117         impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
1118       }
1119     }
1120   }
1121   eventData.mDecoratorUpdated = true;
1122 }
1123
1124 } // namespace Text
1125
1126 } // namespace Toolkit
1127
1128 } // namespace Dali