2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/actors/actor.h>
24 #include <dali/public-api/adaptor-framework/timer.h>
25 #include <dali/public-api/actors/image-actor.h>
26 #include <dali/public-api/actors/layer.h>
27 #include <dali/public-api/animation/constraint.h>
28 #include <dali/public-api/common/constants.h>
29 #include <dali/public-api/common/stage.h>
30 #include <dali/public-api/events/tap-gesture.h>
31 #include <dali/public-api/events/tap-gesture-detector.h>
32 #include <dali/public-api/events/touch-event.h>
33 #include <dali/public-api/events/pan-gesture.h>
34 #include <dali/public-api/events/pan-gesture-detector.h>
35 #include <dali/public-api/images/resource-image.h>
36 #include <dali/public-api/math/rect.h>
37 #include <dali/public-api/math/vector2.h>
38 #include <dali/public-api/math/vector4.h>
39 #include <dali/public-api/object/property-notification.h>
40 #include <dali/public-api/signals/connection-tracker.h>
43 #include <dali-toolkit/public-api/controls/control.h>
44 #include <dali-toolkit/public-api/controls/control-depth-index-ranges.h>
45 #include <dali-toolkit/public-api/controls/control-impl.h>
46 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
47 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
48 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
49 #include <dali-toolkit/devel-api/controls/text-controls/text-selection-popup.h>
52 #define DECORATOR_DEBUG
62 #ifdef DECORATOR_DEBUG
63 Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_TEXT_DECORATOR") );
74 const char* DEFAULT_GRAB_HANDLE_IMAGE_RELEASED( DALI_IMAGE_DIR "insertpoint-icon.png" );
75 const char* DEFAULT_GRAB_HANDLE_IMAGE_PRESSED( DALI_IMAGE_DIR "insertpoint-icon-pressed.png" );
76 const char* DEFAULT_SELECTION_HANDLE_ONE( DALI_IMAGE_DIR "text-input-selection-handle-left.png" );
77 const char* DEFAULT_SELECTION_HANDLE_TWO( DALI_IMAGE_DIR "text-input-selection-handle-right.png" );
79 const Dali::Vector3 DEFAULT_GRAB_HANDLE_RELATIVE_SIZE( 1.5f, 2.0f, 1.0f );
80 const Dali::Vector3 DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE( 1.5f, 1.5f, 1.0f );
82 const Dali::Vector4 LIGHT_BLUE( 0.07f, 0.41f, 0.59f, 1.0f ); // The text highlight color.
84 const unsigned int CURSOR_BLINK_INTERVAL = 500u; // Cursor blink interval
85 const float TO_MILLISECONDS = 1000.f;
86 const float TO_SECONDS = 1.f / TO_MILLISECONDS;
88 const float DISPLAYED_HIGHLIGHT_Z_OFFSET( -0.05f );
90 const unsigned int SCROLL_TICK_INTERVAL = 50u;
92 const float SCROLL_THRESHOLD = 10.f;
93 const float SCROLL_SPEED = 300.f;
94 const float SCROLL_DISTANCE = SCROLL_SPEED * SCROLL_TICK_INTERVAL * TO_SECONDS;
97 * structure to hold coordinates of each quad, which will make up the mesh.
99 struct QuadCoordinates
102 * Default constructor
110 * @param[in] x1 left co-ordinate
111 * @param[in] y1 top co-ordinate
112 * @param[in] x2 right co-ordinate
113 * @param[in] y2 bottom co-ordinate
115 QuadCoordinates(float x1, float y1, float x2, float y2)
121 Dali::Vector2 min; ///< top-left (minimum) position of quad
122 Dali::Vector2 max; ///< bottom-right (maximum) position of quad
125 typedef std::vector<QuadCoordinates> QuadContainer;
128 * @brief Takes a bounding rectangle in the local coordinates of an actor and returns the world coordinates Bounding Box.
129 * @param[in] boundingRectangle local bounding
130 * @param[out] Vector4 World coordinate bounding Box.
132 void LocalToWorldCoordinatesBoundingBox( const Dali::Rect<int>& boundingRectangle, Dali::Vector4& boundingBox )
134 // Convert to world coordinates and store as a Vector4 to be compatible with Property Notifications.
135 Dali::Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
137 const float originX = boundingRectangle.x - 0.5f * stageSize.width;
138 const float originY = boundingRectangle.y - 0.5f * stageSize.height;
140 boundingBox = Dali::Vector4( originX,
142 originX + boundingRectangle.width,
143 originY + boundingRectangle.height );
147 } // end of namespace
158 struct Decorator::Impl : public ConnectionTracker
172 : color( Dali::Color::BLACK ),
174 cursorHeight( 0.0f ),
190 grabDisplacementX( 0.f ),
191 grabDisplacementY( 0.f ),
202 float lineHeight; ///< Not the handle height
203 float grabDisplacementX;
204 float grabDisplacementY;
210 Impl( Dali::Toolkit::Internal::Control& parent, Observer& observer )
211 : mTextControlParent( parent ),
212 mObserver( observer ),
213 mBoundingBox( Rect<int>() ),
214 mHighlightColor( LIGHT_BLUE ),
215 mActiveCursor( ACTIVE_CURSOR_NONE ),
216 mCursorBlinkInterval( CURSOR_BLINK_INTERVAL ),
217 mCursorBlinkDuration( 0.0f ),
218 mHandleScrolling( HANDLE_TYPE_COUNT ),
219 mScrollDirection( SCROLL_NONE ),
220 mScrollThreshold( SCROLL_THRESHOLD ),
221 mScrollSpeed( SCROLL_SPEED ),
222 mScrollDistance( SCROLL_DISTANCE ),
223 mActiveCopyPastePopup( false ),
224 mCursorBlinkStatus( true ),
225 mPrimaryCursorVisible( false ),
226 mSecondaryCursorVisible( false )
231 * Relayout of the decorations owned by the decorator.
232 * @param[in] size The Size of the UI control the decorator is adding it's decorations to.
234 void Relayout( const Vector2& size )
236 // TODO - Remove this if nothing is active
240 // Show or hide the cursors
244 const CursorImpl& cursor = mCursor[PRIMARY_CURSOR];
245 mPrimaryCursorVisible = ( cursor.position.x <= size.width ) && ( cursor.position.x >= 0.f );
246 if( mPrimaryCursorVisible )
248 Vector2 position = cursor.position;
249 if( GRAB_HANDLE == mHandleScrolling )
251 if( mScrollDirection == SCROLL_RIGHT )
257 position.x = size.width;
261 mPrimaryCursor.SetPosition( position.x,
263 mPrimaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
265 mPrimaryCursor.SetVisible( mPrimaryCursorVisible );
267 if( mSecondaryCursor )
269 const CursorImpl& cursor = mCursor[SECONDARY_CURSOR];
270 mSecondaryCursorVisible = ( cursor.position.x <= size.width ) && ( cursor.position.x >= 0.f );
271 if( mSecondaryCursorVisible )
273 mSecondaryCursor.SetPosition( cursor.position.x,
275 mSecondaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
277 mSecondaryCursor.SetVisible( mSecondaryCursorVisible );
280 // Show or hide the grab handle
281 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
282 if( grabHandle.active )
284 Vector2 position = grabHandle.position;
286 if( GRAB_HANDLE == mHandleScrolling )
288 if( mScrollDirection == SCROLL_RIGHT )
294 position.x = size.width;
298 const bool isVisible = ( position.x <= size.width ) && ( position.x >= 0.f );
306 grabHandle.actor.SetPosition( position.x,
307 position.y + grabHandle.lineHeight );
309 grabHandle.actor.SetVisible( isVisible );
311 else if( grabHandle.actor )
313 UnparentAndReset( grabHandle.actor );
316 // Show or hide the selection handles/highlight
317 HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
318 HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
319 if( primary.active || secondary.active )
323 CreateSelectionHandles();
325 primary.actor.SetPosition( primary.position.x,
326 primary.position.y + primary.lineHeight );
328 secondary.actor.SetPosition( secondary.position.x,
329 secondary.position.y + secondary.lineHeight );
336 UnparentAndReset( primary.actor );
337 UnparentAndReset( secondary.actor );
338 //UnparentAndReset( mHighlightMeshActor );
341 if ( mActiveCopyPastePopup )
343 if ( !mCopyPastePopup )
345 mCopyPastePopup = TextSelectionPopup::New();
346 #ifdef DECORATOR_DEBUG
347 mCopyPastePopup.SetName("mCopyPastePopup");
349 mCopyPastePopup.SetAnchorPoint( AnchorPoint::CENTER );
350 mCopyPastePopup.OnRelayoutSignal().Connect( this, &Decorator::Impl::PopUpRelayoutComplete ); // Position popup after size negotiation
351 mActiveLayer.Add ( mCopyPastePopup );
356 if ( mCopyPastePopup )
358 UnparentAndReset( mCopyPastePopup );
363 void UpdatePositions( const Vector2& scrollOffset )
365 mCursor[PRIMARY_CURSOR].position += scrollOffset;
366 mCursor[SECONDARY_CURSOR].position += scrollOffset;
367 mHandle[ GRAB_HANDLE ].position += scrollOffset;
368 mHandle[ LEFT_SELECTION_HANDLE ].position += scrollOffset;
369 mHandle[ RIGHT_SELECTION_HANDLE ].position += scrollOffset;
371 // TODO Highlight box??
374 void PopUpRelayoutComplete( Actor actor )
376 // Size negotiation for CopyPastePopup complete so can get the size and constrain position within bounding box.
378 mCopyPastePopup.OnRelayoutSignal().Disconnect( this, &Decorator::Impl::PopUpRelayoutComplete );
380 Vector3 popupPosition( mCursor[PRIMARY_CURSOR].position.x, mCursor[PRIMARY_CURSOR].position.y -100.0f , 0.0f); //todo 100 to be an offset Property
382 Vector3 popupSize = Vector3( mCopyPastePopup.GetRelayoutSize( Dimension::WIDTH ), mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT ), 0.0f );
384 GetConstrainedPopupPosition( popupPosition, popupSize, AnchorPoint::CENTER, mActiveLayer, mBoundingBox );
386 SetUpPopUpPositionNotifications();
388 mCopyPastePopup.SetPosition( popupPosition ); //todo grabhandle(cursor) or selection handle positions to be used
391 void CreateCursor( ImageActor& cursor, const Vector4& color )
393 cursor = CreateSolidColorActor( color );
394 cursor.SetDepthIndex( DECORATION_DEPTH_INDEX );
395 cursor.SetParentOrigin( ParentOrigin::TOP_LEFT ); // Need to set the default parent origin as CreateSolidColorActor() sets a different one.
396 cursor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
399 // Add or Remove cursor(s) from parent
403 if( mActiveCursor == ACTIVE_CURSOR_NONE )
405 UnparentAndReset( mPrimaryCursor );
406 UnparentAndReset( mSecondaryCursor );
410 // Create Primary and or Secondary Cursor(s) if active and add to parent
411 if ( mActiveCursor == ACTIVE_CURSOR_PRIMARY ||
412 mActiveCursor == ACTIVE_CURSOR_BOTH )
414 if ( !mPrimaryCursor )
416 CreateCursor( mPrimaryCursor, mCursor[PRIMARY_CURSOR].color );
417 #ifdef DECORATOR_DEBUG
418 mPrimaryCursor.SetName( "PrimaryCursorActor" );
420 mActiveLayer.Add( mPrimaryCursor );
424 if ( mActiveCursor == ACTIVE_CURSOR_BOTH )
426 if ( !mSecondaryCursor )
428 CreateCursor( mSecondaryCursor, mCursor[SECONDARY_CURSOR].color );
429 #ifdef DECORATOR_DEBUG
430 mSecondaryCursor.SetName( "SecondaryCursorActor" );
432 mActiveLayer.Add( mSecondaryCursor );
437 UnparentAndReset( mSecondaryCursor );
443 bool OnCursorBlinkTimerTick()
447 if ( mPrimaryCursor )
449 mPrimaryCursor.SetVisible( mPrimaryCursorVisible && mCursorBlinkStatus );
451 if ( mSecondaryCursor )
453 mSecondaryCursor.SetVisible( mSecondaryCursorVisible && mCursorBlinkStatus );
456 mCursorBlinkStatus = !mCursorBlinkStatus;
461 void SetupTouchEvents()
465 mTapDetector = TapGestureDetector::New();
466 mTapDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnTap );
469 if ( !mPanGestureDetector )
471 mPanGestureDetector = PanGestureDetector::New();
472 mPanGestureDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnPan );
476 void CreateActiveLayer()
480 Actor parent = mTextControlParent.Self();
482 mActiveLayer = Layer::New();
483 #ifdef DECORATOR_DEBUG
484 mActiveLayer.SetName ( "ActiveLayerActor" );
487 mActiveLayer.SetParentOrigin( ParentOrigin::CENTER );
488 mActiveLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
489 mActiveLayer.SetPositionInheritanceMode( USE_PARENT_POSITION );
491 parent.Add( mActiveLayer );
494 mActiveLayer.RaiseToTop();
497 void CreateGrabHandle()
499 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
500 if( !grabHandle.actor )
502 if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] )
504 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_RELEASED );
506 if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] )
508 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_PRESSED );
511 grabHandle.actor = ImageActor::New( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] );
512 grabHandle.actor.SetDepthIndex( DECORATION_DEPTH_INDEX );
513 grabHandle.actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
514 // Area that Grab handle responds to, larger than actual handle so easier to move
515 #ifdef DECORATOR_DEBUG
516 grabHandle.actor.SetName( "GrabHandleActor" );
517 if ( Dali::Internal::gLogFilter->IsEnabledFor( Debug::Verbose ) )
519 grabHandle.grabArea = Toolkit::CreateSolidColorActor( Vector4(0.0f, 0.0f, 0.0f, 0.0f), true, Color::RED, 1 );
520 grabHandle.grabArea.SetName( "GrabArea" );
524 grabHandle.grabArea = Actor::New();
525 grabHandle.grabArea.SetName( "GrabArea" );
528 grabHandle.grabArea = Actor::New();
531 grabHandle.grabArea.SetParentOrigin( ParentOrigin::TOP_CENTER );
532 grabHandle.grabArea.SetAnchorPoint( AnchorPoint::TOP_CENTER );
533 grabHandle.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
534 grabHandle.grabArea.SetSizeModeFactor( DEFAULT_GRAB_HANDLE_RELATIVE_SIZE );
535 grabHandle.actor.Add( grabHandle.grabArea );
537 grabHandle.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnGrabHandleTouched );
538 mTapDetector.Attach( grabHandle.grabArea );
539 mPanGestureDetector.Attach( grabHandle.grabArea );
541 mActiveLayer.Add( grabHandle.actor );
545 void CreateSelectionHandles()
547 HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
550 if( !mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
552 mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
555 primary.actor = ImageActor::New( mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
556 #ifdef DECORATOR_DEBUG
557 primary.actor.SetName("SelectionHandleOne");
559 primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
560 primary.actor.SetDepthIndex( DECORATION_DEPTH_INDEX );
561 primary.flipped = false;
563 primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
564 #ifdef DECORATOR_DEBUG
565 primary.grabArea.SetName("SelectionHandleOneGrabArea");
567 primary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
568 primary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
569 primary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
571 mTapDetector.Attach( primary.grabArea );
572 mPanGestureDetector.Attach( primary.grabArea );
573 primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
575 primary.actor.Add( primary.grabArea );
576 mActiveLayer.Add( primary.actor );
579 HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
580 if( !secondary.actor )
582 if( !mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
584 mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
587 secondary.actor = ImageActor::New( mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
588 #ifdef DECORATOR_DEBUG
589 secondary.actor.SetName("SelectionHandleTwo");
591 secondary.actor.SetAnchorPoint( AnchorPoint::TOP_LEFT ); // Change to BOTTOM_LEFT if Look'n'Feel requires handle above text.
592 secondary.actor.SetDepthIndex( DECORATION_DEPTH_INDEX );
593 secondary.flipped = false;
595 secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
596 #ifdef DECORATOR_DEBUG
597 secondary.grabArea.SetName("SelectionHandleTwoGrabArea");
599 secondary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
600 secondary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
601 secondary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
603 mTapDetector.Attach( secondary.grabArea );
604 mPanGestureDetector.Attach( secondary.grabArea );
605 secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
607 secondary.actor.Add( secondary.grabArea );
608 mActiveLayer.Add( secondary.actor );
612 void CreateHighlight()
615 if ( !mHighlightMeshActor )
617 mHighlightMaterial = Material::New( "HighlightMaterial" );
618 mHighlightMaterial.SetDiffuseColor( mHighlightColor );
620 mHighlightMeshData.SetMaterial( mHighlightMaterial );
621 mHighlightMeshData.SetHasNormals( true );
623 mHighlightMesh = Mesh::New( mHighlightMeshData );
625 mHighlightMeshActor = MeshActor::New( mHighlightMesh );
626 #ifdef DECORATOR_DEBUG
627 mHighlightMeshActor.SetName( "HighlightMeshActor" );
629 mHighlightMeshActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
630 mHighlightMeshActor.SetPosition( 0.0f, 0.0f, DISPLAYED_HIGHLIGHT_Z_OFFSET );
632 Actor parent = mTextControlParent.Self();
633 parent.Add( mHighlightMeshActor );
638 void UpdateHighlight()
640 // Construct a Mesh with a texture to be used as the highlight 'box' for selected text
642 // Example scenarios where mesh is made from 3, 1, 2, 2 ,3 or 3 quads.
644 // [ TOP ] [ TOP ] [TOP ] [ TOP ] [ TOP ] [ TOP ]
645 // [ MIDDLE ] [BOTTOM] [BOTTOM] [ MIDDLE ] [ MIDDLE ]
646 // [ BOTTOM] [ MIDDLE ] [ MIDDLE ]
647 // [BOTTOM] [ MIDDLE ]
650 // Each quad is created as 2 triangles.
651 // Middle is just 1 quad regardless of its size.
666 if ( mHighlightMesh && mHighlightMaterial && !mHighlightQuadList.empty() )
668 MeshData::VertexContainer vertices;
669 Dali::MeshData::FaceIndices faceIndices;
671 std::vector<QuadCoordinates>::iterator iter = mHighlightQuadList.begin();
672 std::vector<QuadCoordinates>::iterator endIter = mHighlightQuadList.end();
674 // vertex position defaults to (0 0 0)
675 MeshData::Vertex vertex;
676 // set normal for all vertices as (0 0 1) pointing outward from TextInput Actor.
679 for(std::size_t v = 0; iter != endIter; ++iter,v+=4 )
681 // Add each quad geometry (a sub-selection) to the mesh data.
691 QuadCoordinates& quad = *iter;
693 vertex.x = quad.min.x;
694 vertex.y = quad.min.y;
695 vertices.push_back( vertex );
698 vertex.x = quad.max.x;
699 vertex.y = quad.min.y;
700 vertices.push_back( vertex );
703 vertex.x = quad.min.x;
704 vertex.y = quad.max.y;
705 vertices.push_back( vertex );
707 // bottom-right (v+3)
708 vertex.x = quad.max.x;
709 vertex.y = quad.max.y;
710 vertices.push_back( vertex );
712 // triangle A (3, 1, 0)
713 faceIndices.push_back( v + 3 );
714 faceIndices.push_back( v + 1 );
715 faceIndices.push_back( v );
717 // triangle B (0, 2, 3)
718 faceIndices.push_back( v );
719 faceIndices.push_back( v + 2 );
720 faceIndices.push_back( v + 3 );
722 mHighlightMeshData.SetFaceIndices( faceIndices );
725 BoneContainer bones(0); // passed empty as bones not required
726 mHighlightMeshData.SetData( vertices, faceIndices, bones, mHighlightMaterial );
727 mHighlightMesh.UpdateMeshData( mHighlightMeshData );
732 void OnTap( Actor actor, const TapGesture& tap )
734 if( actor == mHandle[GRAB_HANDLE].actor )
740 void DoPan( HandleImpl& handle, HandleType type, const PanGesture& gesture )
742 if( Gesture::Started == gesture.state )
744 handle.grabDisplacementX = handle.grabDisplacementY = 0;
747 handle.grabDisplacementX += gesture.displacement.x;
748 handle.grabDisplacementY += gesture.displacement.y;
750 const float x = handle.position.x + handle.grabDisplacementX;
751 const float y = handle.position.y + handle.lineHeight*0.5f + handle.grabDisplacementY;
753 if( Gesture::Started == gesture.state ||
754 Gesture::Continuing == gesture.state )
756 if( x < mScrollThreshold )
758 mScrollDirection = SCROLL_RIGHT;
759 mHandleScrolling = type;
762 else if( x > mTextControlParent.Self().GetTargetSize().width - mScrollThreshold )
764 mScrollDirection = SCROLL_LEFT;
765 mHandleScrolling = type;
770 mHandleScrolling = HANDLE_TYPE_COUNT;
772 mObserver.HandleEvent( type, HANDLE_PRESSED, x, y );
775 else if( Gesture::Finished == gesture.state ||
776 Gesture::Cancelled == gesture.state )
778 if( mScrollTimer && mScrollTimer.IsRunning() )
780 mHandleScrolling = HANDLE_TYPE_COUNT;
782 mObserver.HandleEvent( type, HANDLE_STOP_SCROLLING, x, y );
786 mObserver.HandleEvent( type, HANDLE_RELEASED, x, y );
788 handle.actor.SetImage( mHandleImages[type][HANDLE_IMAGE_RELEASED] );
792 void OnPan( Actor actor, const PanGesture& gesture )
794 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
795 HandleImpl& primarySelectionHandle = mHandle[LEFT_SELECTION_HANDLE];
796 HandleImpl& secondarySelectionHandle = mHandle[RIGHT_SELECTION_HANDLE];
798 if( actor == grabHandle.grabArea )
800 DoPan( grabHandle, GRAB_HANDLE, gesture );
802 else if( actor == primarySelectionHandle.grabArea )
804 DoPan( primarySelectionHandle, LEFT_SELECTION_HANDLE, gesture );
806 else if( actor == secondarySelectionHandle.grabArea )
808 DoPan( secondarySelectionHandle, RIGHT_SELECTION_HANDLE, gesture );
812 bool OnGrabHandleTouched( Actor actor, const TouchEvent& event )
814 // Switch between pressed/release grab-handle images
815 if( event.GetPointCount() > 0 &&
816 mHandle[GRAB_HANDLE].actor )
818 const TouchPoint& point = event.GetPoint(0);
820 if( TouchPoint::Down == point.state &&
821 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] )
823 mHandle[GRAB_HANDLE].actor.SetImage( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] );
825 else if( TouchPoint::Up == point.state &&
826 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] )
828 mHandle[GRAB_HANDLE].actor.SetImage( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] );
832 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
836 bool OnHandleOneTouched( Actor actor, const TouchEvent& touch )
839 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
843 bool OnHandleTwoTouched( Actor actor, const TouchEvent& touch )
846 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
852 float AlternatePopUpPositionRelativeToCursor()
854 float alternativePosition=0.0f;;
857 if ( mPrimaryCursor ) // Secondary cursor not used for paste
859 Cursor cursor = PRIMARY_CURSOR;
860 alternativePosition = mCursor[cursor].position.y;
863 const float popupHeight = 120.0f; // todo Set as a MaxSize Property in Control or retrieve from CopyPastePopup class.
865 if( mHandle[GRAB_HANDLE].active )
867 // If grab handle enabled then position pop-up below the grab handle.
868 const Vector2 grabHandleSize( 59.0f, 56.0f ); // todo
869 const float BOTTOM_HANDLE_BOTTOM_OFFSET = 1.5; //todo Should be a property
870 alternativePosition += grabHandleSize.height + popupHeight + BOTTOM_HANDLE_BOTTOM_OFFSET ;
874 alternativePosition += popupHeight;
877 return alternativePosition;
880 void PopUpLeavesVerticalBoundary( PropertyNotification& source )
882 float alternativeYPosition=0.0f;
883 // todo use AlternatePopUpPositionRelativeToSelectionHandles() if text is highlighted
884 // if can't be positioned above, then position below row.
885 alternativeYPosition = AlternatePopUpPositionRelativeToCursor();
887 mCopyPastePopup.SetY( alternativeYPosition );
891 void SetUpPopUpPositionNotifications( )
893 // Note Property notifications ignore any set anchor point so conditions must allow for this. Default is Top Left.
895 // Exceeding vertical boundary
897 Vector4 worldCoordinatesBoundingBox;
898 LocalToWorldCoordinatesBoundingBox( mBoundingBox, worldCoordinatesBoundingBox );
900 float popupHeight = mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT);
902 PropertyNotification verticalExceedNotification = mCopyPastePopup.AddPropertyNotification( Actor::Property::WORLD_POSITION_Y,
903 OutsideCondition( worldCoordinatesBoundingBox.y + popupHeight/2,
904 worldCoordinatesBoundingBox.w - popupHeight/2 ) );
906 verticalExceedNotification.NotifySignal().Connect( this, &Decorator::Impl::PopUpLeavesVerticalBoundary );
909 void GetConstrainedPopupPosition( Vector3& requiredPopupPosition, Vector3& popupSize, Vector3 anchorPoint, Actor& parent, Rect<int>& boundingBox )
911 DALI_ASSERT_DEBUG ( "Popup parent not on stage" && parent.OnStage() )
913 // Parent must already by added to Stage for these Get calls to work
914 Vector3 parentAnchorPoint = parent.GetCurrentAnchorPoint();
915 Vector3 parentWorldPositionLeftAnchor = parent.GetCurrentWorldPosition() - parent.GetCurrentSize()*parentAnchorPoint;
916 Vector3 popupWorldPosition = parentWorldPositionLeftAnchor + requiredPopupPosition; // Parent World position plus popup local position gives World Position
917 Vector3 popupDistanceFromAnchorPoint = popupSize*anchorPoint;
919 // Bounding rectangle is supplied as screen coordinates, bounding will be done in world coordinates.
920 Vector4 boundingRectangleWorld;
921 LocalToWorldCoordinatesBoundingBox( boundingBox, boundingRectangleWorld );
923 // Calculate distance to move popup (in local space) so fits within the boundary
924 float xOffSetToKeepWithinBounds = 0.0f;
925 if( popupWorldPosition.x - popupDistanceFromAnchorPoint.x < boundingRectangleWorld.x )
927 xOffSetToKeepWithinBounds = boundingRectangleWorld.x - ( popupWorldPosition.x - popupDistanceFromAnchorPoint.x );
929 else if ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x > boundingRectangleWorld.z )
931 xOffSetToKeepWithinBounds = boundingRectangleWorld.z - ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x );
934 // Ensure initial display of Popup is in alternative position if can not fit above. As Property notification will be a frame behind.
935 if ( popupWorldPosition.y - popupDistanceFromAnchorPoint.y < boundingRectangleWorld.y )
937 requiredPopupPosition.y = AlternatePopUpPositionRelativeToCursor();
940 requiredPopupPosition.x = requiredPopupPosition.x + xOffSetToKeepWithinBounds;
943 void SetScrollThreshold( float threshold )
945 mScrollThreshold = threshold;
948 float GetScrollThreshold() const
950 return mScrollThreshold;
953 void SetScrollSpeed( float speed )
955 mScrollSpeed = speed;
956 mScrollDistance = speed * SCROLL_TICK_INTERVAL * TO_SECONDS;
959 float GetScrollSpeed() const
965 * Creates and starts a timer to scroll the text when handles are close to the edges of the text.
967 * It only starts the timer if it's already created.
969 void StartScrollTimer()
973 mScrollTimer = Timer::New( SCROLL_TICK_INTERVAL );
974 mScrollTimer.TickSignal().Connect( this, &Decorator::Impl::OnScrollTimerTick );
977 if( !mScrollTimer.IsRunning() )
979 mScrollTimer.Start();
984 * Stops the timer used to scroll the text.
986 void StopScrollTimer()
995 * Callback called by the timer used to scroll the text.
997 * It calculates and sets a new scroll position.
999 bool OnScrollTimerTick()
1001 if( HANDLE_TYPE_COUNT != mHandleScrolling )
1003 mObserver.HandleEvent( mHandleScrolling,
1005 mScrollDirection == SCROLL_RIGHT ? mScrollDistance : -mScrollDistance,
1012 Internal::Control& mTextControlParent;
1013 Observer& mObserver;
1015 TapGestureDetector mTapDetector;
1016 PanGestureDetector mPanGestureDetector;
1017 Timer mCursorBlinkTimer; ///< Timer to signal cursor to blink
1018 Timer mScrollTimer; ///< Timer used to scroll the text when the grab handle is moved close to the edges.
1020 Layer mActiveLayer; ///< Layer for active handles and alike that ensures they are above all else.
1021 //ImageActor mPrimaryCursor;
1022 //ImageActor mSecondaryCursor;
1023 //MeshActor mHighlightMeshActor; ///< Mesh Actor to display highlight
1024 TextSelectionPopup mCopyPastePopup;
1026 Image mHandleImages[HANDLE_TYPE_COUNT][HANDLE_IMAGE_TYPE_COUNT];
1028 //Mesh mHighlightMesh; ///< Mesh for highlight
1029 //MeshData mHighlightMeshData; ///< Mesh Data for highlight
1030 //Material mHighlightMaterial; ///< Material used for highlight
1032 CursorImpl mCursor[CURSOR_COUNT];
1033 HandleImpl mHandle[HANDLE_TYPE_COUNT];
1034 QuadContainer mHighlightQuadList; ///< Sub-selections that combine to create the complete selection highlight
1036 Rect<int> mBoundingBox;
1037 Vector4 mHighlightColor; ///< Color of the highlight
1039 unsigned int mActiveCursor;
1040 unsigned int mCursorBlinkInterval;
1041 float mCursorBlinkDuration;
1042 HandleType mHandleScrolling; ///< The handle which is scrolling.
1043 ScrollDirection mScrollDirection; ///< The direction of the scroll.
1044 float mScrollThreshold; ///< Defines a square area inside the control, close to the edge. A cursor entering this area will trigger scroll events.
1045 float mScrollSpeed; ///< The scroll speed in pixels per second.
1046 float mScrollDistance; ///< Distance the text scrolls during a scroll interval.
1047 unsigned int mScrollInterval; ///< Time in milliseconds of a scroll interval.
1049 bool mActiveCopyPastePopup : 1;
1050 bool mCursorBlinkStatus : 1; ///< Flag to switch between blink on and blink off.
1051 bool mPrimaryCursorVisible : 1; ///< Whether the primary cursor is visible.
1052 bool mSecondaryCursorVisible : 1; ///< Whether the secondary cursor is visible.
1055 DecoratorPtr Decorator::New( Internal::Control& parent, Observer& observer )
1057 return DecoratorPtr( new Decorator(parent, observer) );
1060 void Decorator::SetBoundingBox( const Rect<int>& boundingBox )
1062 mImpl->mBoundingBox = boundingBox;
1065 const Rect<int>& Decorator::GetBoundingBox() const
1067 return mImpl->mBoundingBox;
1070 void Decorator::Relayout( const Vector2& size )
1072 mImpl->Relayout( size );
1075 void Decorator::UpdatePositions( const Vector2& scrollOffset )
1077 mImpl->UpdatePositions( scrollOffset );
1082 void Decorator::SetActiveCursor( ActiveCursor activeCursor )
1084 mImpl->mActiveCursor = activeCursor;
1087 unsigned int Decorator::GetActiveCursor() const
1089 return mImpl->mActiveCursor;
1092 void Decorator::SetPosition( Cursor cursor, float x, float y, float cursorHeight, float lineHeight )
1094 mImpl->mCursor[cursor].position.x = x;
1095 mImpl->mCursor[cursor].position.y = y;
1096 mImpl->mCursor[cursor].cursorHeight = cursorHeight;
1097 mImpl->mCursor[cursor].lineHeight = lineHeight;
1100 void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& cursorHeight, float& lineHeight ) const
1102 x = mImpl->mCursor[cursor].position.x;
1103 y = mImpl->mCursor[cursor].position.y;
1104 cursorHeight = mImpl->mCursor[cursor].cursorHeight;
1105 lineHeight = mImpl->mCursor[cursor].lineHeight;
1108 const Vector2& Decorator::GetPosition( Cursor cursor ) const
1110 return mImpl->mCursor[cursor].position;
1113 void Decorator::SetColor( Cursor cursor, const Dali::Vector4& color )
1115 mImpl->mCursor[cursor].color = color;
1118 const Dali::Vector4& Decorator::GetColor( Cursor cursor ) const
1120 return mImpl->mCursor[cursor].color;
1123 void Decorator::StartCursorBlink()
1125 if ( !mImpl->mCursorBlinkTimer )
1127 mImpl->mCursorBlinkTimer = Timer::New( mImpl->mCursorBlinkInterval );
1128 mImpl->mCursorBlinkTimer.TickSignal().Connect( mImpl, &Decorator::Impl::OnCursorBlinkTimerTick );
1131 if ( !mImpl->mCursorBlinkTimer.IsRunning() )
1133 mImpl->mCursorBlinkTimer.Start();
1137 void Decorator::StopCursorBlink()
1139 if ( mImpl->mCursorBlinkTimer )
1141 mImpl->mCursorBlinkTimer.Stop();
1145 void Decorator::SetCursorBlinkInterval( float seconds )
1147 mImpl->mCursorBlinkInterval = static_cast<unsigned int>( seconds * TO_MILLISECONDS ); // Convert to milliseconds
1150 float Decorator::GetCursorBlinkInterval() const
1152 return static_cast<float>( mImpl->mCursorBlinkInterval ) * TO_SECONDS;
1155 void Decorator::SetCursorBlinkDuration( float seconds )
1157 mImpl->mCursorBlinkDuration = seconds;
1160 float Decorator::GetCursorBlinkDuration() const
1162 return mImpl->mCursorBlinkDuration;
1167 void Decorator::SetHandleActive( HandleType handleType, bool active )
1169 mImpl->mHandle[handleType].active = active;
1172 bool Decorator::IsHandleActive( HandleType handleType ) const
1174 return mImpl->mHandle[handleType].active ;
1177 void Decorator::SetHandleImage( HandleType handleType, HandleImageType handleImageType, Dali::Image image )
1179 mImpl->mHandleImages[handleType][handleImageType] = image;
1182 Dali::Image Decorator::GetHandleImage( HandleType handleType, HandleImageType handleImageType ) const
1184 return mImpl->mHandleImages[handleType][handleImageType];
1187 void Decorator::SetPosition( HandleType handleType, float x, float y, float height )
1189 // Adjust grab handle displacement
1190 Impl::HandleImpl& handle = mImpl->mHandle[handleType];
1192 handle.grabDisplacementX -= x - handle.position.x;
1193 handle.grabDisplacementY -= y - handle.position.y;
1195 handle.position.x = x;
1196 handle.position.y = y;
1197 handle.lineHeight = height;
1200 void Decorator::GetPosition( HandleType handleType, float& x, float& y, float& height ) const
1202 Impl::HandleImpl& handle = mImpl->mHandle[handleType];
1204 x = handle.position.x;
1205 y = handle.position.y;
1206 height = handle.lineHeight;
1209 void Decorator::AddHighlight( float x1, float y1, float x2, float y2 )
1211 mImpl->mHighlightQuadList.push_back( QuadCoordinates(x1, y1, x2, y2) );
1214 void Decorator::ClearHighlights()
1216 mImpl->mHighlightQuadList.clear();
1219 void Decorator::SetHighlightColor( const Vector4& color )
1221 mImpl->mHighlightColor = color;
1224 const Vector4& Decorator::GetHighlightColor() const
1226 return mImpl->mHighlightColor;
1229 void Decorator::SetPopupActive( bool active )
1231 mImpl->mActiveCopyPastePopup = active;
1234 bool Decorator::IsPopupActive() const
1236 return mImpl->mActiveCopyPastePopup ;
1241 void Decorator::SetScrollThreshold( float threshold )
1243 mImpl->SetScrollThreshold( threshold );
1246 float Decorator::GetScrollThreshold() const
1248 return mImpl->GetScrollThreshold();
1251 void Decorator::SetScrollSpeed( float speed )
1253 mImpl->SetScrollSpeed( speed );
1256 float Decorator::GetScrollSpeed() const
1258 return mImpl->GetScrollSpeed();
1261 Decorator::~Decorator()
1266 Decorator::Decorator( Dali::Toolkit::Internal::Control& parent, Observer& observer )
1269 mImpl = new Decorator::Impl( parent, observer );
1274 } // namespace Toolkit