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/public-api/actors/actor.h>
23 #include <dali/public-api/adaptor-framework/timer.h>
24 #include <dali/public-api/actors/image-actor.h>
25 #include <dali/public-api/actors/layer.h>
26 #include <dali/public-api/common/constants.h>
27 #include <dali/public-api/events/tap-gesture.h>
28 #include <dali/public-api/events/tap-gesture-detector.h>
29 #include <dali/public-api/events/pan-gesture.h>
30 #include <dali/public-api/events/pan-gesture-detector.h>
31 #include <dali/public-api/images/resource-image.h>
32 #include <dali/public-api/math/vector2.h>
33 #include <dali/public-api/math/vector4.h>
34 //#include <dali/public-api/images/nine-patch-image.h>
35 #include <dali/public-api/signals/connection-tracker.h>
38 #include <dali-toolkit/public-api/controls/control.h>
39 #include <dali-toolkit/public-api/controls/control-impl.h>
40 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
41 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
42 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
43 #include <dali-toolkit/public-api/controls/text-controls/text-selection-popup.h>
46 #define DECORATOR_DEBUG
53 const char* DEFAULT_GRAB_HANDLE_IMAGE( DALI_IMAGE_DIR "insertpoint-icon.png" );
54 const char* DEFAULT_SELECTION_HANDLE_ONE( DALI_IMAGE_DIR "text-input-selection-handle-left.png" );
55 const char* DEFAULT_SELECTION_HANDLE_TWO( DALI_IMAGE_DIR "text-input-selection-handle-right.png" );
56 //const char* DEFAULT_SELECTION_HANDLE_ONE_PRESSED( DALI_IMAGE_DIR "text-input-selection-handle-left-press.png" );
57 //const char* DEFAULT_SELECTION_HANDLE_TWO_PRESSED( DALI_IMAGE_DIR "text-input-selection-handle-right-press.png" );
59 const Dali::Vector3 DEFAULT_GRAB_HANDLE_RELATIVE_SIZE( 1.5f, 2.0f, 1.0f );
60 const Dali::Vector3 DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE( 1.5f, 1.5f, 1.0f );
62 const std::size_t CURSOR_BLINK_INTERVAL = 500; // Cursor blink interval
63 const std::size_t MILLISECONDS = 1000;
76 struct Decorator::Impl : public ConnectionTracker
84 color(Dali::Color::WHITE)
95 struct SelectionHandleImpl
107 float cursorHeight; ///< Not the handle height
117 Impl( Dali::Toolkit::Internal::Control& parent, Observer& observer )
118 : mTextControlParent(parent),
120 mActiveCursor(ACTIVE_CURSOR_NONE),
121 mActiveGrabHandle(false),
122 mActiveSelection( false ),
123 mActiveCopyPastePopup( false ),
124 mCursorBlinkInterval( CURSOR_BLINK_INTERVAL ),
125 mCursorBlinkDuration( 0.0f ),
126 mCursorBlinkStatus( true ),
127 mGrabDisplacementX( 0.0f ),
128 mGrabDisplacementY( 0.0f ),
129 mBoundingBox( Rect<int>() )
134 * Relayout of the decorations owned by the decorator.
135 * @param[in] size The Size of the UI control the decorater is adding it's decorations to.
137 void Relayout( const Vector2& size, const Vector2& scrollPosition )
139 // TODO - Remove this if nothing is active
142 // Show or hide the cursors
146 mPrimaryCursor.SetPosition( mCursor[PRIMARY_CURSOR].x + scrollPosition.x,
147 mCursor[PRIMARY_CURSOR].y + scrollPosition.y );
148 mPrimaryCursor.SetSize( 1.0f, mCursor[PRIMARY_CURSOR].height );
150 if( mSecondaryCursor )
152 mSecondaryCursor.SetPosition( mCursor[SECONDARY_CURSOR].x + scrollPosition.x,
153 mCursor[SECONDARY_CURSOR].y + scrollPosition.y );
154 mSecondaryCursor.SetSize( 1.0f, mCursor[SECONDARY_CURSOR].height );
157 // Show or hide the grab handle
158 if( mActiveGrabHandle )
164 mGrabHandle.SetPosition( mCursor[PRIMARY_CURSOR].x + scrollPosition.x,
165 mCursor[PRIMARY_CURSOR].y + scrollPosition.y + mCursor[PRIMARY_CURSOR].height );
167 else if( mGrabHandle )
169 UnparentAndReset( mGrabHandle );
172 // Show or hide the selection handles/highlight
173 if( mActiveSelection )
177 CreateSelectionHandles();
179 SelectionHandleImpl& primary = mSelectionHandle[ PRIMARY_SELECTION_HANDLE ];
180 primary.actor.SetPosition( primary.x + scrollPosition.x,
181 primary.y + scrollPosition.y + primary.cursorHeight );
183 SelectionHandleImpl& secondary = mSelectionHandle[ SECONDARY_SELECTION_HANDLE ];
184 secondary.actor.SetPosition( secondary.x + scrollPosition.x,
185 secondary.y + scrollPosition.y + secondary.cursorHeight );
187 //CreateHighlight(); TODO
191 UnparentAndReset( mSelectionHandle[ PRIMARY_SELECTION_HANDLE ].actor );
192 UnparentAndReset( mSelectionHandle[ SECONDARY_SELECTION_HANDLE ].actor );
195 if ( mActiveCopyPastePopup )
197 if ( !mCopyPastePopup )
199 mCopyPastePopup = TextSelectionPopup::New();
200 mActiveLayer.Add ( mCopyPastePopup );
202 mCopyPastePopup.SetPosition( Vector3( 200.0f, -100.0f, 0.0f ) ); //todo grabhandle or selection handle positions to be used
206 if ( mCopyPastePopup )
208 UnparentAndReset( mCopyPastePopup );
213 void CreateCursor( ImageActor& cursor )
215 cursor = CreateSolidColorActor( Color::WHITE );
216 cursor.SetParentOrigin( ParentOrigin::TOP_LEFT );
217 cursor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
220 // Add or Remove cursor(s) from parent
223 if( mActiveCursor == ACTIVE_CURSOR_NONE )
225 UnparentAndReset( mPrimaryCursor );
226 UnparentAndReset( mSecondaryCursor );
230 /* Create Primary and or Secondary Cursor(s) if active and add to parent */
231 if ( mActiveCursor == ACTIVE_CURSOR_PRIMARY ||
232 mActiveCursor == ACTIVE_CURSOR_BOTH )
234 if ( !mPrimaryCursor )
236 CreateCursor( mPrimaryCursor );
237 #ifdef DECORATOR_DEBUG
238 mPrimaryCursor.SetName( "PrimaryCursorActor" );
240 mActiveLayer.Add( mPrimaryCursor);
244 if ( mActiveCursor == ACTIVE_CURSOR_BOTH )
246 if ( !mSecondaryCursor )
248 CreateCursor( mSecondaryCursor );
249 #ifdef DECORATOR_DEBUG
250 mSecondaryCursor.SetName( "SecondaryCursorActor" );
252 mActiveLayer.Add( mSecondaryCursor);
258 bool OnCursorBlinkTimerTick()
261 if ( mPrimaryCursor )
263 mPrimaryCursor.SetVisible( mCursorBlinkStatus );
265 if ( mSecondaryCursor )
267 mSecondaryCursor.SetVisible( mCursorBlinkStatus );
270 mCursorBlinkStatus = !mCursorBlinkStatus;
275 void SetupTouchEvents()
279 mTapDetector = TapGestureDetector::New();
280 mTapDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnTap );
283 if ( !mPanGestureDetector )
285 mPanGestureDetector = PanGestureDetector::New();
286 mPanGestureDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnPan );
290 void CreateActiveLayer()
294 Actor parent = mTextControlParent.Self();
296 mActiveLayer = Layer::New();
297 #ifdef DECORATOR_DEBUG
298 mActiveLayer.SetName ( "ActiveLayerActor" );
301 mActiveLayer.SetAnchorPoint( AnchorPoint::CENTER);
302 mActiveLayer.SetParentOrigin( ParentOrigin::CENTER);
303 mActiveLayer.SetSizeMode( SIZE_EQUAL_TO_PARENT );
304 mActiveLayer.SetPositionInheritanceMode( USE_PARENT_POSITION );
306 parent.Add( mActiveLayer );
309 mActiveLayer.RaiseToTop();
312 void CreateGrabHandle()
316 if ( !mGrabHandleImage )
318 mGrabHandleImage = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE );
321 mGrabHandle = ImageActor::New( mGrabHandleImage );
322 #ifdef DECORATOR_DEBUG
323 mGrabHandle.SetName( "GrabHandleActor" );
325 mGrabHandle.SetParentOrigin( ParentOrigin::TOP_LEFT );
326 mGrabHandle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
327 mGrabHandle.SetDrawMode( DrawMode::OVERLAY );
329 // Area that Grab handle responds to, larger than actual handle so easier to move
330 #ifdef DECORATOR_DEBUG
331 mGrabArea = Toolkit::CreateSolidColorActor( Vector4(1.0f, 0.0f, 0.0f, 0.5f) );
332 mGrabArea.SetName( "GrabArea" );
334 mGrabArea = Actor::New();
336 mGrabArea.SetParentOrigin( ParentOrigin::TOP_CENTER );
337 mGrabArea.SetAnchorPoint( AnchorPoint::TOP_CENTER );
338 mGrabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
339 mGrabArea.SetSizeModeFactor( DEFAULT_GRAB_HANDLE_RELATIVE_SIZE );
340 mGrabHandle.Add(mGrabArea);
342 mTapDetector.Attach( mGrabArea );
343 mPanGestureDetector.Attach( mGrabArea );
345 mActiveLayer.Add(mGrabHandle);
349 void CreateSelectionHandles()
351 SelectionHandleImpl& primary = mSelectionHandle[ PRIMARY_SELECTION_HANDLE ];
352 if ( !primary.actor )
354 if ( !primary.releasedImage )
356 primary.releasedImage = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
359 primary.actor = ImageActor::New( primary.releasedImage );
360 #ifdef DECORATOR_DEBUG
361 primary.actor.SetName("SelectionHandleOne");
363 primary.actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
364 primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
365 primary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
366 primary.flipped = false;
368 primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
369 #ifdef DECORATOR_DEBUG
370 primary.grabArea.SetName("SelectionHandleOneGrabArea");
372 primary.grabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
373 primary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
374 primary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
376 mTapDetector.Attach( primary.grabArea );
377 mPanGestureDetector.Attach( primary.grabArea );
378 primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
380 primary.actor.Add( primary.grabArea );
381 mActiveLayer.Add( primary.actor );
384 SelectionHandleImpl& secondary = mSelectionHandle[ SECONDARY_SELECTION_HANDLE ];
385 if ( !secondary.actor )
387 if ( !secondary.releasedImage )
389 secondary.releasedImage = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
392 secondary.actor = ImageActor::New( secondary.releasedImage );
393 #ifdef DECORATOR_DEBUG
394 secondary.actor.SetName("SelectionHandleTwo");
396 secondary.actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
397 secondary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
398 secondary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
399 secondary.flipped = false;
401 secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
402 #ifdef DECORATOR_DEBUG
403 secondary.grabArea.SetName("SelectionHandleTwoGrabArea");
405 secondary.grabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
406 secondary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
407 secondary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
409 mTapDetector.Attach( secondary.grabArea );
410 mPanGestureDetector.Attach( secondary.grabArea );
411 secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
413 secondary.actor.Add( secondary.grabArea );
414 mActiveLayer.Add( secondary.actor );
417 //SetUpHandlePropertyNotifications(); TODO
420 void OnTap( Actor actor, const TapGesture& tap )
422 if( actor == mGrabHandle )
428 void OnPan( Actor actor, const PanGesture& gesture )
430 if( actor == mGrabArea )
432 if( Gesture::Started == gesture.state )
434 mGrabDisplacementX = mGrabDisplacementY = 0;
437 mGrabDisplacementX += gesture.displacement.x;
438 mGrabDisplacementY += gesture.displacement.y;
440 float x = mCursor[PRIMARY_CURSOR].x + mGrabDisplacementX;
441 float y = mCursor[PRIMARY_CURSOR].y + mCursor[PRIMARY_CURSOR].height*0.5f + mGrabDisplacementY;
443 if( Gesture::Started == gesture.state ||
444 Gesture::Continuing == gesture.state )
446 mObserver.GrabHandleEvent( GRAB_HANDLE_PRESSED, x, y );
448 else if( Gesture::Finished == gesture.state ||
449 Gesture::Cancelled == gesture.state )
451 mObserver.GrabHandleEvent( GRAB_HANDLE_RELEASED, x, y );
456 bool OnHandleOneTouched( Actor actor, const TouchEvent& touch )
462 bool OnHandleTwoTouched( Actor actor, const TouchEvent& touch )
469 Internal::Control& mTextControlParent;
472 Layer mActiveLayer; // Layer for active handles and alike that ensures they are above all else.
474 unsigned int mActiveCursor;
475 bool mActiveGrabHandle;
476 bool mActiveSelection;
477 bool mActiveCopyPastePopup;
479 CursorImpl mCursor[CURSOR_COUNT];
481 Timer mCursorBlinkTimer; // Timer to signal cursor to blink
482 unsigned int mCursorBlinkInterval;
483 float mCursorBlinkDuration;
484 bool mCursorBlinkStatus; // Flag to switch between blink on and blink off
486 ImageActor mPrimaryCursor;
487 ImageActor mSecondaryCursor;
489 ImageActor mGrabHandle;
491 float mGrabDisplacementX;
492 float mGrabDisplacementY;
494 SelectionHandleImpl mSelectionHandle[SELECTION_HANDLE_COUNT];
496 TextSelectionPopup mCopyPastePopup;
499 Image mGrabHandleImage;
501 TapGestureDetector mTapDetector;
502 PanGestureDetector mPanGestureDetector;
504 Rect<int> mBoundingBox;
507 DecoratorPtr Decorator::New( Internal::Control& parent, Observer& observer )
509 return DecoratorPtr( new Decorator(parent, observer) );
512 void Decorator::SetBoundingBox( const Rect<int>& boundingBox )
514 mImpl->mBoundingBox = boundingBox;
517 const Rect<int>& Decorator::GetBoundingBox() const
519 return mImpl->mBoundingBox;
522 void Decorator::Relayout( const Vector2& size, const Vector2& scrollPosition )
524 mImpl->Relayout( size, scrollPosition );
529 void Decorator::SetActiveCursor( ActiveCursor activeCursor )
531 mImpl->mActiveCursor = activeCursor;
534 unsigned int Decorator::GetActiveCursor() const
536 return mImpl->mActiveCursor;
539 void Decorator::SetPosition( Cursor cursor, float x, float y, float height )
541 // Adjust grab handle displacement
542 mImpl->mGrabDisplacementX -= x - mImpl->mCursor[cursor].x;
543 mImpl->mGrabDisplacementY -= y - mImpl->mCursor[cursor].y;
545 mImpl->mCursor[cursor].x = x;
546 mImpl->mCursor[cursor].y = y;
547 mImpl->mCursor[cursor].height = height;
550 void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& height ) const
552 x = mImpl->mCursor[cursor].x;
553 y = mImpl->mCursor[cursor].y;
554 height = mImpl->mCursor[cursor].height;
557 void Decorator::SetColor( Cursor cursor, const Dali::Vector4& color )
559 mImpl->mCursor[cursor].color = color;
562 const Dali::Vector4& Decorator::GetColor( Cursor cursor ) const
564 return mImpl->mCursor[cursor].color;
567 void Decorator::StartCursorBlink()
569 if ( !mImpl->mCursorBlinkTimer )
571 mImpl->mCursorBlinkTimer = Timer::New( mImpl->mCursorBlinkInterval );
572 mImpl->mCursorBlinkTimer.TickSignal().Connect( mImpl, &Decorator::Impl::OnCursorBlinkTimerTick );
575 if ( !mImpl->mCursorBlinkTimer.IsRunning() )
577 mImpl->mCursorBlinkTimer.Start();
581 void Decorator::StopCursorBlink()
583 if ( mImpl->mCursorBlinkTimer )
585 mImpl->mCursorBlinkTimer.Stop();
589 void Decorator::SetCursorBlinkInterval( float seconds )
591 mImpl->mCursorBlinkInterval = seconds*MILLISECONDS; // Convert to milliseconds
594 float Decorator::GetCursorBlinkInterval() const
596 return mImpl->mCursorBlinkInterval;
599 void Decorator::SetCursorBlinkDuration( float seconds )
601 mImpl->mCursorBlinkDuration = seconds;
604 float Decorator::GetCursorBlinkDuration() const
606 return mImpl->mCursorBlinkDuration;
611 void Decorator::SetGrabHandleActive( bool active )
613 mImpl->mActiveGrabHandle = active;
616 bool Decorator::IsGrabHandleActive() const
618 return mImpl->mActiveGrabHandle;
621 void Decorator::SetGrabHandleImage( Dali::Image image )
623 mImpl->mGrabHandleImage = image;
626 Dali::Image Decorator::GetGrabHandleImage() const
628 return mImpl->mGrabHandleImage;
633 void Decorator::SetSelectionActive( bool active )
635 mImpl->mActiveSelection = active;
638 bool Decorator::IsSelectionActive() const
640 return mImpl->mActiveSelection;
643 void Decorator::SetPosition( SelectionHandle handle, float x, float y, float height )
645 mImpl->mSelectionHandle[handle].x = x;
646 mImpl->mSelectionHandle[handle].y = y;
647 mImpl->mSelectionHandle[handle].cursorHeight = height;
650 void Decorator::GetPosition( SelectionHandle handle, float& x, float& y, float& height ) const
652 x = mImpl->mSelectionHandle[handle].x;
653 y = mImpl->mSelectionHandle[handle].y;
654 height = mImpl->mSelectionHandle[handle].cursorHeight;
657 void Decorator::SetImage( SelectionHandle handle, SelectionHandleState state, Dali::Image image )
659 if( SELECTION_HANDLE_PRESSED == state )
661 mImpl->mSelectionHandle[handle].pressedImage = image;
665 mImpl->mSelectionHandle[handle].releasedImage = image;
669 Dali::Image Decorator::GetImage( SelectionHandle handle, SelectionHandleState state ) const
671 if( SELECTION_HANDLE_PRESSED == state )
673 return mImpl->mSelectionHandle[handle].pressedImage;
676 return mImpl->mSelectionHandle[handle].releasedImage;
679 void Decorator::SetPopupActive( bool active )
681 mImpl->mActiveCopyPastePopup = active;
684 bool Decorator::IsPopupActive() const
686 return mImpl->mActiveCopyPastePopup ;
689 Decorator::~Decorator()
694 Decorator::Decorator( Dali::Toolkit::Internal::Control& parent, Observer& observer )
697 mImpl = new Decorator::Impl( parent, observer );
702 } // namespace Toolkit