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>
29 #include <dali-toolkit/internal/controls/text-input/textview-character-positions-impl.h>
30 #include <dali-toolkit/internal/controls/text-input/text-input-impl.h>
31 #include <dali-toolkit/internal/controls/text-view/text-processor.h>
32 #include <dali-toolkit/internal/controls/text-view/text-view-impl.h>
33 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
34 #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.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), RelativeToConstraint( relativeScale ) ) ); // grab area to be larger than text actor
66 handleGrabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
68 return handleGrabArea;
71 ImageActor CreateHandle( const Vector3& anchorPoint, const Image& handleImage, const std::string& name )
73 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateSelectionHandle\n" );
75 ImageActor selectionHandle = ImageActor::New( handleImage );
76 selectionHandle.SetName( name );
77 selectionHandle.SetAnchorPoint( anchorPoint );
78 selectionHandle.SetDrawMode( DrawMode::OVERLAY ); // ensure handle above text
80 return selectionHandle;
93 // Default constructor
94 TextInputHandles::TextInputHandles():
95 mSelectionHandleOne(),
96 mSelectionHandleTwo(),
97 mSelectionHandleOneOffset( Vector3::ZERO ),
98 mSelectionHandleTwoOffset( Vector3::ZERO ),
99 mSelectionHandleOneCoordinatePosition( Vector3::ZERO ),
100 mSelectionHandleTwoCoordinatePosition( Vector3::ZERO ),
101 mSelectionHandleOneStringPosition( 0 ),
102 mSelectionHandleTwoStringPosition( 0 ),
103 mIsSelectionHandleOneFlipped( false ),
104 mIsSelectionHandleTwoFlipped( false )
108 TextInputHandles::~TextInputHandles()
112 void TextInputHandles::CreateSelectionHandles()
114 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateSelectionHandles\n" );
116 mSelectionHandleOneImage = Image::New( DEFAULT_SELECTION_HANDLE_ONE );
117 mSelectionHandleOneImagePressed = Image::New( DEFAULT_SELECTION_HANDLE_ONE_PRESSED );
118 mSelectionHandleOne = CreateHandle( AnchorPoint::TOP_RIGHT, mSelectionHandleOneImage, "SelectionHandleOne" );
119 mIsSelectionHandleOneFlipped = false;
121 mHandleOneGrabArea = CreateGrabArea( SELECTION_GRAB_AREA_ONE, DEFAULT_SELECTION_HANDLE_RELATIVE_SCALE );
122 mSelectionHandleOne.Add( mHandleOneGrabArea );
123 mHandleOneGrabArea.TouchedSignal().Connect(this, &TextInputHandles::OnSelectionHandleTouched);
125 // mTapDetector.Attach( mHandleOneGrabArea );
127 mSelectionHandleTwoImage = Image::New( DEFAULT_SELECTION_HANDLE_TWO );
128 mSelectionHandleTwoImagePressed = Image::New( DEFAULT_SELECTION_HANDLE_TWO_PRESSED );
129 mSelectionHandleTwo = CreateHandle( AnchorPoint::TOP_LEFT, mSelectionHandleTwoImage, "SelectionHandleTwo" );
130 mIsSelectionHandleTwoFlipped = false;
132 mHandleTwoGrabArea = CreateGrabArea( SELECTION_GRAB_AREA_TWO, DEFAULT_SELECTION_HANDLE_RELATIVE_SCALE );
133 mSelectionHandleTwo.Add( mHandleTwoGrabArea );
134 mHandleTwoGrabArea.TouchedSignal().Connect(this, &TextInputHandles::OnSelectionHandleTouched);
136 // mTapDetector.Attach( mHandleTwoGrabArea );
139 void TextInputHandles::DestorySelectionHandles()
141 if ( mSelectionHandleOne && mSelectionHandleTwo)
143 mSelectionHandleOne.Unparent();
144 mSelectionHandleTwo.Unparent();
145 mSelectionHandleOneImagePressed.Reset();
146 mSelectionHandleOneImage.Reset();
147 mSelectionHandleTwoImagePressed.Reset();
148 mSelectionHandleTwoImage.Reset();
149 mSelectionHandleOne.Reset();
150 mSelectionHandleTwo.Reset();
154 void TextInputHandles::SetSelectionHandleOneVisibility( bool visibility )
156 if ( mSelectionHandleOne )
158 mSelectionHandleOne.SetVisible( visibility );
162 void TextInputHandles::SetSelectionHandleTwoVisibility( bool visibility )
164 if ( mSelectionHandleTwo )
166 mSelectionHandleTwo.SetVisible( visibility );
170 void TextInputHandles::AttachSelectionHandlesToGivenPanGesture( PanGestureDetector& panGestureDetector )
172 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: AttachSelectionHandlesToGivenPanGesture\n" );
174 panGestureDetector.Attach( mHandleOneGrabArea );
175 panGestureDetector.Attach( mHandleTwoGrabArea );
178 void TextInputHandles::AttachSelectionHandlesToGivenTapDetector(TapGestureDetector& tapGestureDetector )
180 tapGestureDetector.Attach( mHandleOneGrabArea );
181 tapGestureDetector.Attach( mHandleTwoGrabArea );
184 void TextInputHandles::AttachGrabHandleToGivenPanGesture( PanGestureDetector& panGestureDetector )
186 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: AttachGrabHandleToGivenPanGesture\n" );
188 panGestureDetector.Attach( mGrabHandleGrabArea );
191 Actor TextInputHandles::GetSelectionHandleOne()
193 return mSelectionHandleOne;
196 Actor TextInputHandles::GetSelectionHandleTwo()
198 return mSelectionHandleTwo;
201 bool TextInputHandles::OnSelectionHandleTouched(Dali::Actor actor, const TouchEvent& touch)
203 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: OnSelectionHandleTouched\n" );
208 ImageActor handleTouched = ImageActor::DownCast( actor.GetParent() ); // Hit actor would be the GrabArea hence get parent.
210 if ( handleTouched == mSelectionHandleOne )
212 pressedImage = mSelectionHandleOneImagePressed;
213 normalImage = mSelectionHandleOneImage;
217 pressedImage = mSelectionHandleTwoImagePressed;
218 normalImage = mSelectionHandleTwoImage;
221 if (touch.GetPoint(0).state == TouchPoint::Down)
223 handleTouched.SetImage( pressedImage );
225 else if (touch.GetPoint(0).state == TouchPoint::Up )
227 handleTouched.SetImage( normalImage );
234 Actor TextInputHandles::GetGrabHandle()
236 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: GetGrabHandle\n" );
241 void TextInputHandles::CreateGrabHandle()
243 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateGrabHandle\n" );
247 if ( !mGrabHandleImage )
249 mGrabHandleImage = Image::New( DEFAULT_GRAB_HANDLE );
252 mGrabHandle = CreateHandle( AnchorPoint::TOP_CENTER, mGrabHandleImage, "GrabHandle" );
253 mGrabHandleGrabArea = CreateGrabArea( GRABHANDLE_GRAB_AREA, DEFAULT_GRAB_HANDLE_RELATIVE_SCALE );
254 mGrabHandle.Add( mGrabHandleGrabArea );
258 void TextInputHandles::DestoryGrabHandle()
262 mGrabHandle.Unparent();
263 mGrabHandleImage.Reset();
268 void TextInputHandles::SetGrabHandleImage( Dali::Image image )
272 mGrabHandleImage = image;
273 mGrabHandle.SetImage( mGrabHandleImage );
277 void TextInputHandles::SetGrabHandleVisibility( bool visibility )
279 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: SetGrabHandleVisibility (%s) \n", ( visibility )?"true":"false" );
283 mGrabHandle.SetVisible( visibility );
289 } // namespace Toolkit