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 mGrabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
330 #ifdef DECORATOR_DEBUG
331 mGrabArea.SetName( "GrabArea" );
333 mGrabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
334 mGrabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
335 mGrabArea.SetSizeModeFactor( DEFAULT_GRAB_HANDLE_RELATIVE_SIZE );
336 mGrabHandle.Add(mGrabArea);
338 mTapDetector.Attach( mGrabArea );
339 mPanGestureDetector.Attach( mGrabArea );
341 mActiveLayer.Add(mGrabHandle);
345 void CreateSelectionHandles()
347 SelectionHandleImpl& primary = mSelectionHandle[ PRIMARY_SELECTION_HANDLE ];
348 if ( !primary.actor )
350 if ( !primary.releasedImage )
352 primary.releasedImage = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
355 primary.actor = ImageActor::New( primary.releasedImage );
356 #ifdef DECORATOR_DEBUG
357 primary.actor.SetName("SelectionHandleOne");
359 primary.actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
360 primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
361 primary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
362 primary.flipped = false;
364 primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
365 #ifdef DECORATOR_DEBUG
366 primary.grabArea.SetName("SelectionHandleOneGrabArea");
368 primary.grabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
369 primary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
370 primary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
372 mTapDetector.Attach( primary.grabArea );
373 mPanGestureDetector.Attach( primary.grabArea );
374 primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
376 primary.actor.Add( primary.grabArea );
377 mActiveLayer.Add( primary.actor );
380 SelectionHandleImpl& secondary = mSelectionHandle[ SECONDARY_SELECTION_HANDLE ];
381 if ( !secondary.actor )
383 if ( !secondary.releasedImage )
385 secondary.releasedImage = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
388 secondary.actor = ImageActor::New( secondary.releasedImage );
389 #ifdef DECORATOR_DEBUG
390 secondary.actor.SetName("SelectionHandleTwo");
392 secondary.actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
393 secondary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
394 secondary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
395 secondary.flipped = false;
397 secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
398 #ifdef DECORATOR_DEBUG
399 secondary.grabArea.SetName("SelectionHandleTwoGrabArea");
401 secondary.grabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
402 secondary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
403 secondary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
405 mTapDetector.Attach( secondary.grabArea );
406 mPanGestureDetector.Attach( secondary.grabArea );
407 secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
409 secondary.actor.Add( secondary.grabArea );
410 mActiveLayer.Add( secondary.actor );
413 //SetUpHandlePropertyNotifications(); TODO
416 void OnTap( Actor actor, const TapGesture& tap )
418 if( actor == mGrabHandle )
424 void OnPan( Actor actor, const PanGesture& gesture )
426 if( actor == mGrabArea )
428 if( Gesture::Started == gesture.state )
430 mGrabDisplacementX = mGrabDisplacementY = 0;
433 mGrabDisplacementX += gesture.displacement.x;
434 mGrabDisplacementY += gesture.displacement.y;
436 float x = mCursor[PRIMARY_CURSOR].x + mGrabDisplacementX;
437 float y = mCursor[PRIMARY_CURSOR].y + mCursor[PRIMARY_CURSOR].height*0.5f + mGrabDisplacementY;
439 if( Gesture::Started == gesture.state ||
440 Gesture::Continuing == gesture.state )
442 mObserver.GrabHandleEvent( GRAB_HANDLE_PRESSED, x, y );
444 else if( Gesture::Finished == gesture.state ||
445 Gesture::Cancelled == gesture.state )
447 mObserver.GrabHandleEvent( GRAB_HANDLE_RELEASED, x, y );
452 bool OnHandleOneTouched( Actor actor, const TouchEvent& touch )
458 bool OnHandleTwoTouched( Actor actor, const TouchEvent& touch )
465 Internal::Control& mTextControlParent;
468 Layer mActiveLayer; // Layer for active handles and alike that ensures they are above all else.
470 unsigned int mActiveCursor;
471 bool mActiveGrabHandle;
472 bool mActiveSelection;
473 bool mActiveCopyPastePopup;
475 CursorImpl mCursor[CURSOR_COUNT];
477 Timer mCursorBlinkTimer; // Timer to signal cursor to blink
478 unsigned int mCursorBlinkInterval;
479 float mCursorBlinkDuration;
480 bool mCursorBlinkStatus; // Flag to switch between blink on and blink off
482 ImageActor mPrimaryCursor;
483 ImageActor mSecondaryCursor;
485 ImageActor mGrabHandle;
487 float mGrabDisplacementX;
488 float mGrabDisplacementY;
490 SelectionHandleImpl mSelectionHandle[SELECTION_HANDLE_COUNT];
492 TextSelectionPopup mCopyPastePopup;
495 Image mGrabHandleImage;
497 TapGestureDetector mTapDetector;
498 PanGestureDetector mPanGestureDetector;
500 Rect<int> mBoundingBox;
503 DecoratorPtr Decorator::New( Internal::Control& parent, Observer& observer )
505 return DecoratorPtr( new Decorator(parent, observer) );
508 void Decorator::SetBoundingBox( const Rect<int>& boundingBox )
510 mImpl->mBoundingBox = boundingBox;
513 const Rect<int>& Decorator::GetBoundingBox() const
515 return mImpl->mBoundingBox;
518 void Decorator::Relayout( const Vector2& size, const Vector2& scrollPosition )
520 mImpl->Relayout( size, scrollPosition );
525 void Decorator::SetActiveCursor( ActiveCursor activeCursor )
527 mImpl->mActiveCursor = activeCursor;
530 unsigned int Decorator::GetActiveCursor() const
532 return mImpl->mActiveCursor;
535 void Decorator::SetPosition( Cursor cursor, float x, float y, float height )
537 // Adjust grab handle displacement
538 mImpl->mGrabDisplacementX -= x - mImpl->mCursor[cursor].x;
539 mImpl->mGrabDisplacementY -= y - mImpl->mCursor[cursor].y;
541 mImpl->mCursor[cursor].x = x;
542 mImpl->mCursor[cursor].y = y;
543 mImpl->mCursor[cursor].height = height;
546 void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& height ) const
548 x = mImpl->mCursor[cursor].x;
549 y = mImpl->mCursor[cursor].y;
550 height = mImpl->mCursor[cursor].height;
553 void Decorator::SetColor( Cursor cursor, const Dali::Vector4& color )
555 mImpl->mCursor[cursor].color = color;
558 const Dali::Vector4& Decorator::GetColor( Cursor cursor ) const
560 return mImpl->mCursor[cursor].color;
563 void Decorator::StartCursorBlink()
565 if ( !mImpl->mCursorBlinkTimer )
567 mImpl->mCursorBlinkTimer = Timer::New( mImpl->mCursorBlinkInterval );
568 mImpl->mCursorBlinkTimer.TickSignal().Connect( mImpl, &Decorator::Impl::OnCursorBlinkTimerTick );
571 if ( !mImpl->mCursorBlinkTimer.IsRunning() )
573 mImpl->mCursorBlinkTimer.Start();
577 void Decorator::StopCursorBlink()
579 if ( mImpl->mCursorBlinkTimer )
581 mImpl->mCursorBlinkTimer.Stop();
585 void Decorator::SetCursorBlinkInterval( float seconds )
587 mImpl->mCursorBlinkInterval = seconds*MILLISECONDS; // Convert to milliseconds
590 float Decorator::GetCursorBlinkInterval() const
592 return mImpl->mCursorBlinkInterval;
595 void Decorator::SetCursorBlinkDuration( float seconds )
597 mImpl->mCursorBlinkDuration = seconds;
600 float Decorator::GetCursorBlinkDuration() const
602 return mImpl->mCursorBlinkDuration;
607 void Decorator::SetGrabHandleActive( bool active )
609 mImpl->mActiveGrabHandle = active;
612 bool Decorator::IsGrabHandleActive() const
614 return mImpl->mActiveGrabHandle;
617 void Decorator::SetGrabHandleImage( Dali::Image image )
619 mImpl->mGrabHandleImage = image;
622 Dali::Image Decorator::GetGrabHandleImage() const
624 return mImpl->mGrabHandleImage;
629 void Decorator::SetSelectionActive( bool active )
631 mImpl->mActiveSelection = active;
634 bool Decorator::IsSelectionActive() const
636 return mImpl->mActiveSelection;
639 void Decorator::SetPosition( SelectionHandle handle, float x, float y, float height )
641 mImpl->mSelectionHandle[handle].x = x;
642 mImpl->mSelectionHandle[handle].y = y;
643 mImpl->mSelectionHandle[handle].cursorHeight = height;
646 void Decorator::GetPosition( SelectionHandle handle, float& x, float& y, float& height ) const
648 x = mImpl->mSelectionHandle[handle].x;
649 y = mImpl->mSelectionHandle[handle].y;
650 height = mImpl->mSelectionHandle[handle].cursorHeight;
653 void Decorator::SetImage( SelectionHandle handle, SelectionHandleState state, Dali::Image image )
655 if( SELECTION_HANDLE_PRESSED == state )
657 mImpl->mSelectionHandle[handle].pressedImage = image;
661 mImpl->mSelectionHandle[handle].releasedImage = image;
665 Dali::Image Decorator::GetImage( SelectionHandle handle, SelectionHandleState state ) const
667 if( SELECTION_HANDLE_PRESSED == state )
669 return mImpl->mSelectionHandle[handle].pressedImage;
672 return mImpl->mSelectionHandle[handle].releasedImage;
675 void Decorator::SetPopupActive( bool active )
677 mImpl->mActiveCopyPastePopup = active;
680 bool Decorator::IsPopupActive() const
682 return mImpl->mActiveCopyPastePopup ;
685 Decorator::~Decorator()
690 Decorator::Decorator( Dali::Toolkit::Internal::Control& parent, Observer& observer )
693 mImpl = new Decorator::Impl( parent, observer );
698 } // namespace Toolkit