fix wrong behaviour of SelectNone()
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller-impl-event-handler.cpp
1 /*
2  * Copyright (c) 2021 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
290   CharacterIndex& primaryCursorPosition         = eventData.mPrimaryCursorPosition;
291   CharacterIndex  previousPrimaryCursorPosition = primaryCursorPosition;
292
293   if(Dali::DALI_KEY_CURSOR_LEFT == keyCode)
294   {
295     if(primaryCursorPosition > 0u)
296     {
297       if(!isShiftModifier && eventData.mDecorator->IsHighlightVisible())
298       {
299         primaryCursorPosition = std::min(eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
300       }
301       else
302       {
303         primaryCursorPosition = impl.CalculateNewCursorIndex(primaryCursorPosition - 1u);
304       }
305     }
306   }
307   else if(Dali::DALI_KEY_CURSOR_RIGHT == keyCode)
308   {
309     if(logicalModel->mText.Count() > primaryCursorPosition)
310     {
311       if(!isShiftModifier && eventData.mDecorator->IsHighlightVisible())
312       {
313         primaryCursorPosition = std::max(eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
314       }
315       else
316       {
317         primaryCursorPosition = impl.CalculateNewCursorIndex(primaryCursorPosition);
318       }
319     }
320   }
321   else if(Dali::DALI_KEY_CURSOR_UP == keyCode && !isShiftModifier)
322   {
323     // Ignore Shift-Up for text selection for now.
324
325     // Get first the line index of the current cursor position index.
326     CharacterIndex characterIndex = 0u;
327
328     if(primaryCursorPosition > 0u)
329     {
330       characterIndex = primaryCursorPosition - 1u;
331     }
332
333     const LineIndex lineIndex         = visualModel->GetLineOfCharacter(characterIndex);
334     const LineIndex previousLineIndex = (lineIndex > 0 ? lineIndex - 1u : lineIndex);
335
336     // Retrieve the cursor position info.
337     CursorInfo cursorInfo;
338     impl.GetCursorPosition(primaryCursorPosition,
339                            cursorInfo);
340
341     // Get the line above.
342     const LineRun& line = *(visualModel->mLines.Begin() + previousLineIndex);
343
344     // Get the next hit 'y' point.
345     const float hitPointY = cursorInfo.lineOffset - 0.5f * GetLineHeight(line);
346
347     // Use the cursor hook position 'x' and the next hit 'y' position to calculate the new cursor index.
348     bool matchedCharacter = false;
349     primaryCursorPosition = Text::GetClosestCursorIndex(visualModel,
350                                                         logicalModel,
351                                                         impl.mMetrics,
352                                                         eventData.mCursorHookPositionX,
353                                                         hitPointY,
354                                                         CharacterHitTest::TAP,
355                                                         matchedCharacter);
356   }
357   else if(Dali::DALI_KEY_CURSOR_DOWN == keyCode && !isShiftModifier)
358   {
359     // Ignore Shift-Down for text selection for now.
360
361     // Get first the line index of the current cursor position index.
362     CharacterIndex characterIndex = 0u;
363
364     if(primaryCursorPosition > 0u)
365     {
366       characterIndex = primaryCursorPosition - 1u;
367     }
368
369     const LineIndex lineIndex = visualModel->GetLineOfCharacter(characterIndex);
370
371     if(lineIndex + 1u < visualModel->mLines.Count())
372     {
373       // Retrieve the cursor position info.
374       CursorInfo cursorInfo;
375       impl.GetCursorPosition(primaryCursorPosition, cursorInfo);
376
377       // Get the line below.
378       const LineRun& line = *(visualModel->mLines.Begin() + lineIndex + 1u);
379
380       // Get the next hit 'y' point.
381       const float hitPointY = cursorInfo.lineOffset + cursorInfo.lineHeight + 0.5f * GetLineHeight(line);
382
383       // Use the cursor hook position 'x' and the next hit 'y' position to calculate the new cursor index.
384       bool matchedCharacter = false;
385       primaryCursorPosition = Text::GetClosestCursorIndex(visualModel,
386                                                           logicalModel,
387                                                           impl.mMetrics,
388                                                           eventData.mCursorHookPositionX,
389                                                           hitPointY,
390                                                           CharacterHitTest::TAP,
391                                                           matchedCharacter);
392     }
393   }
394
395   if(!isShiftModifier && eventData.mState != EventData::SELECTING)
396   {
397     // Update selection position after moving the cursor
398     eventData.mLeftSelectionPosition  = primaryCursorPosition;
399     eventData.mRightSelectionPosition = primaryCursorPosition;
400   }
401
402   if(isShiftModifier && impl.IsShowingRealText() && eventData.mShiftSelectionFlag)
403   {
404     // Handle text selection
405     bool selecting = false;
406
407     if(Dali::DALI_KEY_CURSOR_LEFT == keyCode || Dali::DALI_KEY_CURSOR_RIGHT == keyCode)
408     {
409       // Shift-Left/Right to select the text
410       int cursorPositionDelta = primaryCursorPosition - previousPrimaryCursorPosition;
411       if(cursorPositionDelta > 0 || eventData.mRightSelectionPosition > 0u) // Check the boundary
412       {
413         uint32_t oldStart = eventData.mLeftSelectionPosition;
414         uint32_t oldEnd   = eventData.mRightSelectionPosition;
415
416         eventData.mRightSelectionPosition += cursorPositionDelta;
417
418         if(impl.mSelectableControlInterface != nullptr)
419         {
420           impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
421         }
422       }
423       selecting = true;
424     }
425     else if(eventData.mLeftSelectionPosition != eventData.mRightSelectionPosition)
426     {
427       // Show no grab handles and text highlight if Shift-Up/Down pressed but no selected text
428       selecting = true;
429     }
430
431     if(selecting)
432     {
433       // Notify the cursor position to the InputMethodContext.
434       if(eventData.mInputMethodContext)
435       {
436         eventData.mInputMethodContext.SetCursorPosition(primaryCursorPosition);
437         eventData.mInputMethodContext.NotifyCursorPosition();
438       }
439
440       impl.ChangeState(EventData::SELECTING);
441
442       eventData.mUpdateLeftSelectionPosition  = true;
443       eventData.mUpdateRightSelectionPosition = true;
444       eventData.mUpdateGrabHandlePosition     = true;
445       eventData.mUpdateHighlightBox           = true;
446
447       // Hide the text selection popup if select the text using keyboard instead of moving grab handles
448       if(eventData.mGrabHandlePopupEnabled)
449       {
450         eventData.mDecorator->SetPopupActive(false);
451       }
452     }
453   }
454   else
455   {
456     // Handle normal cursor move
457     impl.ChangeState(EventData::EDITING);
458     eventData.mUpdateCursorPosition = true;
459   }
460
461   eventData.mUpdateInputStyle          = true;
462   eventData.mScrollAfterUpdatePosition = true;
463 }
464
465 void ControllerImplEventHandler::OnTapEvent(Controller::Impl& impl, const Event& event)
466 {
467   if(impl.mEventData)
468   {
469     const unsigned int tapCount     = event.p1.mUint;
470     EventData&         eventData    = *impl.mEventData;
471     ModelPtr&          model        = impl.mModel;
472     LogicalModelPtr&   logicalModel = model->mLogicalModel;
473     VisualModelPtr&    visualModel  = model->mVisualModel;
474
475     if(1u == tapCount)
476     {
477       if(impl.IsShowingRealText())
478       {
479         // Convert from control's coords to text's coords.
480         const float xPosition = event.p2.mFloat - model->mScrollPosition.x;
481         const float yPosition = event.p3.mFloat - model->mScrollPosition.y;
482
483         // Keep the tap 'x' position. Used to move the cursor.
484         eventData.mCursorHookPositionX = xPosition;
485
486         // Whether to touch point hits on a glyph.
487         bool matchedCharacter            = false;
488         eventData.mPrimaryCursorPosition = Text::GetClosestCursorIndex(visualModel,
489                                                                        logicalModel,
490                                                                        impl.mMetrics,
491                                                                        xPosition,
492                                                                        yPosition,
493                                                                        CharacterHitTest::TAP,
494                                                                        matchedCharacter);
495
496         // When the cursor position is changing, delay cursor blinking
497         eventData.mDecorator->DelayCursorBlink();
498       }
499       else
500       {
501         eventData.mPrimaryCursorPosition = 0u;
502       }
503
504       // Update selection position after tapping
505       eventData.mLeftSelectionPosition  = eventData.mPrimaryCursorPosition;
506       eventData.mRightSelectionPosition = eventData.mPrimaryCursorPosition;
507
508       eventData.mUpdateCursorPosition      = true;
509       eventData.mUpdateGrabHandlePosition  = true;
510       eventData.mScrollAfterUpdatePosition = true;
511       eventData.mUpdateInputStyle          = true;
512
513       // Notify the cursor position to the InputMethodContext.
514       if(eventData.mInputMethodContext)
515       {
516         eventData.mInputMethodContext.SetCursorPosition(eventData.mPrimaryCursorPosition);
517         eventData.mInputMethodContext.NotifyCursorPosition();
518       }
519     }
520     else if(2u == tapCount)
521     {
522       if(eventData.mSelectionEnabled)
523       {
524         // Convert from control's coords to text's coords.
525         const float xPosition = event.p2.mFloat - model->mScrollPosition.x;
526         const float yPosition = event.p3.mFloat - model->mScrollPosition.y;
527
528         // Calculates the logical position from the x,y coords.
529         impl.RepositionSelectionHandles(xPosition, yPosition, eventData.mDoubleTapAction);
530       }
531     }
532   }
533 }
534
535 void ControllerImplEventHandler::OnPanEvent(Controller::Impl& impl, const Event& event)
536 {
537   if(impl.mEventData)
538   {
539     EventData&    eventData = *impl.mEventData;
540     DecoratorPtr& decorator = eventData.mDecorator;
541
542     const bool isHorizontalScrollEnabled = decorator->IsHorizontalScrollEnabled();
543     const bool isVerticalScrollEnabled   = decorator->IsVerticalScrollEnabled();
544
545     if(!isHorizontalScrollEnabled && !isVerticalScrollEnabled)
546     {
547       // Nothing to do if scrolling is not enabled.
548       return;
549     }
550
551     const GestureState state = static_cast<GestureState>(event.p1.mInt);
552     switch(state)
553     {
554       case GestureState::STARTED:
555       {
556         // Will remove the cursor, handles or text's popup, ...
557         impl.ChangeState(EventData::TEXT_PANNING);
558         break;
559       }
560       case GestureState::CONTINUING:
561       {
562         ModelPtr& model = impl.mModel;
563
564         const Vector2& layoutSize     = model->mVisualModel->GetLayoutSize();
565         Vector2&       scrollPosition = model->mScrollPosition;
566         const Vector2  currentScroll  = scrollPosition;
567
568         if(isHorizontalScrollEnabled)
569         {
570           const float displacementX = event.p2.mFloat;
571           scrollPosition.x += displacementX;
572
573           impl.ClampHorizontalScroll(layoutSize);
574         }
575
576         if(isVerticalScrollEnabled)
577         {
578           const float displacementY = event.p3.mFloat;
579           scrollPosition.y += displacementY;
580
581           impl.ClampVerticalScroll(layoutSize);
582         }
583
584         decorator->UpdatePositions(scrollPosition - currentScroll);
585         break;
586       }
587       case GestureState::FINISHED:
588       case GestureState::CANCELLED: // FALLTHROUGH
589       {
590         // Will go back to the previous state to show the cursor, handles, the text's popup, ...
591         impl.ChangeState(eventData.mPreviousState);
592         break;
593       }
594       default:
595         break;
596     }
597   }
598 }
599
600 void ControllerImplEventHandler::OnLongPressEvent(Controller::Impl& impl, const Event& event)
601 {
602   DALI_LOG_INFO(gLogFilter, Debug::General, "Controller::OnLongPressEvent\n");
603
604   if(impl.mEventData)
605   {
606     EventData& eventData = *impl.mEventData;
607
608     if(!impl.IsShowingRealText() && (EventData::EDITING == eventData.mState))
609     {
610       impl.ChangeState(EventData::EDITING_WITH_POPUP);
611       eventData.mDecoratorUpdated = true;
612       eventData.mUpdateInputStyle = true;
613     }
614     else
615     {
616       if(eventData.mSelectionEnabled)
617       {
618         ModelPtr& model = impl.mModel;
619
620         // Convert from control's coords to text's coords.
621         const float xPosition = event.p2.mFloat - model->mScrollPosition.x;
622         const float yPosition = event.p3.mFloat - model->mScrollPosition.y;
623
624         // Calculates the logical position from the x,y coords.
625         impl.RepositionSelectionHandles(xPosition, yPosition, eventData.mLongPressAction);
626       }
627     }
628   }
629 }
630
631 void ControllerImplEventHandler::OnHandleEvent(Controller::Impl& impl, const Event& event)
632 {
633   if(impl.mEventData)
634   {
635     const unsigned int state                    = event.p1.mUint;
636     const bool         handleStopScrolling      = (HANDLE_STOP_SCROLLING == state);
637     const bool         isSmoothHandlePanEnabled = impl.mEventData->mDecorator->IsSmoothHandlePanEnabled();
638
639     if(HANDLE_PRESSED == state)
640     {
641       OnHandlePressed(impl, event, isSmoothHandlePanEnabled);
642     } // end ( HANDLE_PRESSED == state )
643     else if((HANDLE_RELEASED == state) ||
644             handleStopScrolling)
645     {
646       OnHandleReleased(impl, event, isSmoothHandlePanEnabled, handleStopScrolling);
647     } // end ( ( HANDLE_RELEASED == state ) || ( HANDLE_STOP_SCROLLING == state ) )
648     else if(HANDLE_SCROLLING == state)
649     {
650       OnHandleScrolling(impl, event, isSmoothHandlePanEnabled);
651     } // end ( HANDLE_SCROLLING == state )
652   }
653 }
654
655 void ControllerImplEventHandler::OnSelectEvent(Controller::Impl& impl, const Event& event)
656 {
657   if(impl.mEventData && impl.mEventData->mSelectionEnabled)
658   {
659     ModelPtr&      model          = impl.mModel;
660     const Vector2& scrollPosition = model->mScrollPosition;
661
662     // Convert from control's coords to text's coords.
663     const float xPosition = event.p2.mFloat - scrollPosition.x;
664     const float yPosition = event.p3.mFloat - scrollPosition.y;
665
666     // Calculates the logical position from the x,y coords.
667     impl.RepositionSelectionHandles(xPosition, yPosition, Controller::NoTextTap::HIGHLIGHT);
668   }
669 }
670
671 void ControllerImplEventHandler::OnSelectAllEvent(Controller::Impl& impl)
672 {
673   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "OnSelectAllEvent mEventData->mSelectionEnabled%s \n", impl.mEventData->mSelectionEnabled ? "true" : "false");
674
675   if(impl.mEventData)
676   {
677     EventData& eventData = *impl.mEventData;
678     if(eventData.mSelectionEnabled && eventData.mState != EventData::INACTIVE)
679     {
680       ModelPtr&      model          = impl.mModel;
681       const Vector2& scrollPosition = model->mScrollPosition;
682
683       // Calculates the logical position from the start.
684       impl.RepositionSelectionHandles(0.f - scrollPosition.x,
685                                       0.f - scrollPosition.y,
686                                       Controller::NoTextTap::HIGHLIGHT);
687
688       uint32_t oldStart = eventData.mLeftSelectionPosition;
689       uint32_t oldEnd   = eventData.mRightSelectionPosition;
690
691       eventData.mLeftSelectionPosition  = 0u;
692       eventData.mRightSelectionPosition = model->mLogicalModel->mText.Count();
693
694       if(impl.mSelectableControlInterface != nullptr)
695       {
696         impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
697       }
698     }
699   }
700 }
701
702 void ControllerImplEventHandler::OnSelectNoneEvent(Controller::Impl& impl)
703 {
704   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "OnSelectNoneEvent mEventData->mSelectionEnabled%s \n", impl.mEventData->mSelectionEnabled ? "true" : "false");
705
706   if(impl.mEventData)
707   {
708     EventData& eventData = *impl.mEventData;
709     if(eventData.mSelectionEnabled && eventData.mState == EventData::SELECTING)
710     {
711       uint32_t oldStart                = eventData.mLeftSelectionPosition;
712       uint32_t oldEnd                  = eventData.mRightSelectionPosition;
713
714       eventData.mLeftSelectionPosition = eventData.mRightSelectionPosition = eventData.mPrimaryCursorPosition;
715       impl.ChangeState(EventData::EDITING);
716       eventData.mUpdateCursorPosition      = true;
717       eventData.mUpdateInputStyle          = true;
718       eventData.mScrollAfterUpdatePosition = true;
719
720       if(impl.mSelectableControlInterface != nullptr)
721       {
722         impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
723       }
724     }
725   }
726 }
727
728 void ControllerImplEventHandler::OnSelectRangeEvent(Controller::Impl& impl, const Event& event)
729 {
730   if(impl.mEventData && impl.mEventData->mSelectionEnabled && impl.mEventData->mState != EventData::INACTIVE)
731   {
732     ModelPtr&      model          = impl.mModel;
733     const Vector2& scrollPosition = model->mScrollPosition;
734
735     // Calculate the selection index.
736     const uint32_t length = static_cast<uint32_t>(model->mLogicalModel->mText.Count());
737     const uint32_t start  = std::min(event.p2.mUint, length);
738     const uint32_t end    = std::min(event.p3.mUint, length);
739
740     if(start != end)
741     {
742       uint32_t oldStart = impl.mEventData->mLeftSelectionPosition;
743       uint32_t oldEnd   = impl.mEventData->mRightSelectionPosition;
744
745       // Calculates the logical position from the x,y coords.
746       impl.RepositionSelectionHandles(0.f - scrollPosition.x, 0.f - scrollPosition.y, Controller::NoTextTap::HIGHLIGHT);
747
748       impl.mEventData->mLeftSelectionPosition  = start;
749       impl.mEventData->mRightSelectionPosition = end;
750
751       if(impl.mSelectableControlInterface != nullptr)
752       {
753         impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, start, end);
754       }
755     }
756   }
757 }
758
759 void ControllerImplEventHandler::OnHandlePressed(Controller::Impl& impl, const Event& event, const bool isSmoothHandlePanEnabled)
760 {
761   ModelPtr&      model          = impl.mModel;
762   const Vector2& scrollPosition = model->mScrollPosition;
763
764   // Convert from decorator's coords to text's coords.
765   const float xPosition = event.p2.mFloat - scrollPosition.x;
766   const float yPosition = event.p3.mFloat - scrollPosition.y;
767
768   // Need to calculate the handle's new position.
769   bool                 matchedCharacter  = false;
770   const CharacterIndex handleNewPosition = Text::GetClosestCursorIndex(model->mVisualModel,
771                                                                        model->mLogicalModel,
772                                                                        impl.mMetrics,
773                                                                        xPosition,
774                                                                        yPosition,
775                                                                        CharacterHitTest::SCROLL,
776                                                                        matchedCharacter);
777
778   EventData& eventData = *impl.mEventData;
779   uint32_t   oldStart  = eventData.mLeftSelectionPosition;
780   uint32_t   oldEnd    = eventData.mRightSelectionPosition;
781
782   if(Event::GRAB_HANDLE_EVENT == event.type)
783   {
784     impl.ChangeState(EventData::GRAB_HANDLE_PANNING);
785
786     if(handleNewPosition != eventData.mPrimaryCursorPosition)
787     {
788       // Updates the cursor position if the handle's new position is different than the current one.
789       eventData.mUpdateCursorPosition = true;
790       // Does not update the grab handle position if the smooth panning is enabled. (The decorator does it smooth).
791       eventData.mUpdateGrabHandlePosition = !isSmoothHandlePanEnabled;
792       eventData.mPrimaryCursorPosition    = handleNewPosition;
793     }
794
795     // 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.
796     eventData.mDecoratorUpdated = isSmoothHandlePanEnabled;
797   }
798   else if(Event::LEFT_SELECTION_HANDLE_EVENT == event.type)
799   {
800     impl.ChangeState(EventData::SELECTION_HANDLE_PANNING);
801
802     if((handleNewPosition != eventData.mLeftSelectionPosition) &&
803        (handleNewPosition != eventData.mRightSelectionPosition))
804     {
805       // Updates the highlight box if the handle's new position is different than the current one.
806       eventData.mUpdateHighlightBox = true;
807       // Does not update the selection handle position if the smooth panning is enabled. (The decorator does it smooth).
808       eventData.mUpdateLeftSelectionPosition = !isSmoothHandlePanEnabled;
809       eventData.mLeftSelectionPosition       = handleNewPosition;
810     }
811
812     // 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.
813     eventData.mDecoratorUpdated = isSmoothHandlePanEnabled;
814
815     // Will define the order to scroll the text to match the handle position.
816     eventData.mIsLeftHandleSelected  = true;
817     eventData.mIsRightHandleSelected = false;
818   }
819   else if(Event::RIGHT_SELECTION_HANDLE_EVENT == event.type)
820   {
821     impl.ChangeState(EventData::SELECTION_HANDLE_PANNING);
822
823     if((handleNewPosition != eventData.mRightSelectionPosition) &&
824        (handleNewPosition != eventData.mLeftSelectionPosition))
825     {
826       // Updates the highlight box if the handle's new position is different than the current one.
827       eventData.mUpdateHighlightBox = true;
828       // Does not update the selection handle position if the smooth panning is enabled. (The decorator does it smooth).
829       eventData.mUpdateRightSelectionPosition = !isSmoothHandlePanEnabled;
830       eventData.mRightSelectionPosition       = handleNewPosition;
831     }
832
833     // 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.
834     eventData.mDecoratorUpdated = isSmoothHandlePanEnabled;
835
836     // Will define the order to scroll the text to match the handle position.
837     eventData.mIsLeftHandleSelected  = false;
838     eventData.mIsRightHandleSelected = true;
839   }
840
841   if((impl.mSelectableControlInterface != nullptr) && ((oldStart != eventData.mLeftSelectionPosition) || (oldEnd != eventData.mRightSelectionPosition)))
842   {
843     impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
844   }
845 }
846
847 void ControllerImplEventHandler::OnHandleReleased(Controller::Impl& impl, const Event& event, const bool isSmoothHandlePanEnabled, const bool handleStopScrolling)
848 {
849   CharacterIndex handlePosition = 0u;
850   if(handleStopScrolling || isSmoothHandlePanEnabled)
851   {
852     ModelPtr&      model          = impl.mModel;
853     const Vector2& scrollPosition = model->mScrollPosition;
854
855     // Convert from decorator's coords to text's coords.
856     const float xPosition = event.p2.mFloat - scrollPosition.x;
857     const float yPosition = event.p3.mFloat - scrollPosition.y;
858
859     bool matchedCharacter = false;
860     handlePosition        = Text::GetClosestCursorIndex(model->mVisualModel,
861                                                  model->mLogicalModel,
862                                                  impl.mMetrics,
863                                                  xPosition,
864                                                  yPosition,
865                                                  CharacterHitTest::SCROLL,
866                                                  matchedCharacter);
867   }
868
869   EventData& eventData = *impl.mEventData;
870   uint32_t   oldStart  = eventData.mLeftSelectionPosition;
871   uint32_t   oldEnd    = eventData.mRightSelectionPosition;
872
873   if(Event::GRAB_HANDLE_EVENT == event.type)
874   {
875     eventData.mUpdateCursorPosition     = true;
876     eventData.mUpdateGrabHandlePosition = true;
877     eventData.mUpdateInputStyle         = true;
878
879     if(!impl.IsClipboardEmpty())
880     {
881       impl.ChangeState(EventData::EDITING_WITH_PASTE_POPUP); // Moving grabhandle will show Paste Popup
882     }
883
884     if(handleStopScrolling || isSmoothHandlePanEnabled)
885     {
886       eventData.mScrollAfterUpdatePosition = true;
887       eventData.mPrimaryCursorPosition     = handlePosition;
888     }
889   }
890   else if(Event::LEFT_SELECTION_HANDLE_EVENT == event.type)
891   {
892     impl.ChangeState(EventData::SELECTING);
893
894     eventData.mUpdateHighlightBox           = true;
895     eventData.mUpdateLeftSelectionPosition  = true;
896     eventData.mUpdateRightSelectionPosition = true;
897
898     if(handleStopScrolling || isSmoothHandlePanEnabled)
899     {
900       eventData.mScrollAfterUpdatePosition = true;
901
902       if((handlePosition != eventData.mRightSelectionPosition) &&
903          (handlePosition != eventData.mLeftSelectionPosition))
904       {
905         eventData.mLeftSelectionPosition = handlePosition;
906       }
907     }
908   }
909   else if(Event::RIGHT_SELECTION_HANDLE_EVENT == event.type)
910   {
911     impl.ChangeState(EventData::SELECTING);
912
913     eventData.mUpdateHighlightBox           = true;
914     eventData.mUpdateRightSelectionPosition = true;
915     eventData.mUpdateLeftSelectionPosition  = true;
916
917     if(handleStopScrolling || isSmoothHandlePanEnabled)
918     {
919       eventData.mScrollAfterUpdatePosition = true;
920       if((handlePosition != eventData.mRightSelectionPosition) &&
921          (handlePosition != eventData.mLeftSelectionPosition))
922       {
923         eventData.mRightSelectionPosition = handlePosition;
924       }
925     }
926   }
927
928   if((impl.mSelectableControlInterface != nullptr) && ((oldStart != eventData.mLeftSelectionPosition) || (oldEnd != eventData.mRightSelectionPosition)))
929   {
930     impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
931   }
932
933   eventData.mDecoratorUpdated = true;
934 }
935
936 void ControllerImplEventHandler::OnHandleScrolling(Controller::Impl& impl, const Event& event, const bool isSmoothHandlePanEnabled)
937 {
938   ModelPtr&       model          = impl.mModel;
939   Vector2&        scrollPosition = model->mScrollPosition;
940   VisualModelPtr& visualModel    = model->mVisualModel;
941
942   const float    xSpeed                = event.p2.mFloat;
943   const float    ySpeed                = event.p3.mFloat;
944   const Vector2& layoutSize            = visualModel->GetLayoutSize();
945   const Vector2  currentScrollPosition = scrollPosition;
946
947   scrollPosition.x += xSpeed;
948   scrollPosition.y += ySpeed;
949
950   impl.ClampHorizontalScroll(layoutSize);
951   impl.ClampVerticalScroll(layoutSize);
952
953   EventData&    eventData = *impl.mEventData;
954   DecoratorPtr& decorator = eventData.mDecorator;
955
956   bool endOfScroll = false;
957   if(Vector2::ZERO == (currentScrollPosition - scrollPosition))
958   {
959     // Notify the decorator there is no more text to scroll.
960     // The decorator won't send more scroll events.
961     decorator->NotifyEndOfScroll();
962     // Still need to set the position of the handle.
963     endOfScroll = true;
964   }
965
966   // Set the position of the handle.
967   const bool scrollRightDirection      = xSpeed > 0.f;
968   const bool scrollBottomDirection     = ySpeed > 0.f;
969   const bool leftSelectionHandleEvent  = Event::LEFT_SELECTION_HANDLE_EVENT == event.type;
970   const bool rightSelectionHandleEvent = Event::RIGHT_SELECTION_HANDLE_EVENT == event.type;
971
972   if(Event::GRAB_HANDLE_EVENT == event.type)
973   {
974     impl.ChangeState(EventData::GRAB_HANDLE_PANNING);
975
976     // Get the grab handle position in decorator coords.
977     Vector2 position = decorator->GetPosition(GRAB_HANDLE);
978
979     if(decorator->IsHorizontalScrollEnabled())
980     {
981       // Position the grag handle close to either the left or right edge.
982       position.x = scrollRightDirection ? 0.f : visualModel->mControlSize.width;
983     }
984
985     if(decorator->IsVerticalScrollEnabled())
986     {
987       position.x = eventData.mCursorHookPositionX;
988
989       // Position the grag handle close to either the top or bottom edge.
990       position.y = scrollBottomDirection ? 0.f : visualModel->mControlSize.height;
991     }
992
993     // Get the new handle position.
994     // The grab handle's position is in decorator's coords. Need to transforms to text's coords.
995     bool                 matchedCharacter = false;
996     const CharacterIndex handlePosition   = Text::GetClosestCursorIndex(visualModel,
997                                                                       impl.mModel->mLogicalModel,
998                                                                       impl.mMetrics,
999                                                                       position.x - scrollPosition.x,
1000                                                                       position.y - scrollPosition.y,
1001                                                                       CharacterHitTest::SCROLL,
1002                                                                       matchedCharacter);
1003
1004     if(eventData.mPrimaryCursorPosition != handlePosition)
1005     {
1006       eventData.mUpdateCursorPosition      = true;
1007       eventData.mUpdateGrabHandlePosition  = !isSmoothHandlePanEnabled;
1008       eventData.mScrollAfterUpdatePosition = true;
1009       eventData.mPrimaryCursorPosition     = handlePosition;
1010     }
1011     eventData.mUpdateInputStyle = eventData.mUpdateCursorPosition;
1012
1013     // Updates the decorator if the soft handle panning is enabled.
1014     eventData.mDecoratorUpdated = isSmoothHandlePanEnabled;
1015   }
1016   else if(leftSelectionHandleEvent || rightSelectionHandleEvent)
1017   {
1018     impl.ChangeState(EventData::SELECTION_HANDLE_PANNING);
1019
1020     // Get the selection handle position in decorator coords.
1021     Vector2 position = decorator->GetPosition(leftSelectionHandleEvent ? Text::LEFT_SELECTION_HANDLE : Text::RIGHT_SELECTION_HANDLE);
1022
1023     if(decorator->IsHorizontalScrollEnabled())
1024     {
1025       // Position the selection handle close to either the left or right edge.
1026       position.x = scrollRightDirection ? 0.f : visualModel->mControlSize.width;
1027     }
1028
1029     if(decorator->IsVerticalScrollEnabled())
1030     {
1031       position.x = eventData.mCursorHookPositionX;
1032
1033       // Position the grag handle close to either the top or bottom edge.
1034       position.y = scrollBottomDirection ? 0.f : visualModel->mControlSize.height;
1035     }
1036
1037     // Get the new handle position.
1038     // The selection handle's position is in decorator's coords. Need to transform to text's coords.
1039     bool                 matchedCharacter = false;
1040     const CharacterIndex handlePosition   = Text::GetClosestCursorIndex(visualModel,
1041                                                                       impl.mModel->mLogicalModel,
1042                                                                       impl.mMetrics,
1043                                                                       position.x - scrollPosition.x,
1044                                                                       position.y - scrollPosition.y,
1045                                                                       CharacterHitTest::SCROLL,
1046                                                                       matchedCharacter);
1047     uint32_t             oldStart         = eventData.mLeftSelectionPosition;
1048     uint32_t             oldEnd           = eventData.mRightSelectionPosition;
1049
1050     if(leftSelectionHandleEvent)
1051     {
1052       const bool differentHandles = (eventData.mLeftSelectionPosition != handlePosition) && (eventData.mRightSelectionPosition != handlePosition);
1053
1054       if(differentHandles || endOfScroll)
1055       {
1056         eventData.mUpdateHighlightBox           = true;
1057         eventData.mUpdateLeftSelectionPosition  = !isSmoothHandlePanEnabled;
1058         eventData.mUpdateRightSelectionPosition = isSmoothHandlePanEnabled;
1059         eventData.mLeftSelectionPosition        = handlePosition;
1060       }
1061     }
1062     else
1063     {
1064       const bool differentHandles = (eventData.mRightSelectionPosition != handlePosition) && (eventData.mLeftSelectionPosition != handlePosition);
1065       if(differentHandles || endOfScroll)
1066       {
1067         eventData.mUpdateHighlightBox           = true;
1068         eventData.mUpdateRightSelectionPosition = !isSmoothHandlePanEnabled;
1069         eventData.mUpdateLeftSelectionPosition  = isSmoothHandlePanEnabled;
1070         eventData.mRightSelectionPosition       = handlePosition;
1071       }
1072     }
1073
1074     if(eventData.mUpdateLeftSelectionPosition || eventData.mUpdateRightSelectionPosition)
1075     {
1076       impl.RepositionSelectionHandles();
1077
1078       eventData.mScrollAfterUpdatePosition = !isSmoothHandlePanEnabled;
1079
1080       if(impl.mSelectableControlInterface != nullptr)
1081       {
1082         impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition);
1083       }
1084     }
1085   }
1086   eventData.mDecoratorUpdated = true;
1087 }
1088
1089 } // namespace Text
1090
1091 } // namespace Toolkit
1092
1093 } // namespace Dali