2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include <dali-toolkit/internal/controls/text-input/text-input-handles-impl.h>
19 #include <dali-toolkit/internal/controls/text-input/textview-character-positions-impl.h>
21 #include <dali/dali.h>
23 #include <dali-toolkit/internal/controls/text-input/text-input-impl.h>
24 #include <dali-toolkit/internal/controls/text-view/text-processor.h>
25 #include <dali-toolkit/internal/controls/text-view/text-view-impl.h>
26 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
27 #include <dali-toolkit/public-api/controls/alignment/alignment.h>
29 #include <dali/integration-api/debug.h>
40 const char* const DEFAULT_SELECTION_HANDLE_ONE( DALI_IMAGE_DIR "text-input-selection-handle-left.png" );
41 const char* const DEFAULT_SELECTION_HANDLE_TWO( DALI_IMAGE_DIR "text-input-selection-handle-right.png" );
42 const char* const DEFAULT_SELECTION_HANDLE_ONE_PRESSED( DALI_IMAGE_DIR "text-input-selection-handle-left-press.png" );
43 const char* const DEFAULT_SELECTION_HANDLE_TWO_PRESSED( DALI_IMAGE_DIR "text-input-selection-handle-right-press.png" );
45 const char* const DEFAULT_GRAB_HANDLE( DALI_IMAGE_DIR "insertpoint-icon.png" );
47 const Vector3 DEFAULT_SELECTION_HANDLE_RELATIVE_SCALE( 1.5f, 1.5f, 1.0f );
48 const Vector3 DEFAULT_GRAB_HANDLE_RELATIVE_SCALE( 1.5f, 2.0f, 1.0f );
50 const char* const SELECTION_GRAB_AREA_ONE( "SelectionHandleOneGrabArea");
51 const char* const SELECTION_GRAB_AREA_TWO( "SelectionHandleTwoGrabArea");
52 const char* const GRABHANDLE_GRAB_AREA( "GrabHandleGrabArea");
54 #if defined(DEBUG_ENABLED)
55 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "TEXT_INPUT_HANDLES" );
58 Actor CreateGrabArea( const std::string& name, const Vector3& relativeScale )
60 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateGrabArea\n" );
62 Actor handleGrabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
63 handleGrabArea.SetName( name );
64 handleGrabArea.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), RelativeToConstraint( relativeScale ) ) ); // grab area to be larger than text actor
65 handleGrabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
67 return handleGrabArea;
70 Actor CreateHandle( const Vector3& anchorPoint, const Image& handleImage, const std::string& name )
72 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateSelectionHandle\n" );
74 ImageActor selectionHandle = ImageActor::New( handleImage );
75 selectionHandle.SetName( name );
76 selectionHandle.SetAnchorPoint( anchorPoint );
77 selectionHandle.SetDrawMode( DrawMode::OVERLAY ); // ensure handle above text
79 return selectionHandle;
92 // Default constructor
93 TextInputHandles::TextInputHandles():
94 mSelectionHandleOne(),
95 mSelectionHandleTwo(),
96 mSelectionHandleOneOffset( Vector3::ZERO ),
97 mSelectionHandleTwoOffset( Vector3::ZERO ),
98 mSelectionHandleOneCoordinatePosition( Vector3::ZERO ),
99 mSelectionHandleTwoCoordinatePosition( Vector3::ZERO ),
100 mSelectionHandleOneStringPosition( 0 ),
101 mSelectionHandleTwoStringPosition( 0 ),
102 mIsSelectionHandleOneFlipped( false ),
103 mIsSelectionHandleTwoFlipped( false )
107 TextInputHandles::~TextInputHandles()
111 void TextInputHandles::CreateSelectionHandles()
113 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateSelectionHandles\n" );
115 mSelectionHandleOneImage = Image::New( DEFAULT_SELECTION_HANDLE_ONE );
116 mSelectionHandleOneImagePressed = Image::New( DEFAULT_SELECTION_HANDLE_ONE_PRESSED );
117 mSelectionHandleOne = CreateHandle( AnchorPoint::TOP_RIGHT, mSelectionHandleOneImage, "SelectionHandleOne" );
118 mIsSelectionHandleOneFlipped = false;
120 mHandleOneGrabArea = CreateGrabArea( SELECTION_GRAB_AREA_ONE, DEFAULT_SELECTION_HANDLE_RELATIVE_SCALE );
121 mSelectionHandleOne.Add( mHandleOneGrabArea );
122 mHandleOneGrabArea.TouchedSignal().Connect(this, &TextInputHandles::OnSelectionHandleTouched);
124 // mTapDetector.Attach( mHandleOneGrabArea );
126 mSelectionHandleTwoImage = Image::New( DEFAULT_SELECTION_HANDLE_TWO );
127 mSelectionHandleTwoImagePressed = Image::New( DEFAULT_SELECTION_HANDLE_TWO_PRESSED );
128 mSelectionHandleTwo = CreateHandle( AnchorPoint::TOP_LEFT, mSelectionHandleTwoImage, "SelectionHandleTwo" );
129 mIsSelectionHandleTwoFlipped = false;
131 mHandleTwoGrabArea = CreateGrabArea( SELECTION_GRAB_AREA_TWO, DEFAULT_SELECTION_HANDLE_RELATIVE_SCALE );
132 mSelectionHandleTwo.Add( mHandleTwoGrabArea );
133 mHandleTwoGrabArea.TouchedSignal().Connect(this, &TextInputHandles::OnSelectionHandleTouched);
135 // mTapDetector.Attach( mHandleTwoGrabArea );
138 void TextInputHandles::DestorySelectionHandles()
140 if ( mSelectionHandleOne && mSelectionHandleTwo)
142 mSelectionHandleOne.Unparent();
143 mSelectionHandleTwo.Unparent();
144 mSelectionHandleOneImagePressed.Reset();
145 mSelectionHandleOneImage.Reset();
146 mSelectionHandleTwoImagePressed.Reset();
147 mSelectionHandleTwoImage.Reset();
148 mSelectionHandleOne.Reset();
149 mSelectionHandleTwo.Reset();
153 void TextInputHandles::SetSelectionHandleOneVisibility( bool visibility )
155 if ( mSelectionHandleOne )
157 mSelectionHandleOne.SetVisible( visibility );
161 void TextInputHandles::SetSelectionHandleTwoVisibility( bool visibility )
163 if ( mSelectionHandleTwo )
165 mSelectionHandleTwo.SetVisible( visibility );
169 void TextInputHandles::AttachSelectionHandlesToGivenPanGesture( PanGestureDetector& panGestureDetector )
171 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: AttachSelectionHandlesToGivenPanGesture\n" );
173 panGestureDetector.Attach( mHandleOneGrabArea );
174 panGestureDetector.Attach( mHandleTwoGrabArea );
177 void TextInputHandles::AttachSelectionHandlesToGivenTapDetector(TapGestureDetector& tapGestureDetector )
179 tapGestureDetector.Attach( mHandleOneGrabArea );
180 tapGestureDetector.Attach( mHandleTwoGrabArea );
183 void TextInputHandles::AttachGrabHandleToGivenPanGesture( PanGestureDetector& panGestureDetector )
185 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: AttachGrabHandleToGivenPanGesture\n" );
187 panGestureDetector.Attach( mGrabHandleGrabArea );
190 Actor TextInputHandles::GetSelectionHandleOne()
192 return mSelectionHandleOne;
195 Actor TextInputHandles::GetSelectionHandleTwo()
197 return mSelectionHandleTwo;
200 bool TextInputHandles::OnSelectionHandleTouched(Dali::Actor actor, const TouchEvent& touch)
202 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: OnSelectionHandleTouched\n" );
207 ImageActor handleTouched = ImageActor::DownCast( actor.GetParent() ); // Hit actor would be the GrabArea hence get parent.
209 if ( handleTouched == mSelectionHandleOne )
211 pressedImage = mSelectionHandleOneImagePressed;
212 normalImage = mSelectionHandleOneImage;
216 pressedImage = mSelectionHandleTwoImagePressed;
217 normalImage = mSelectionHandleTwoImage;
220 if (touch.GetPoint(0).state == TouchPoint::Down)
222 handleTouched.SetImage( pressedImage );
224 else if (touch.GetPoint(0).state == TouchPoint::Up )
226 handleTouched.SetImage( normalImage );
233 Actor TextInputHandles::GetGrabHandle()
235 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: GetGrabHandle\n" );
240 void TextInputHandles::CreateGrabHandle()
242 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateGrabHandle\n" );
246 if ( !mGrabHandleImage )
248 mGrabHandleImage = Image::New( DEFAULT_GRAB_HANDLE );
251 mGrabHandle = CreateHandle( AnchorPoint::TOP_CENTER, mGrabHandleImage, "GrabHandle" );
252 mGrabHandleGrabArea = CreateGrabArea( GRABHANDLE_GRAB_AREA, DEFAULT_GRAB_HANDLE_RELATIVE_SCALE );
253 mGrabHandle.Add( mGrabHandleGrabArea );
257 void TextInputHandles::DestoryGrabHandle()
261 mGrabHandle.Unparent();
262 mGrabHandleImage.Reset();
267 void TextInputHandles::SetGrabHandleImage( Dali::Image image )
271 mGrabHandleImage = image;
272 mGrabHandle.SetImage( mGrabHandleImage );
276 void TextInputHandles::SetGrabHandleVisibility( bool visibility )
278 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: SetGrabHandleVisibility (%s) \n", ( visibility )?"true":"false" );
282 mGrabHandle.SetVisible( visibility );
288 } // namespace Toolkit