2 * Copyright (c) 2014 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/controls/text-input/text-input-handles-impl.h>
25 #include <dali/public-api/animation/constraints.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/public-api/images/resource-image.h>
30 #include <dali-toolkit/internal/controls/text-input/textview-character-positions-impl.h>
31 #include <dali-toolkit/internal/controls/text-input/text-input-impl.h>
32 #include <dali-toolkit/internal/controls/text-view/text-processor.h>
33 #include <dali-toolkit/internal/controls/text-view/text-view-impl.h>
34 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
35 #include <dali-toolkit/public-api/controls/alignment/alignment.h>
41 const char* const DEFAULT_SELECTION_HANDLE_ONE( DALI_IMAGE_DIR "text-input-selection-handle-left.png" );
42 const char* const DEFAULT_SELECTION_HANDLE_TWO( DALI_IMAGE_DIR "text-input-selection-handle-right.png" );
43 const char* const DEFAULT_SELECTION_HANDLE_ONE_PRESSED( DALI_IMAGE_DIR "text-input-selection-handle-left-press.png" );
44 const char* const DEFAULT_SELECTION_HANDLE_TWO_PRESSED( DALI_IMAGE_DIR "text-input-selection-handle-right-press.png" );
46 const char* const DEFAULT_GRAB_HANDLE( DALI_IMAGE_DIR "insertpoint-icon.png" );
48 const Vector3 DEFAULT_SELECTION_HANDLE_RELATIVE_SCALE( 1.5f, 1.5f, 1.0f );
49 const Vector3 DEFAULT_GRAB_HANDLE_RELATIVE_SCALE( 1.5f, 2.0f, 1.0f );
51 const char* const SELECTION_GRAB_AREA_ONE( "SelectionHandleOneGrabArea");
52 const char* const SELECTION_GRAB_AREA_TWO( "SelectionHandleTwoGrabArea");
53 const char* const GRABHANDLE_GRAB_AREA( "GrabHandleGrabArea");
55 #if defined(DEBUG_ENABLED)
56 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "TEXT_INPUT_HANDLES" );
59 Actor CreateGrabArea( const std::string& name, const Vector3& relativeScale )
61 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateGrabArea\n" );
63 Actor handleGrabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
64 handleGrabArea.SetName( name );
65 handleGrabArea.SetResizePolicy( SIZE_RELATIVE_TO_PARENT, ALL_DIMENSIONS );
66 handleGrabArea.SetSizeModeFactor( relativeScale );
67 handleGrabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
69 return handleGrabArea;
72 ImageActor CreateHandle( const Vector3& anchorPoint, const Image& handleImage, const std::string& name )
74 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateSelectionHandle\n" );
76 ImageActor selectionHandle = ImageActor::New( handleImage );
77 selectionHandle.SetName( name );
78 selectionHandle.SetAnchorPoint( anchorPoint );
79 selectionHandle.SetDrawMode( DrawMode::OVERLAY ); // ensure handle above text
81 return selectionHandle;
94 // Default constructor
95 TextInputHandles::TextInputHandles():
96 mSelectionHandleOne(),
97 mSelectionHandleTwo(),
98 mSelectionHandleOneOffset( Vector3::ZERO ),
99 mSelectionHandleTwoOffset( Vector3::ZERO ),
100 mSelectionHandleOneCoordinatePosition( Vector3::ZERO ),
101 mSelectionHandleTwoCoordinatePosition( Vector3::ZERO ),
102 mSelectionHandleOneStringPosition( 0 ),
103 mSelectionHandleTwoStringPosition( 0 ),
104 mIsSelectionHandleOneFlipped( false ),
105 mIsSelectionHandleTwoFlipped( false )
109 TextInputHandles::~TextInputHandles()
113 void TextInputHandles::CreateSelectionHandles()
115 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateSelectionHandles\n" );
117 mSelectionHandleOneImage = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
118 mSelectionHandleOneImagePressed = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE_PRESSED );
119 mSelectionHandleOne = CreateHandle( AnchorPoint::TOP_RIGHT, mSelectionHandleOneImage, "SelectionHandleOne" );
120 mIsSelectionHandleOneFlipped = false;
122 mHandleOneGrabArea = CreateGrabArea( SELECTION_GRAB_AREA_ONE, DEFAULT_SELECTION_HANDLE_RELATIVE_SCALE );
123 mSelectionHandleOne.Add( mHandleOneGrabArea );
124 mHandleOneGrabArea.TouchedSignal().Connect(this, &TextInputHandles::OnSelectionHandleTouched);
126 // mTapDetector.Attach( mHandleOneGrabArea );
128 mSelectionHandleTwoImage = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
129 mSelectionHandleTwoImagePressed = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO_PRESSED );
130 mSelectionHandleTwo = CreateHandle( AnchorPoint::TOP_LEFT, mSelectionHandleTwoImage, "SelectionHandleTwo" );
131 mIsSelectionHandleTwoFlipped = false;
133 mHandleTwoGrabArea = CreateGrabArea( SELECTION_GRAB_AREA_TWO, DEFAULT_SELECTION_HANDLE_RELATIVE_SCALE );
134 mSelectionHandleTwo.Add( mHandleTwoGrabArea );
135 mHandleTwoGrabArea.TouchedSignal().Connect(this, &TextInputHandles::OnSelectionHandleTouched);
137 // mTapDetector.Attach( mHandleTwoGrabArea );
140 void TextInputHandles::DestorySelectionHandles()
142 if ( mSelectionHandleOne && mSelectionHandleTwo)
144 mSelectionHandleOne.Unparent();
145 mSelectionHandleTwo.Unparent();
146 mSelectionHandleOneImagePressed.Reset();
147 mSelectionHandleOneImage.Reset();
148 mSelectionHandleTwoImagePressed.Reset();
149 mSelectionHandleTwoImage.Reset();
150 mSelectionHandleOne.Reset();
151 mSelectionHandleTwo.Reset();
155 void TextInputHandles::SetSelectionHandleOneVisibility( bool visibility )
157 if ( mSelectionHandleOne )
159 mSelectionHandleOne.SetVisible( visibility );
163 void TextInputHandles::SetSelectionHandleTwoVisibility( bool visibility )
165 if ( mSelectionHandleTwo )
167 mSelectionHandleTwo.SetVisible( visibility );
171 void TextInputHandles::AttachSelectionHandlesToGivenPanGesture( PanGestureDetector& panGestureDetector )
173 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: AttachSelectionHandlesToGivenPanGesture\n" );
175 panGestureDetector.Attach( mHandleOneGrabArea );
176 panGestureDetector.Attach( mHandleTwoGrabArea );
179 void TextInputHandles::AttachSelectionHandlesToGivenTapDetector(TapGestureDetector& tapGestureDetector )
181 tapGestureDetector.Attach( mHandleOneGrabArea );
182 tapGestureDetector.Attach( mHandleTwoGrabArea );
185 void TextInputHandles::AttachGrabHandleToGivenPanGesture( PanGestureDetector& panGestureDetector )
187 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: AttachGrabHandleToGivenPanGesture\n" );
189 panGestureDetector.Attach( mGrabHandleGrabArea );
192 Actor TextInputHandles::GetSelectionHandleOne()
194 return mSelectionHandleOne;
197 Actor TextInputHandles::GetSelectionHandleTwo()
199 return mSelectionHandleTwo;
202 bool TextInputHandles::OnSelectionHandleTouched(Dali::Actor actor, const TouchEvent& touch)
204 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: OnSelectionHandleTouched\n" );
209 ImageActor handleTouched = ImageActor::DownCast( actor.GetParent() ); // Hit actor would be the GrabArea hence get parent.
211 if ( handleTouched == mSelectionHandleOne )
213 pressedImage = mSelectionHandleOneImagePressed;
214 normalImage = mSelectionHandleOneImage;
218 pressedImage = mSelectionHandleTwoImagePressed;
219 normalImage = mSelectionHandleTwoImage;
222 if (touch.GetPoint(0).state == TouchPoint::Down)
224 handleTouched.SetImage( pressedImage );
226 else if (touch.GetPoint(0).state == TouchPoint::Up )
228 handleTouched.SetImage( normalImage );
235 Actor TextInputHandles::GetGrabHandle()
237 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: GetGrabHandle\n" );
242 void TextInputHandles::CreateGrabHandle()
244 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateGrabHandle\n" );
248 if ( !mGrabHandleImage )
250 mGrabHandleImage = ResourceImage::New( DEFAULT_GRAB_HANDLE );
253 mGrabHandle = CreateHandle( AnchorPoint::TOP_CENTER, mGrabHandleImage, "GrabHandle" );
254 mGrabHandleGrabArea = CreateGrabArea( GRABHANDLE_GRAB_AREA, DEFAULT_GRAB_HANDLE_RELATIVE_SCALE );
255 mGrabHandle.Add( mGrabHandleGrabArea );
259 void TextInputHandles::DestoryGrabHandle()
263 mGrabHandle.Unparent();
264 mGrabHandleImage.Reset();
269 void TextInputHandles::SetGrabHandleImage( Dali::Image image )
273 mGrabHandleImage = image;
274 mGrabHandle.SetImage( mGrabHandleImage );
278 void TextInputHandles::SetGrabHandleVisibility( bool visibility )
280 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: SetGrabHandleVisibility (%s) \n", ( visibility )?"true":"false" );
284 mGrabHandle.SetVisible( visibility );
290 } // namespace Toolkit