PushButton to use container Actor size not just ImageActors added to it
[platform/core/uifw/dali-toolkit.git] / 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 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/text-input/text-input-handles-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <math.h>
23 #include <sstream>
24 #include <algorithm>
25 #include <dali/public-api/animation/constraints.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/public-api/images/resource-image.h>
28
29 // INTERNAL INCLUDES
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>
36
37 using namespace Dali;
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.SetResizePolicy( SIZE_RELATIVE_TO_PARENT, ALL_DIMENSIONS );
66   handleGrabArea.SetSizeModeFactor( relativeScale );
67   handleGrabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
68
69   return handleGrabArea;
70 }
71
72 ImageActor CreateHandle( const Vector3& anchorPoint, const Image& handleImage, const std::string& name )
73 {
74   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateSelectionHandle\n" );
75
76   ImageActor selectionHandle = ImageActor::New( handleImage );
77   selectionHandle.SetName( name );
78   selectionHandle.SetAnchorPoint( anchorPoint );
79   selectionHandle.SetDrawMode( DrawMode::OVERLAY ); // ensure handle above text
80
81   return selectionHandle;
82 }
83 }
84
85 namespace Dali
86 {
87
88 namespace Toolkit
89 {
90
91 namespace Internal
92 {
93
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 )
106 {
107 }
108
109 TextInputHandles::~TextInputHandles()
110 {
111 }
112
113 void TextInputHandles::CreateSelectionHandles()
114 {
115   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateSelectionHandles\n" );
116
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;
121
122   mHandleOneGrabArea = CreateGrabArea( SELECTION_GRAB_AREA_ONE, DEFAULT_SELECTION_HANDLE_RELATIVE_SCALE );
123   mSelectionHandleOne.Add( mHandleOneGrabArea );
124   mHandleOneGrabArea.TouchedSignal().Connect(this, &TextInputHandles::OnSelectionHandleTouched);
125
126 //  mTapDetector.Attach( mHandleOneGrabArea );
127
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;
132
133   mHandleTwoGrabArea = CreateGrabArea( SELECTION_GRAB_AREA_TWO, DEFAULT_SELECTION_HANDLE_RELATIVE_SCALE );
134   mSelectionHandleTwo.Add( mHandleTwoGrabArea );
135   mHandleTwoGrabArea.TouchedSignal().Connect(this, &TextInputHandles::OnSelectionHandleTouched);
136
137   //  mTapDetector.Attach( mHandleTwoGrabArea );
138 }
139
140 void TextInputHandles::DestorySelectionHandles()
141 {
142   if ( mSelectionHandleOne && mSelectionHandleTwo)
143   {
144     mSelectionHandleOne.Unparent();
145     mSelectionHandleTwo.Unparent();
146     mSelectionHandleOneImagePressed.Reset();
147     mSelectionHandleOneImage.Reset();
148     mSelectionHandleTwoImagePressed.Reset();
149     mSelectionHandleTwoImage.Reset();
150     mSelectionHandleOne.Reset();
151     mSelectionHandleTwo.Reset();
152   }
153 }
154
155 void TextInputHandles::SetSelectionHandleOneVisibility( bool visibility )
156 {
157   if ( mSelectionHandleOne )
158   {
159     mSelectionHandleOne.SetVisible( visibility );
160   }
161 }
162
163 void TextInputHandles::SetSelectionHandleTwoVisibility( bool visibility )
164 {
165   if ( mSelectionHandleTwo )
166   {
167     mSelectionHandleTwo.SetVisible( visibility );
168   }
169 }
170
171 void TextInputHandles::AttachSelectionHandlesToGivenPanGesture( PanGestureDetector& panGestureDetector )
172 {
173   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: AttachSelectionHandlesToGivenPanGesture\n" );
174
175   panGestureDetector.Attach( mHandleOneGrabArea );
176   panGestureDetector.Attach( mHandleTwoGrabArea );
177 }
178
179 void TextInputHandles::AttachSelectionHandlesToGivenTapDetector(TapGestureDetector& tapGestureDetector )
180 {
181   tapGestureDetector.Attach( mHandleOneGrabArea );
182   tapGestureDetector.Attach( mHandleTwoGrabArea );
183 }
184
185 void TextInputHandles::AttachGrabHandleToGivenPanGesture( PanGestureDetector& panGestureDetector )
186 {
187   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: AttachGrabHandleToGivenPanGesture\n" );
188
189   panGestureDetector.Attach( mGrabHandleGrabArea );
190 }
191
192 Actor TextInputHandles::GetSelectionHandleOne()
193 {
194   return mSelectionHandleOne;
195 }
196
197 Actor TextInputHandles::GetSelectionHandleTwo()
198 {
199   return mSelectionHandleTwo;
200 }
201
202 bool TextInputHandles::OnSelectionHandleTouched(Dali::Actor actor, const TouchEvent& touch)
203 {
204   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: OnSelectionHandleTouched\n" );
205
206   Image pressedImage;
207   Image normalImage;
208
209   ImageActor handleTouched = ImageActor::DownCast( actor.GetParent() ); // Hit actor would be the GrabArea hence get parent.
210
211   if ( handleTouched == mSelectionHandleOne )
212   {
213     pressedImage = mSelectionHandleOneImagePressed;
214     normalImage = mSelectionHandleOneImage;
215   }
216   else
217   {
218     pressedImage = mSelectionHandleTwoImagePressed;
219     normalImage = mSelectionHandleTwoImage;
220   }
221
222   if (touch.GetPoint(0).state == TouchPoint::Down)
223   {
224     handleTouched.SetImage( pressedImage );
225   }
226   else if (touch.GetPoint(0).state == TouchPoint::Up )
227   {
228     handleTouched.SetImage( normalImage );
229   }
230   return false;
231 }
232
233 // Grab handle
234
235 Actor TextInputHandles::GetGrabHandle()
236 {
237   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: GetGrabHandle\n" );
238
239   return mGrabHandle;
240 }
241
242 void TextInputHandles::CreateGrabHandle()
243 {
244   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: CreateGrabHandle\n" );
245
246   if ( !mGrabHandle )
247   {
248     if ( !mGrabHandleImage )
249     {
250       mGrabHandleImage = ResourceImage::New( DEFAULT_GRAB_HANDLE );
251     }
252
253     mGrabHandle = CreateHandle( AnchorPoint::TOP_CENTER, mGrabHandleImage, "GrabHandle" );
254     mGrabHandleGrabArea = CreateGrabArea( GRABHANDLE_GRAB_AREA, DEFAULT_GRAB_HANDLE_RELATIVE_SCALE );
255     mGrabHandle.Add( mGrabHandleGrabArea );
256   }
257 }
258
259 void TextInputHandles::DestoryGrabHandle()
260 {
261   if ( mGrabHandle )
262   {
263     mGrabHandle.Unparent();
264     mGrabHandleImage.Reset();
265     mGrabHandle.Reset();
266   }
267 }
268
269 void TextInputHandles::SetGrabHandleImage( Dali::Image image )
270 {
271   if ( mGrabHandle )
272   {
273     mGrabHandleImage = image;
274     mGrabHandle.SetImage( mGrabHandleImage );
275   }
276 }
277
278 void TextInputHandles::SetGrabHandleVisibility( bool visibility )
279 {
280   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextInputHandles: SetGrabHandleVisibility (%s) \n", ( visibility )?"true":"false" );
281
282   if ( mGrabHandle )
283   {
284     mGrabHandle.SetVisible( visibility );
285   }
286 }
287
288 } // Internal
289
290 } // namespace Toolkit
291
292 } // namespace Dali
293