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