Removed invalid handle assignments from Builder/TextInput
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / text-input / text-input-handles-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // INTERNAL INCLUDES
19 #include <dali-toolkit/internal/controls/text-input/text-input-handles-impl.h>
20 #include <dali-toolkit/internal/controls/text-input/textview-character-positions-impl.h>
21
22 #include <dali/dali.h>
23
24 #include <dali-toolkit/internal/controls/text-input/text-input-impl.h>
25 #include <dali-toolkit/internal/controls/text-view/text-processor.h>
26 #include <dali-toolkit/internal/controls/text-view/text-view-impl.h>
27 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
28 #include <dali-toolkit/public-api/controls/alignment/alignment.h>
29
30 #include <dali/integration-api/debug.h>
31
32 #include <math.h>
33 #include <sstream>
34 #include <algorithm>
35
36 using namespace Dali;
37 using namespace std;
38
39 namespace
40 {
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" );
45
46 const char* const DEFAULT_GRAB_HANDLE( DALI_IMAGE_DIR "insertpoint-icon.png" );
47
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 );
50
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");
54
55 #if defined(DEBUG_ENABLED)
56 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "TEXT_INPUT_HANDLES" );
57 #endif
58
59 Actor CreateGrabArea( const std::string& name, const Vector3& relativeScale )
60 {
61   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateGrabArea\n" );
62
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 );
67
68   return handleGrabArea;
69 }
70
71 ImageActor CreateHandle( const Vector3& anchorPoint, const Image& handleImage, const std::string& name )
72 {
73   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateSelectionHandle\n" );
74
75   ImageActor selectionHandle = ImageActor::New( handleImage );
76   selectionHandle.SetName( name );
77   selectionHandle.SetAnchorPoint( anchorPoint );
78   selectionHandle.SetDrawMode( DrawMode::OVERLAY ); // ensure handle above text
79
80   return selectionHandle;
81 }
82 }
83
84 namespace Dali
85 {
86
87 namespace Toolkit
88 {
89
90 namespace Internal
91 {
92
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 )
105 {
106 }
107
108 TextInputHandles::~TextInputHandles()
109 {
110 }
111
112 void TextInputHandles::CreateSelectionHandles()
113 {
114   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateSelectionHandles\n" );
115
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;
120
121   mHandleOneGrabArea = CreateGrabArea( SELECTION_GRAB_AREA_ONE, DEFAULT_SELECTION_HANDLE_RELATIVE_SCALE );
122   mSelectionHandleOne.Add( mHandleOneGrabArea );
123   mHandleOneGrabArea.TouchedSignal().Connect(this, &TextInputHandles::OnSelectionHandleTouched);
124
125 //  mTapDetector.Attach( mHandleOneGrabArea );
126
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;
131
132   mHandleTwoGrabArea = CreateGrabArea( SELECTION_GRAB_AREA_TWO, DEFAULT_SELECTION_HANDLE_RELATIVE_SCALE );
133   mSelectionHandleTwo.Add( mHandleTwoGrabArea );
134   mHandleTwoGrabArea.TouchedSignal().Connect(this, &TextInputHandles::OnSelectionHandleTouched);
135
136   //  mTapDetector.Attach( mHandleTwoGrabArea );
137 }
138
139 void TextInputHandles::DestorySelectionHandles()
140 {
141   if ( mSelectionHandleOne && mSelectionHandleTwo)
142   {
143     mSelectionHandleOne.Unparent();
144     mSelectionHandleTwo.Unparent();
145     mSelectionHandleOneImagePressed.Reset();
146     mSelectionHandleOneImage.Reset();
147     mSelectionHandleTwoImagePressed.Reset();
148     mSelectionHandleTwoImage.Reset();
149     mSelectionHandleOne.Reset();
150     mSelectionHandleTwo.Reset();
151   }
152 }
153
154 void TextInputHandles::SetSelectionHandleOneVisibility( bool visibility )
155 {
156   if ( mSelectionHandleOne )
157   {
158     mSelectionHandleOne.SetVisible( visibility );
159   }
160 }
161
162 void TextInputHandles::SetSelectionHandleTwoVisibility( bool visibility )
163 {
164   if ( mSelectionHandleTwo )
165   {
166     mSelectionHandleTwo.SetVisible( visibility );
167   }
168 }
169
170 void TextInputHandles::AttachSelectionHandlesToGivenPanGesture( PanGestureDetector& panGestureDetector )
171 {
172   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: AttachSelectionHandlesToGivenPanGesture\n" );
173
174   panGestureDetector.Attach( mHandleOneGrabArea );
175   panGestureDetector.Attach( mHandleTwoGrabArea );
176 }
177
178 void TextInputHandles::AttachSelectionHandlesToGivenTapDetector(TapGestureDetector& tapGestureDetector )
179 {
180   tapGestureDetector.Attach( mHandleOneGrabArea );
181   tapGestureDetector.Attach( mHandleTwoGrabArea );
182 }
183
184 void TextInputHandles::AttachGrabHandleToGivenPanGesture( PanGestureDetector& panGestureDetector )
185 {
186   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: AttachGrabHandleToGivenPanGesture\n" );
187
188   panGestureDetector.Attach( mGrabHandleGrabArea );
189 }
190
191 Actor TextInputHandles::GetSelectionHandleOne()
192 {
193   return mSelectionHandleOne;
194 }
195
196 Actor TextInputHandles::GetSelectionHandleTwo()
197 {
198   return mSelectionHandleTwo;
199 }
200
201 bool TextInputHandles::OnSelectionHandleTouched(Dali::Actor actor, const TouchEvent& touch)
202 {
203   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: OnSelectionHandleTouched\n" );
204
205   Image pressedImage;
206   Image normalImage;
207
208   ImageActor handleTouched = ImageActor::DownCast( actor.GetParent() ); // Hit actor would be the GrabArea hence get parent.
209
210   if ( handleTouched == mSelectionHandleOne )
211   {
212     pressedImage = mSelectionHandleOneImagePressed;
213     normalImage = mSelectionHandleOneImage;
214   }
215   else
216   {
217     pressedImage = mSelectionHandleTwoImagePressed;
218     normalImage = mSelectionHandleTwoImage;
219   }
220
221   if (touch.GetPoint(0).state == TouchPoint::Down)
222   {
223     handleTouched.SetImage( pressedImage );
224   }
225   else if (touch.GetPoint(0).state == TouchPoint::Up )
226   {
227     handleTouched.SetImage( normalImage );
228   }
229   return false;
230 }
231
232 // Grab handle
233
234 Actor TextInputHandles::GetGrabHandle()
235 {
236   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: GetGrabHandle\n" );
237
238   return mGrabHandle;
239 }
240
241 void TextInputHandles::CreateGrabHandle()
242 {
243   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateGrabHandle\n" );
244
245   if ( !mGrabHandle )
246   {
247     if ( !mGrabHandleImage )
248     {
249       mGrabHandleImage = Image::New( DEFAULT_GRAB_HANDLE );
250     }
251
252     mGrabHandle = CreateHandle( AnchorPoint::TOP_CENTER, mGrabHandleImage, "GrabHandle" );
253     mGrabHandleGrabArea = CreateGrabArea( GRABHANDLE_GRAB_AREA, DEFAULT_GRAB_HANDLE_RELATIVE_SCALE );
254     mGrabHandle.Add( mGrabHandleGrabArea );
255   }
256 }
257
258 void TextInputHandles::DestoryGrabHandle()
259 {
260   if ( mGrabHandle )
261   {
262     mGrabHandle.Unparent();
263     mGrabHandleImage.Reset();
264     mGrabHandle.Reset();
265   }
266 }
267
268 void TextInputHandles::SetGrabHandleImage( Dali::Image image )
269 {
270   if ( mGrabHandle )
271   {
272     mGrabHandleImage = image;
273     mGrabHandle.SetImage( mGrabHandleImage );
274   }
275 }
276
277 void TextInputHandles::SetGrabHandleVisibility( bool visibility )
278 {
279   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: SetGrabHandleVisibility (%s) \n", ( visibility )?"true":"false" );
280
281   if ( mGrabHandle )
282   {
283     mGrabHandle.SetVisible( visibility );
284   }
285 }
286
287 } // Internal
288
289 } // namespace Toolkit
290
291 } // namespace Dali
292