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/devel-api/actors/mesh-actor.h>
28 #include <dali/public-api/animation/constraint.h>
29 #include <dali/public-api/common/constants.h>
30 #include <dali/public-api/common/stage.h>
31 #include <dali/public-api/events/tap-gesture.h>
32 #include <dali/public-api/events/tap-gesture-detector.h>
33 #include <dali/public-api/events/touch-event.h>
34 #include <dali/public-api/events/pan-gesture.h>
35 #include <dali/public-api/events/pan-gesture-detector.h>
36 #include <dali/devel-api/geometry/mesh.h>
37 #include <dali/devel-api/geometry/mesh-data.h>
38 #include <dali/public-api/images/resource-image.h>
39 #include <dali/public-api/math/rect.h>
40 #include <dali/public-api/math/vector2.h>
41 #include <dali/public-api/math/vector4.h>
42 #include <dali/public-api/object/property-notification.h>
43 #include <dali/public-api/signals/connection-tracker.h>
46 #include <dali-toolkit/public-api/controls/control.h>
47 #include <dali-toolkit/public-api/controls/control-impl.h>
48 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
49 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
50 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
51 #include <dali-toolkit/devel-api/controls/text-controls/text-selection-popup.h>
54 #define DECORATOR_DEBUG
64 #ifdef DECORATOR_DEBUG
65 Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_TEXT_DECORATOR") );
76 const char* DEFAULT_GRAB_HANDLE_IMAGE_RELEASED( DALI_IMAGE_DIR "insertpoint-icon.png" );
77 const char* DEFAULT_GRAB_HANDLE_IMAGE_PRESSED( DALI_IMAGE_DIR "insertpoint-icon-pressed.png" );
78 const char* DEFAULT_SELECTION_HANDLE_ONE( DALI_IMAGE_DIR "text-input-selection-handle-left.png" );
79 const char* DEFAULT_SELECTION_HANDLE_TWO( DALI_IMAGE_DIR "text-input-selection-handle-right.png" );
81 const Dali::Vector3 DEFAULT_GRAB_HANDLE_RELATIVE_SIZE( 1.5f, 2.0f, 1.0f );
82 const Dali::Vector3 DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE( 1.5f, 1.5f, 1.0f );
84 const Dali::Vector4 LIGHT_BLUE( 0.07f, 0.41f, 0.59f, 1.0f ); // The text highlight color.
86 const unsigned int CURSOR_BLINK_INTERVAL = 500u; // Cursor blink interval
87 const float TO_MILLISECONDS = 1000.f;
88 const float TO_SECONDS = 1.f / TO_MILLISECONDS;
90 const float DISPLAYED_HIGHLIGHT_Z_OFFSET( -0.05f );
92 const unsigned int SCROLL_TICK_INTERVAL = 50u;
94 const float SCROLL_THRESHOLD = 10.f;
95 const float SCROLL_SPEED = 300.f;
96 const float SCROLL_DISTANCE = SCROLL_SPEED * SCROLL_TICK_INTERVAL * TO_SECONDS;
99 * structure to hold coordinates of each quad, which will make up the mesh.
101 struct QuadCoordinates
104 * Default constructor
112 * @param[in] x1 left co-ordinate
113 * @param[in] y1 top co-ordinate
114 * @param[in] x2 right co-ordinate
115 * @param[in] y2 bottom co-ordinate
117 QuadCoordinates(float x1, float y1, float x2, float y2)
123 Dali::Vector2 min; ///< top-left (minimum) position of quad
124 Dali::Vector2 max; ///< bottom-right (maximum) position of quad
127 typedef std::vector<QuadCoordinates> QuadContainer;
130 * @brief Takes a bounding rectangle in the local coordinates of an actor and returns the world coordinates Bounding Box.
131 * @param[in] boundingRectangle local bounding
132 * @param[out] Vector4 World coordinate bounding Box.
134 void LocalToWorldCoordinatesBoundingBox( const Dali::Rect<int>& boundingRectangle, Dali::Vector4& boundingBox )
136 // Convert to world coordinates and store as a Vector4 to be compatible with Property Notifications.
137 Dali::Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
139 const float originX = boundingRectangle.x - 0.5f * stageSize.width;
140 const float originY = boundingRectangle.y - 0.5f * stageSize.height;
142 boundingBox = Dali::Vector4( originX,
144 originX + boundingRectangle.width,
145 originY + boundingRectangle.height );
149 } // end of namespace
160 struct Decorator::Impl : public ConnectionTracker
174 : color( Dali::Color::BLACK ),
176 cursorHeight( 0.0f ),
192 grabDisplacementX( 0.f ),
193 grabDisplacementY( 0.f ),
204 float lineHeight; ///< Not the handle height
205 float grabDisplacementX;
206 float grabDisplacementY;
212 Impl( Dali::Toolkit::Internal::Control& parent, Observer& observer )
213 : mTextControlParent( parent ),
214 mObserver( observer ),
215 mBoundingBox( Rect<int>() ),
216 mHighlightColor( LIGHT_BLUE ),
217 mActiveCursor( ACTIVE_CURSOR_NONE ),
218 mCursorBlinkInterval( CURSOR_BLINK_INTERVAL ),
219 mCursorBlinkDuration( 0.0f ),
220 mHandleScrolling( HANDLE_TYPE_COUNT ),
221 mScrollDirection( SCROLL_NONE ),
222 mScrollThreshold( SCROLL_THRESHOLD ),
223 mScrollSpeed( SCROLL_SPEED ),
224 mScrollDistance( SCROLL_DISTANCE ),
225 mActiveCopyPastePopup( false ),
226 mCursorBlinkStatus( true ),
227 mPrimaryCursorVisible( false ),
228 mSecondaryCursorVisible( false )
233 * Relayout of the decorations owned by the decorator.
234 * @param[in] size The Size of the UI control the decorator is adding it's decorations to.
236 void Relayout( const Vector2& size )
238 // TODO - Remove this if nothing is active
241 // Show or hide the cursors
245 const CursorImpl& cursor = mCursor[PRIMARY_CURSOR];
246 mPrimaryCursorVisible = ( cursor.position.x <= size.width ) && ( cursor.position.x >= 0.f );
247 if( mPrimaryCursorVisible )
249 Vector2 position = cursor.position;
250 if( GRAB_HANDLE == mHandleScrolling )
252 if( mScrollDirection == SCROLL_RIGHT )
258 position.x = size.width;
262 mPrimaryCursor.SetPosition( position.x,
264 mPrimaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
266 mPrimaryCursor.SetVisible( mPrimaryCursorVisible );
268 if( mSecondaryCursor )
270 const CursorImpl& cursor = mCursor[SECONDARY_CURSOR];
271 mSecondaryCursorVisible = ( cursor.position.x <= size.width ) && ( cursor.position.x >= 0.f );
272 if( mSecondaryCursorVisible )
274 mSecondaryCursor.SetPosition( cursor.position.x,
276 mSecondaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
278 mSecondaryCursor.SetVisible( mSecondaryCursorVisible );
281 // Show or hide the grab handle
282 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
283 if( grabHandle.active )
285 Vector2 position = grabHandle.position;
287 if( GRAB_HANDLE == mHandleScrolling )
289 if( mScrollDirection == SCROLL_RIGHT )
295 position.x = size.width;
299 const bool isVisible = ( position.x <= size.width ) && ( position.x >= 0.f );
307 grabHandle.actor.SetPosition( position.x,
308 position.y + grabHandle.lineHeight );
310 grabHandle.actor.SetVisible( isVisible );
312 else if( grabHandle.actor )
314 UnparentAndReset( grabHandle.actor );
317 // Show or hide the selection handles/highlight
318 HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
319 HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
320 if( primary.active || secondary.active )
324 CreateSelectionHandles();
326 primary.actor.SetPosition( primary.position.x,
327 primary.position.y + primary.lineHeight );
329 secondary.actor.SetPosition( secondary.position.x,
330 secondary.position.y + secondary.lineHeight );
337 UnparentAndReset( primary.actor );
338 UnparentAndReset( secondary.actor );
339 UnparentAndReset( mHighlightMeshActor );
342 if ( mActiveCopyPastePopup )
344 if ( !mCopyPastePopup )
346 mCopyPastePopup = TextSelectionPopup::New();
347 #ifdef DECORATOR_DEBUG
348 mCopyPastePopup.SetName("mCopyPastePopup");
350 mCopyPastePopup.SetAnchorPoint( AnchorPoint::CENTER );
351 mCopyPastePopup.OnRelayoutSignal().Connect( this, &Decorator::Impl::PopUpRelayoutComplete ); // Position popup after size negotiation
352 mActiveLayer.Add ( mCopyPastePopup );
357 if ( mCopyPastePopup )
359 UnparentAndReset( mCopyPastePopup );
364 void UpdatePositions( const Vector2& scrollOffset )
366 mCursor[PRIMARY_CURSOR].position += scrollOffset;
367 mCursor[SECONDARY_CURSOR].position += scrollOffset;
368 mHandle[ GRAB_HANDLE ].position += scrollOffset;
369 mHandle[ LEFT_SELECTION_HANDLE ].position += scrollOffset;
370 mHandle[ RIGHT_SELECTION_HANDLE ].position += scrollOffset;
372 // TODO Highlight box??
375 void PopUpRelayoutComplete( Actor actor )
377 // Size negotiation for CopyPastePopup complete so can get the size and constrain position within bounding box.
379 mCopyPastePopup.OnRelayoutSignal().Disconnect( this, &Decorator::Impl::PopUpRelayoutComplete );
381 Vector3 popupPosition( mCursor[PRIMARY_CURSOR].position.x, mCursor[PRIMARY_CURSOR].position.y -100.0f , 0.0f); //todo 100 to be an offset Property
383 Vector3 popupSize = Vector3( mCopyPastePopup.GetRelayoutSize( Dimension::WIDTH ), mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT ), 0.0f );
385 GetConstrainedPopupPosition( popupPosition, popupSize, AnchorPoint::CENTER, mActiveLayer, mBoundingBox );
387 SetUpPopUpPositionNotifications();
389 mCopyPastePopup.SetPosition( popupPosition ); //todo grabhandle(cursor) or selection handle positions to be used
392 void CreateCursor( ImageActor& cursor, const Vector4& color )
394 cursor = CreateSolidColorActor( color );
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
402 if( mActiveCursor == ACTIVE_CURSOR_NONE )
404 UnparentAndReset( mPrimaryCursor );
405 UnparentAndReset( mSecondaryCursor );
409 /* Create Primary and or Secondary Cursor(s) if active and add to parent */
410 if ( mActiveCursor == ACTIVE_CURSOR_PRIMARY ||
411 mActiveCursor == ACTIVE_CURSOR_BOTH )
413 if ( !mPrimaryCursor )
415 CreateCursor( mPrimaryCursor, mCursor[PRIMARY_CURSOR].color );
416 #ifdef DECORATOR_DEBUG
417 mPrimaryCursor.SetName( "PrimaryCursorActor" );
419 mActiveLayer.Add( mPrimaryCursor );
423 if ( mActiveCursor == ACTIVE_CURSOR_BOTH )
425 if ( !mSecondaryCursor )
427 CreateCursor( mSecondaryCursor, mCursor[SECONDARY_CURSOR].color );
428 #ifdef DECORATOR_DEBUG
429 mSecondaryCursor.SetName( "SecondaryCursorActor" );
431 mActiveLayer.Add( mSecondaryCursor );
436 UnparentAndReset( mSecondaryCursor );
441 bool OnCursorBlinkTimerTick()
444 if ( mPrimaryCursor )
446 mPrimaryCursor.SetVisible( mPrimaryCursorVisible && mCursorBlinkStatus );
448 if ( mSecondaryCursor )
450 mSecondaryCursor.SetVisible( mSecondaryCursorVisible && mCursorBlinkStatus );
453 mCursorBlinkStatus = !mCursorBlinkStatus;
458 void SetupTouchEvents()
462 mTapDetector = TapGestureDetector::New();
463 mTapDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnTap );
466 if ( !mPanGestureDetector )
468 mPanGestureDetector = PanGestureDetector::New();
469 mPanGestureDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnPan );
473 void CreateActiveLayer()
477 Actor parent = mTextControlParent.Self();
479 mActiveLayer = Layer::New();
480 #ifdef DECORATOR_DEBUG
481 mActiveLayer.SetName ( "ActiveLayerActor" );
484 mActiveLayer.SetParentOrigin( ParentOrigin::CENTER );
485 mActiveLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
486 mActiveLayer.SetPositionInheritanceMode( USE_PARENT_POSITION );
488 parent.Add( mActiveLayer );
491 mActiveLayer.RaiseToTop();
494 void CreateGrabHandle()
496 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
497 if( !grabHandle.actor )
499 if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] )
501 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_RELEASED );
503 if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] )
505 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_PRESSED );
508 grabHandle.actor = ImageActor::New( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] );
509 grabHandle.actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
510 grabHandle.actor.SetDrawMode( DrawMode::OVERLAY );
511 // Area that Grab handle responds to, larger than actual handle so easier to move
512 #ifdef DECORATOR_DEBUG
513 grabHandle.actor.SetName( "GrabHandleActor" );
514 if ( Dali::Internal::gLogFilter->IsEnabledFor( Debug::Verbose ) )
516 grabHandle.grabArea = Toolkit::CreateSolidColorActor( Vector4(0.0f, 0.0f, 0.0f, 0.0f), true, Color::RED, 1 );
517 grabHandle.grabArea.SetName( "GrabArea" );
521 grabHandle.grabArea = Actor::New();
522 grabHandle.grabArea.SetName( "GrabArea" );
525 grabHandle.grabArea = Actor::New();
528 grabHandle.grabArea.SetParentOrigin( ParentOrigin::TOP_CENTER );
529 grabHandle.grabArea.SetAnchorPoint( AnchorPoint::TOP_CENTER );
530 grabHandle.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
531 grabHandle.grabArea.SetSizeModeFactor( DEFAULT_GRAB_HANDLE_RELATIVE_SIZE );
532 grabHandle.actor.Add( grabHandle.grabArea );
534 grabHandle.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnGrabHandleTouched );
535 mTapDetector.Attach( grabHandle.grabArea );
536 mPanGestureDetector.Attach( grabHandle.grabArea );
538 mActiveLayer.Add( grabHandle.actor );
542 void CreateSelectionHandles()
544 HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
547 if( !mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
549 mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
552 primary.actor = ImageActor::New( mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
553 #ifdef DECORATOR_DEBUG
554 primary.actor.SetName("SelectionHandleOne");
556 primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
557 primary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
558 primary.flipped = false;
560 primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
561 #ifdef DECORATOR_DEBUG
562 primary.grabArea.SetName("SelectionHandleOneGrabArea");
564 primary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
565 primary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
566 primary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
568 mTapDetector.Attach( primary.grabArea );
569 mPanGestureDetector.Attach( primary.grabArea );
570 primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
572 primary.actor.Add( primary.grabArea );
573 mActiveLayer.Add( primary.actor );
576 HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
577 if( !secondary.actor )
579 if( !mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
581 mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
584 secondary.actor = ImageActor::New( mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
585 #ifdef DECORATOR_DEBUG
586 secondary.actor.SetName("SelectionHandleTwo");
588 secondary.actor.SetAnchorPoint( AnchorPoint::TOP_LEFT ); // Change to BOTTOM_LEFT if Look'n'Feel requires handle above text.
589 secondary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
590 secondary.flipped = false;
592 secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
593 #ifdef DECORATOR_DEBUG
594 secondary.grabArea.SetName("SelectionHandleTwoGrabArea");
596 secondary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
597 secondary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
598 secondary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
600 mTapDetector.Attach( secondary.grabArea );
601 mPanGestureDetector.Attach( secondary.grabArea );
602 secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
604 secondary.actor.Add( secondary.grabArea );
605 mActiveLayer.Add( secondary.actor );
609 void CreateHighlight()
611 if ( !mHighlightMeshActor )
613 mHighlightMaterial = Material::New( "HighlightMaterial" );
614 mHighlightMaterial.SetDiffuseColor( mHighlightColor );
616 mHighlightMeshData.SetMaterial( mHighlightMaterial );
617 mHighlightMeshData.SetHasNormals( true );
619 mHighlightMesh = Mesh::New( mHighlightMeshData );
621 mHighlightMeshActor = MeshActor::New( mHighlightMesh );
622 #ifdef DECORATOR_DEBUG
623 mHighlightMeshActor.SetName( "HighlightMeshActor" );
625 mHighlightMeshActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
626 mHighlightMeshActor.SetPosition( 0.0f, 0.0f, DISPLAYED_HIGHLIGHT_Z_OFFSET );
628 Actor parent = mTextControlParent.Self();
629 parent.Add( mHighlightMeshActor );
633 void UpdateHighlight()
635 // Construct a Mesh with a texture to be used as the highlight 'box' for selected text
637 // Example scenarios where mesh is made from 3, 1, 2, 2 ,3 or 3 quads.
639 // [ TOP ] [ TOP ] [TOP ] [ TOP ] [ TOP ] [ TOP ]
640 // [ MIDDLE ] [BOTTOM] [BOTTOM] [ MIDDLE ] [ MIDDLE ]
641 // [ BOTTOM] [ MIDDLE ] [ MIDDLE ]
642 // [BOTTOM] [ MIDDLE ]
645 // Each quad is created as 2 triangles.
646 // Middle is just 1 quad regardless of its size.
660 if ( mHighlightMesh && mHighlightMaterial && !mHighlightQuadList.empty() )
662 MeshData::VertexContainer vertices;
663 Dali::MeshData::FaceIndices faceIndices;
665 std::vector<QuadCoordinates>::iterator iter = mHighlightQuadList.begin();
666 std::vector<QuadCoordinates>::iterator endIter = mHighlightQuadList.end();
668 // vertex position defaults to (0 0 0)
669 MeshData::Vertex vertex;
670 // set normal for all vertices as (0 0 1) pointing outward from TextInput Actor.
673 for(std::size_t v = 0; iter != endIter; ++iter,v+=4 )
675 // Add each quad geometry (a sub-selection) to the mesh data.
685 QuadCoordinates& quad = *iter;
687 vertex.x = quad.min.x;
688 vertex.y = quad.min.y;
689 vertices.push_back( vertex );
692 vertex.x = quad.max.x;
693 vertex.y = quad.min.y;
694 vertices.push_back( vertex );
697 vertex.x = quad.min.x;
698 vertex.y = quad.max.y;
699 vertices.push_back( vertex );
701 // bottom-right (v+3)
702 vertex.x = quad.max.x;
703 vertex.y = quad.max.y;
704 vertices.push_back( vertex );
706 // triangle A (3, 1, 0)
707 faceIndices.push_back( v + 3 );
708 faceIndices.push_back( v + 1 );
709 faceIndices.push_back( v );
711 // triangle B (0, 2, 3)
712 faceIndices.push_back( v );
713 faceIndices.push_back( v + 2 );
714 faceIndices.push_back( v + 3 );
716 mHighlightMeshData.SetFaceIndices( faceIndices );
719 BoneContainer bones(0); // passed empty as bones not required
720 mHighlightMeshData.SetData( vertices, faceIndices, bones, mHighlightMaterial );
721 mHighlightMesh.UpdateMeshData( mHighlightMeshData );
725 void OnTap( Actor actor, const TapGesture& tap )
727 if( actor == mHandle[GRAB_HANDLE].actor )
733 void DoPan( HandleImpl& handle, HandleType type, const PanGesture& gesture )
735 if( Gesture::Started == gesture.state )
737 handle.grabDisplacementX = handle.grabDisplacementY = 0;
740 handle.grabDisplacementX += gesture.displacement.x;
741 handle.grabDisplacementY += gesture.displacement.y;
743 const float x = handle.position.x + handle.grabDisplacementX;
744 const float y = handle.position.y + handle.lineHeight*0.5f + handle.grabDisplacementY;
746 if( Gesture::Started == gesture.state ||
747 Gesture::Continuing == gesture.state )
749 if( x < mScrollThreshold )
751 mScrollDirection = SCROLL_RIGHT;
752 mHandleScrolling = type;
755 else if( x > mTextControlParent.Self().GetTargetSize().width - mScrollThreshold )
757 mScrollDirection = SCROLL_LEFT;
758 mHandleScrolling = type;
763 mHandleScrolling = HANDLE_TYPE_COUNT;
765 mObserver.HandleEvent( type, HANDLE_PRESSED, x, y );
768 else if( Gesture::Finished == gesture.state ||
769 Gesture::Cancelled == gesture.state )
771 if( mScrollTimer && mScrollTimer.IsRunning() )
773 mHandleScrolling = HANDLE_TYPE_COUNT;
775 mObserver.HandleEvent( type, HANDLE_STOP_SCROLLING, x, y );
779 mObserver.HandleEvent( type, HANDLE_RELEASED, x, y );
781 handle.actor.SetImage( mHandleImages[type][HANDLE_IMAGE_RELEASED] );
785 void OnPan( Actor actor, const PanGesture& gesture )
787 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
788 HandleImpl& primarySelectionHandle = mHandle[LEFT_SELECTION_HANDLE];
789 HandleImpl& secondarySelectionHandle = mHandle[RIGHT_SELECTION_HANDLE];
791 if( actor == grabHandle.grabArea )
793 DoPan( grabHandle, GRAB_HANDLE, gesture );
795 else if( actor == primarySelectionHandle.grabArea )
797 DoPan( primarySelectionHandle, LEFT_SELECTION_HANDLE, gesture );
799 else if( actor == secondarySelectionHandle.grabArea )
801 DoPan( secondarySelectionHandle, RIGHT_SELECTION_HANDLE, gesture );
805 bool OnGrabHandleTouched( Actor actor, const TouchEvent& event )
807 // Switch between pressed/release grab-handle images
808 if( event.GetPointCount() > 0 &&
809 mHandle[GRAB_HANDLE].actor )
811 const TouchPoint& point = event.GetPoint(0);
813 if( TouchPoint::Down == point.state &&
814 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] )
816 mHandle[GRAB_HANDLE].actor.SetImage( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] );
818 else if( TouchPoint::Up == point.state &&
819 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] )
821 mHandle[GRAB_HANDLE].actor.SetImage( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] );
825 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
829 bool OnHandleOneTouched( Actor actor, const TouchEvent& touch )
832 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
836 bool OnHandleTwoTouched( Actor actor, const TouchEvent& touch )
839 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
845 float AlternatePopUpPositionRelativeToCursor()
847 float alternativePosition=0.0f;;
849 if ( mPrimaryCursor ) // Secondary cursor not used for paste
851 Cursor cursor = PRIMARY_CURSOR;
852 alternativePosition = mCursor[cursor].position.y;
855 const float popupHeight = 120.0f; // todo Set as a MaxSize Property in Control or retrieve from CopyPastePopup class.
857 if( mHandle[GRAB_HANDLE].active )
859 // If grab handle enabled then position pop-up below the grab handle.
860 const Vector2 grabHandleSize( 59.0f, 56.0f ); // todo
861 const float BOTTOM_HANDLE_BOTTOM_OFFSET = 1.5; //todo Should be a property
862 alternativePosition += grabHandleSize.height + popupHeight + BOTTOM_HANDLE_BOTTOM_OFFSET ;
866 alternativePosition += popupHeight;
869 return alternativePosition;
872 void PopUpLeavesVerticalBoundary( PropertyNotification& source )
874 float alternativeYPosition=0.0f;
875 // todo use AlternatePopUpPositionRelativeToSelectionHandles() if text is highlighted
876 // if can't be positioned above, then position below row.
877 alternativeYPosition = AlternatePopUpPositionRelativeToCursor();
879 mCopyPastePopup.SetY( alternativeYPosition );
883 void SetUpPopUpPositionNotifications( )
885 // Note Property notifications ignore any set anchor point so conditions must allow for this. Default is Top Left.
887 // Exceeding vertical boundary
889 Vector4 worldCoordinatesBoundingBox;
890 LocalToWorldCoordinatesBoundingBox( mBoundingBox, worldCoordinatesBoundingBox );
892 float popupHeight = mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT);
894 PropertyNotification verticalExceedNotification = mCopyPastePopup.AddPropertyNotification( Actor::Property::WORLD_POSITION_Y,
895 OutsideCondition( worldCoordinatesBoundingBox.y + popupHeight/2,
896 worldCoordinatesBoundingBox.w - popupHeight/2 ) );
898 verticalExceedNotification.NotifySignal().Connect( this, &Decorator::Impl::PopUpLeavesVerticalBoundary );
901 void GetConstrainedPopupPosition( Vector3& requiredPopupPosition, Vector3& popupSize, Vector3 anchorPoint, Actor& parent, Rect<int>& boundingBox )
903 DALI_ASSERT_DEBUG ( "Popup parent not on stage" && parent.OnStage() )
905 // Parent must already by added to Stage for these Get calls to work
906 Vector3 parentAnchorPoint = parent.GetCurrentAnchorPoint();
907 Vector3 parentWorldPositionLeftAnchor = parent.GetCurrentWorldPosition() - parent.GetCurrentSize()*parentAnchorPoint;
908 Vector3 popupWorldPosition = parentWorldPositionLeftAnchor + requiredPopupPosition; // Parent World position plus popup local position gives World Position
909 Vector3 popupDistanceFromAnchorPoint = popupSize*anchorPoint;
911 // Bounding rectangle is supplied as screen coordinates, bounding will be done in world coordinates.
912 Vector4 boundingRectangleWorld;
913 LocalToWorldCoordinatesBoundingBox( boundingBox, boundingRectangleWorld );
915 // Calculate distance to move popup (in local space) so fits within the boundary
916 float xOffSetToKeepWithinBounds = 0.0f;
917 if( popupWorldPosition.x - popupDistanceFromAnchorPoint.x < boundingRectangleWorld.x )
919 xOffSetToKeepWithinBounds = boundingRectangleWorld.x - ( popupWorldPosition.x - popupDistanceFromAnchorPoint.x );
921 else if ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x > boundingRectangleWorld.z )
923 xOffSetToKeepWithinBounds = boundingRectangleWorld.z - ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x );
926 // Ensure initial display of Popup is in alternative position if can not fit above. As Property notification will be a frame behind.
927 if ( popupWorldPosition.y - popupDistanceFromAnchorPoint.y < boundingRectangleWorld.y )
929 requiredPopupPosition.y = AlternatePopUpPositionRelativeToCursor();
932 requiredPopupPosition.x = requiredPopupPosition.x + xOffSetToKeepWithinBounds;
935 void SetScrollThreshold( float threshold )
937 mScrollThreshold = threshold;
940 float GetScrollThreshold() const
942 return mScrollThreshold;
945 void SetScrollSpeed( float speed )
947 mScrollSpeed = speed;
948 mScrollDistance = speed * SCROLL_TICK_INTERVAL * TO_SECONDS;
951 float GetScrollSpeed() const
957 * Creates and starts a timer to scroll the text when handles are close to the edges of the text.
959 * It only starts the timer if it's already created.
961 void StartScrollTimer()
965 mScrollTimer = Timer::New( SCROLL_TICK_INTERVAL );
966 mScrollTimer.TickSignal().Connect( this, &Decorator::Impl::OnScrollTimerTick );
969 if( !mScrollTimer.IsRunning() )
971 mScrollTimer.Start();
976 * Stops the timer used to scroll the text.
978 void StopScrollTimer()
987 * Callback called by the timer used to scroll the text.
989 * It calculates and sets a new scroll position.
991 bool OnScrollTimerTick()
993 if( HANDLE_TYPE_COUNT != mHandleScrolling )
995 mObserver.HandleEvent( mHandleScrolling,
997 mScrollDirection == SCROLL_RIGHT ? mScrollDistance : -mScrollDistance,
1004 Internal::Control& mTextControlParent;
1005 Observer& mObserver;
1007 TapGestureDetector mTapDetector;
1008 PanGestureDetector mPanGestureDetector;
1009 Timer mCursorBlinkTimer; ///< Timer to signal cursor to blink
1010 Timer mScrollTimer; ///< Timer used to scroll the text when the grab handle is moved close to the edges.
1012 Layer mActiveLayer; ///< Layer for active handles and alike that ensures they are above all else.
1013 ImageActor mPrimaryCursor;
1014 ImageActor mSecondaryCursor;
1015 MeshActor mHighlightMeshActor; ///< Mesh Actor to display highlight
1016 TextSelectionPopup mCopyPastePopup;
1018 Image mHandleImages[HANDLE_TYPE_COUNT][HANDLE_IMAGE_TYPE_COUNT];
1020 Mesh mHighlightMesh; ///< Mesh for highlight
1021 MeshData mHighlightMeshData; ///< Mesh Data for highlight
1022 Material mHighlightMaterial; ///< Material used for highlight
1024 CursorImpl mCursor[CURSOR_COUNT];
1025 HandleImpl mHandle[HANDLE_TYPE_COUNT];
1026 QuadContainer mHighlightQuadList; ///< Sub-selections that combine to create the complete selection highlight
1028 Rect<int> mBoundingBox;
1029 Vector4 mHighlightColor; ///< Color of the highlight
1031 unsigned int mActiveCursor;
1032 unsigned int mCursorBlinkInterval;
1033 float mCursorBlinkDuration;
1034 HandleType mHandleScrolling; ///< The handle which is scrolling.
1035 ScrollDirection mScrollDirection; ///< The direction of the scroll.
1036 float mScrollThreshold; ///< Defines a square area inside the control, close to the edge. A cursor entering this area will trigger scroll events.
1037 float mScrollSpeed; ///< The scroll speed in pixels per second.
1038 float mScrollDistance; ///< Distance the text scrolls during a scroll interval.
1039 unsigned int mScrollInterval; ///< Time in milliseconds of a scroll interval.
1041 bool mActiveCopyPastePopup : 1;
1042 bool mCursorBlinkStatus : 1; ///< Flag to switch between blink on and blink off.
1043 bool mPrimaryCursorVisible : 1; ///< Whether the primary cursor is visible.
1044 bool mSecondaryCursorVisible : 1; ///< Whether the secondary cursor is visible.
1047 DecoratorPtr Decorator::New( Internal::Control& parent, Observer& observer )
1049 return DecoratorPtr( new Decorator(parent, observer) );
1052 void Decorator::SetBoundingBox( const Rect<int>& boundingBox )
1054 mImpl->mBoundingBox = boundingBox;
1057 const Rect<int>& Decorator::GetBoundingBox() const
1059 return mImpl->mBoundingBox;
1062 void Decorator::Relayout( const Vector2& size )
1064 mImpl->Relayout( size );
1067 void Decorator::UpdatePositions( const Vector2& scrollOffset )
1069 mImpl->UpdatePositions( scrollOffset );
1074 void Decorator::SetActiveCursor( ActiveCursor activeCursor )
1076 mImpl->mActiveCursor = activeCursor;
1079 unsigned int Decorator::GetActiveCursor() const
1081 return mImpl->mActiveCursor;
1084 void Decorator::SetPosition( Cursor cursor, float x, float y, float cursorHeight, float lineHeight )
1086 mImpl->mCursor[cursor].position.x = x;
1087 mImpl->mCursor[cursor].position.y = y;
1088 mImpl->mCursor[cursor].cursorHeight = cursorHeight;
1089 mImpl->mCursor[cursor].lineHeight = lineHeight;
1092 void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& cursorHeight, float& lineHeight ) const
1094 x = mImpl->mCursor[cursor].position.x;
1095 y = mImpl->mCursor[cursor].position.y;
1096 cursorHeight = mImpl->mCursor[cursor].cursorHeight;
1097 lineHeight = mImpl->mCursor[cursor].lineHeight;
1100 const Vector2& Decorator::GetPosition( Cursor cursor ) const
1102 return mImpl->mCursor[cursor].position;
1105 void Decorator::SetColor( Cursor cursor, const Dali::Vector4& color )
1107 mImpl->mCursor[cursor].color = color;
1110 const Dali::Vector4& Decorator::GetColor( Cursor cursor ) const
1112 return mImpl->mCursor[cursor].color;
1115 void Decorator::StartCursorBlink()
1117 if ( !mImpl->mCursorBlinkTimer )
1119 mImpl->mCursorBlinkTimer = Timer::New( mImpl->mCursorBlinkInterval );
1120 mImpl->mCursorBlinkTimer.TickSignal().Connect( mImpl, &Decorator::Impl::OnCursorBlinkTimerTick );
1123 if ( !mImpl->mCursorBlinkTimer.IsRunning() )
1125 mImpl->mCursorBlinkTimer.Start();
1129 void Decorator::StopCursorBlink()
1131 if ( mImpl->mCursorBlinkTimer )
1133 mImpl->mCursorBlinkTimer.Stop();
1137 void Decorator::SetCursorBlinkInterval( float seconds )
1139 mImpl->mCursorBlinkInterval = static_cast<unsigned int>( seconds * TO_MILLISECONDS ); // Convert to milliseconds
1142 float Decorator::GetCursorBlinkInterval() const
1144 return static_cast<float>( mImpl->mCursorBlinkInterval ) * TO_SECONDS;
1147 void Decorator::SetCursorBlinkDuration( float seconds )
1149 mImpl->mCursorBlinkDuration = seconds;
1152 float Decorator::GetCursorBlinkDuration() const
1154 return mImpl->mCursorBlinkDuration;
1159 void Decorator::SetHandleActive( HandleType handleType, bool active )
1161 mImpl->mHandle[handleType].active = active;
1164 bool Decorator::IsHandleActive( HandleType handleType ) const
1166 return mImpl->mHandle[handleType].active ;
1169 void Decorator::SetHandleImage( HandleType handleType, HandleImageType handleImageType, Dali::Image image )
1171 mImpl->mHandleImages[handleType][handleImageType] = image;
1174 Dali::Image Decorator::GetHandleImage( HandleType handleType, HandleImageType handleImageType ) const
1176 return mImpl->mHandleImages[handleType][handleImageType];
1179 void Decorator::SetPosition( HandleType handleType, float x, float y, float height )
1181 // Adjust grab handle displacement
1182 Impl::HandleImpl& handle = mImpl->mHandle[handleType];
1184 handle.grabDisplacementX -= x - handle.position.x;
1185 handle.grabDisplacementY -= y - handle.position.y;
1187 handle.position.x = x;
1188 handle.position.y = y;
1189 handle.lineHeight = height;
1192 void Decorator::GetPosition( HandleType handleType, float& x, float& y, float& height ) const
1194 Impl::HandleImpl& handle = mImpl->mHandle[handleType];
1196 x = handle.position.x;
1197 y = handle.position.y;
1198 height = handle.lineHeight;
1201 void Decorator::AddHighlight( float x1, float y1, float x2, float y2 )
1203 mImpl->mHighlightQuadList.push_back( QuadCoordinates(x1, y1, x2, y2) );
1206 void Decorator::ClearHighlights()
1208 mImpl->mHighlightQuadList.clear();
1211 void Decorator::SetHighlightColor( const Vector4& color )
1213 mImpl->mHighlightColor = color;
1216 const Vector4& Decorator::GetHighlightColor() const
1218 return mImpl->mHighlightColor;
1221 void Decorator::SetPopupActive( bool active )
1223 mImpl->mActiveCopyPastePopup = active;
1226 bool Decorator::IsPopupActive() const
1228 return mImpl->mActiveCopyPastePopup ;
1233 void Decorator::SetScrollThreshold( float threshold )
1235 mImpl->SetScrollThreshold( threshold );
1238 float Decorator::GetScrollThreshold() const
1240 return mImpl->GetScrollThreshold();
1243 void Decorator::SetScrollSpeed( float speed )
1245 mImpl->SetScrollSpeed( speed );
1248 float Decorator::GetScrollSpeed() const
1250 return mImpl->GetScrollSpeed();
1253 Decorator::~Decorator()
1258 Decorator::Decorator( Dali::Toolkit::Internal::Control& parent, Observer& observer )
1261 mImpl = new Decorator::Impl( parent, observer );
1266 } // namespace Toolkit