upload tizen_2.1 source
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / tizen / TextSelection.cpp
1 /*
2  * Copyright (C) 2012 Samsung Electronics
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
29 #include "TextSelection.h"
30
31 #include "EditorState.h"
32 #include "NativeWebMouseEvent.h"
33 #include "ewk_view.h"
34 #include <Elementary.h>
35
36 using namespace WebCore;
37
38 namespace WebKit {
39
40 TextSelection::TextSelection(Evas_Object* viewWidget, WebPageProxy* page, PageClientImpl* pageClient)
41       : m_object(viewWidget)
42       , m_page(page)
43       , m_pageClient(pageClient)
44       , m_isTextSelectionDowned(false)
45       , m_isTextSelectionMode(false)
46       , m_moveAnimator(0)
47 {
48     ASSERT(viewWidget);
49
50     m_viewImpl = EwkViewImpl::fromEvasObject(m_object);
51
52     const Eina_List* defaultThemeList = elm_theme_list_get(0);
53
54     const Eina_List* l;
55     void* theme;
56     EINA_LIST_FOREACH(defaultThemeList, l, theme) {
57         char* themePath = elm_theme_list_item_path_get((const char*)theme, 0);
58
59         if (themePath) {
60             m_leftHandle = new TextSelectionHandle(m_object, themePath, "elm/entry/selection/block_handle_left", true, this);
61             m_rightHandle = new TextSelectionHandle(m_object, themePath, "elm/entry/selection/block_handle_right", false, this);
62
63             free(themePath);
64             break;
65         }
66     }
67
68     m_magnifier = new TextSelectionMagnifier(m_object, page, pageClient);
69
70     evas_object_event_callback_add(m_object, EVAS_CALLBACK_MOUSE_UP, onMouseUp, this);
71 }
72
73 TextSelection::~TextSelection()
74 {
75     if (m_leftHandle)
76         delete m_leftHandle;
77
78     if (m_rightHandle)
79         delete m_rightHandle;
80
81     delete m_magnifier;
82
83     evas_object_event_callback_del(m_object, EVAS_CALLBACK_MOUSE_UP, onMouseUp);
84 }
85
86 void TextSelection::update()
87 {
88     if (isTextSelectionMode() ) {
89         EditorState editorState = m_page->editorState();
90         if (!editorState.selectionIsRange) {
91             if (editorState.isContentEditable && !evas_object_focus_get(m_object)) {
92                 WebCore::IntRect caretRect;
93                 m_page->getCaretPosition(caretRect);
94                 if (!caretRect.isEmpty())
95                     return;
96             } else {
97                 WebCore::IntRect leftRect;
98                 WebCore::IntRect rightRect;
99                 if (isTextSelectionDowned() || m_page->getSelectionHandlers(leftRect, rightRect))
100                     return;
101
102                 setIsTextSelectionMode(false);
103             }
104 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
105             m_page->getTextStyleStateForSelection();
106 #endif
107         } else {
108             if (!isTextSelectionDowned() && !isTextSelectionHandleDowned()) {
109                 updateHandlers();
110                 showContextMenu();
111 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
112                 m_page->getTextStyleStateForSelection();
113 #endif
114             }
115         }
116     }
117 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
118     else {
119         EditorState editorState = m_page->editorState();
120         if (editorState.isContentEditable && !editorState.selectionIsRange) {
121             WebCore::IntRect caretRect;
122             m_page->getCaretPosition(caretRect);
123             if (!caretRect.isEmpty()) {
124                 m_page->getTextStyleStateForSelection();
125             }
126         }
127     }
128 #endif
129 }
130
131 void TextSelection::setIsTextSelectionMode(bool isTextSelectionMode)
132 {
133     if (!isAutomaticClearEnabled())
134         return;
135
136     if (!isTextSelectionMode) {
137         hide();
138         clear();
139     }
140
141     m_isTextSelectionMode = isTextSelectionMode;
142 }
143
144 void TextSelection::clear()
145 {
146     EditorState editorState = m_page->editorState();
147     if (!editorState.selectionIsRange)
148         return;
149
150     m_page->selectionRangeClear();
151 }
152
153 void TextSelection::hide()
154 {
155     hideHandlers();
156     hideMagnifier();
157     hideContextMenu();
158 }
159
160 void TextSelection::updateHandlers()
161 {
162     WebCore::IntRect leftRect, rightRect;
163     if (!m_page->getSelectionHandlers(leftRect, rightRect)) {
164         clear();
165         return;
166     }
167
168     AffineTransform toEvasTransform = m_viewImpl->transformToScene();
169     WebCore::IntPoint leftEvasPoint = toEvasTransform.mapPoint(leftRect.minXMaxYCorner());
170     WebCore::IntPoint rightEvasPoint = toEvasTransform.mapPoint(rightRect.maxXMaxYCorner());
171
172     EditorState editorState = m_page->editorState();
173     if (editorState.isContentEditable) {
174         m_leftHandle->hide();
175         m_rightHandle->hide();
176
177         WebCore::IntRect editorRect = editorState.editorRect;
178         WebCore::IntPoint editorLeftEvasPoint = toEvasTransform.mapPoint(editorRect.location());
179         WebCore::IntPoint editorRightEvasPoint = toEvasTransform.mapPoint(editorRect.maxXMaxYCorner());
180         int webViewX, webViewY, webViewWidth, webViewHeight;
181
182         evas_object_geometry_get(m_object, &webViewX, &webViewY, &webViewWidth, &webViewHeight);
183         if ((editorLeftEvasPoint.x() <= leftEvasPoint.x() && editorLeftEvasPoint.y() <= leftEvasPoint.y())
184             && (webViewX <= leftEvasPoint.x() && webViewY <= leftEvasPoint.y())) {
185                 m_leftHandle->move(leftEvasPoint);
186                 m_leftHandle->show();
187         }
188
189         if ((editorRightEvasPoint.x() >= rightEvasPoint.x() && editorRightEvasPoint.y() >= rightEvasPoint.y())
190             && ((webViewX + webViewWidth) >= rightEvasPoint.x() && (webViewY <= rightEvasPoint.y() && (webViewY + webViewHeight) >= rightEvasPoint.y()))) {
191             m_rightHandle->move(rightEvasPoint);
192             m_rightHandle->show();
193         }
194     } else {
195         m_leftHandle->move(leftEvasPoint);
196         m_leftHandle->show();
197
198         m_rightHandle->move(rightEvasPoint);
199         m_rightHandle->show();
200     }
201 }
202
203 void TextSelection::hideHandlers()
204 {
205     m_leftHandle->hide();
206     m_rightHandle->hide();
207 }
208
209 void TextSelection::showMagnifier()
210 {
211     m_magnifier->show();
212 }
213
214 void TextSelection::hideMagnifier()
215 {
216     m_magnifier->hide();
217 }
218
219 void TextSelection::updateMagnifier(const IntPoint& position)
220 {
221     m_magnifier->update(position);
222     m_magnifier->move(position);
223 }
224
225 void TextSelection::showContextMenu()
226 {
227     if (!isEnabled())
228         return;
229
230     EditorState editorState = m_page->editorState();
231     if (editorState.isInPasswordField)
232         return;
233
234     if (!editorState.selectionIsRange && !editorState.isContentEditable)
235         return;
236
237     WebCore::IntPoint point;
238 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
239     bool isPresentInViewPort = false;
240 #endif
241     if (editorState.selectionIsRange) {
242         WebCore::IntRect leftRect, rightRect;
243         if (!m_page->getSelectionHandlers(leftRect, rightRect)) {
244             clear();
245             return;
246         }
247 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
248         // Checking if this point is in viewport area. If the calcualated
249         // point/Left/Right point are in view port then draw else do not draw the
250         // context menu. Only draw the selection points.
251         FloatRect unscaledRect = FloatRect(m_pageClient->visibleContentRect());
252         unscaledRect.scale(1 / m_pageClient->scaleFactor());
253         IntRect viewportRect = enclosingIntRect(unscaledRect);
254
255         WebCore::IntPoint visiblePoint = leftRect.center();
256         if (viewportRect.contains(visiblePoint)) {
257             // First check That the modified points are present in view port
258             point = visiblePoint;
259             isPresentInViewPort = true;
260         } else if (viewportRect.contains(leftRect.location())) {
261             // else if the calculated point is not in the view port area the
262             // draw context menu at left point if visible
263             point = leftRect.location();
264             isPresentInViewPort = true;
265         } else if (viewportRect.contains(rightRect.maxXMinYCorner())) {
266             // else if the calculated point is not in the view port area the
267             // draw context menu at right point if visible
268             point = rightRect.maxXMinYCorner();
269             isPresentInViewPort = true;
270         }
271 #else
272         point = leftRect.center();
273 #endif
274     } else if (editorState.isContentEditable) {
275         WebCore::IntRect caretRect;
276         m_page->getCaretPosition(caretRect);
277
278         if (caretRect.isEmpty())
279             return;
280
281         point = caretRect.center();
282 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
283         isPresentInViewPort = true;
284 #endif
285     }
286 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
287     if (!isPresentInViewPort)
288         return;
289 #endif
290
291     // show context menu if its in viewport else do not show the contextmenu
292
293     Ewk_View_Smart_Data* smartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get(m_object));
294     if (!smartData || !smartData->api || !smartData->api->mouse_down || !smartData->api->mouse_up)
295         return;
296
297     point = m_viewImpl->transformToScene().mapPoint(point);
298     Evas* evas = evas_object_evas_get(m_object);
299
300     // send mouse down.
301     Evas_Event_Mouse_Down mouseDown;
302     mouseDown.button = 3;
303     mouseDown.output.x = mouseDown.canvas.x = point.x();
304     mouseDown.output.y = mouseDown.canvas.y = point.y();
305     mouseDown.data = 0;
306     mouseDown.modifiers = const_cast<Evas_Modifier*>(evas_key_modifier_get(evas));
307     mouseDown.locks = const_cast<Evas_Lock*>(evas_key_lock_get(evas));
308     mouseDown.flags = EVAS_BUTTON_NONE;
309     mouseDown.timestamp = ecore_time_get() * 1000;
310     mouseDown.event_flags = EVAS_EVENT_FLAG_NONE;
311     mouseDown.dev = 0;
312     smartData->api->mouse_down(smartData, &mouseDown);
313
314     // send mouse up.
315     Evas_Event_Mouse_Up mouseUp;
316     mouseUp.button = 3;
317     mouseUp.output.x = mouseUp.canvas.x = point.x();
318     mouseUp.output.y = mouseUp.canvas.y = point.y();
319     mouseUp.data = 0;
320     mouseUp.modifiers = const_cast<Evas_Modifier*>(evas_key_modifier_get(evas));
321     mouseUp.locks = const_cast<Evas_Lock*>(evas_key_lock_get(evas));
322     mouseUp.flags = EVAS_BUTTON_NONE;
323     mouseUp.timestamp = ecore_time_get() * 1000;
324     mouseUp.event_flags = EVAS_EVENT_FLAG_NONE;
325     mouseUp.dev = 0;
326     smartData->api->mouse_up(smartData, &mouseUp);
327 }
328
329 void TextSelection::hideContextMenu()
330 {
331     if (!isEnabled())
332         return;
333
334     m_page->hideContextMenu();
335 }
336
337 void TextSelection::setLeftSelectionToEvasPoint(const IntPoint& evasPoint)
338 {
339     m_page->setLeftSelection(m_viewImpl->transformFromScene().mapPoint(evasPoint));
340     updateHandlers();
341 }
342
343 void TextSelection::setRightSelectionToEvasPoint(const IntPoint& evasPoint)
344 {
345     m_page->setRightSelection(m_viewImpl->transformFromScene().mapPoint(evasPoint));
346     updateHandlers();
347 }
348
349 // handle callbacks
350 void TextSelection::handleMouseDown(TextSelectionHandle* handle, const IntPoint& /*position*/)
351 {
352     WebCore::IntPoint basePosition;
353     EditorState editorState = m_page->editorState();
354
355     if (editorState.selectionIsRange) {
356         WebCore::IntRect leftRect, rightRect;
357         if (!m_page->getSelectionHandlers(leftRect, rightRect)) {
358             clear();
359             return;
360         }
361
362         if (handle->isLeft()) {
363             basePosition.setX(leftRect.x());
364             basePosition.setY(leftRect.y() + (leftRect.height()/2));
365         } else {
366             basePosition.setX(rightRect.x() + rightRect.width());
367             basePosition.setY(rightRect.y() + (rightRect.height()/2));
368         }
369
370         handle->setBasePositionForMove(m_viewImpl->transformToScene().mapPoint(basePosition));
371     } else
372         return;
373
374     hideContextMenu();
375     updateMagnifier(handle->position());
376     showMagnifier();
377 }
378
379 void TextSelection::handleMouseMove(TextSelectionHandle* handle, const IntPoint& position)
380 {
381     if (handle->isLeft())
382         setLeftSelectionToEvasPoint(position);
383     else
384         setRightSelectionToEvasPoint(position);
385
386     updateMagnifier(handle->position());
387 }
388
389 void TextSelection::handleMouseUp(TextSelectionHandle* /* handle */, const IntPoint& /* position */)
390 {
391     hideMagnifier();
392     showContextMenu();
393 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
394     m_page->getTextStyleStateForSelection();
395 #endif
396 }
397
398 bool TextSelection::isMagnifierVisible()
399 {
400     return m_magnifier->isVisible();
401 }
402
403 void TextSelection::updateHandlesAndContextMenu(bool isShow, bool isScrolling)
404 {
405     if (isTextSelectionDowned() && !isScrolling) {
406         showMagnifier();
407         return;
408     }
409
410     if (isShow) {
411         updateHandlers();
412         showContextMenu();
413     } else {
414         hideHandlers();
415         hideContextMenu();
416     }
417
418     if (isScrolling && isMagnifierVisible())
419         hideMagnifier();
420 }
421
422 void TextSelection::startMoveAnimator()
423 {
424     if (!isEnabled() || !isTextSelectionDowned())
425         return;
426
427     if (!m_moveAnimator)
428         m_moveAnimator = ecore_animator_add(moveAnimatorCallback, this);
429 }
430
431 void TextSelection::stopMoveAnimator()
432 {
433     if (m_moveAnimator) {
434         ecore_animator_del(m_moveAnimator);
435         m_moveAnimator = 0;
436     }
437 }
438
439 void TextSelection::onMouseUp(void* data, Evas*, Evas_Object*, void* eventInfo)
440 {
441     static_cast<TextSelection*>(data)->textSelectionUp(IntPoint());
442 }
443
444 Eina_Bool TextSelection::moveAnimatorCallback(void* data)
445 {
446     TextSelection* textSelection = static_cast<TextSelection*>(data);
447
448     Evas_Coord_Point point;
449     evas_pointer_canvas_xy_get(evas_object_evas_get(textSelection->m_object), &point.x, &point.y);
450     textSelection->textSelectionMove(IntPoint(point.x, point.y));
451
452     return ECORE_CALLBACK_RENEW;
453 }
454
455 // 'return false' means text selection is not possible for point.
456 // 'return true' means text selection is possible.
457 bool TextSelection::textSelectionDown(const IntPoint& point, bool isStartedTextSelectionFromOutside)
458 {
459     setIsTextSelectionMode(false);
460
461     IntPoint contentsPoint = m_viewImpl->transformFromScene().mapPoint(point);
462     bool result = m_page->selectClosestWord(contentsPoint, isStartedTextSelectionFromOutside);
463     if (!result)
464         return false;
465
466     if (isTextSelectionMode()) {
467         hide();
468     } else {
469         setIsTextSelectionMode(true);
470     }
471     setIsTextSelectionDowned(true);
472
473     updateMagnifier(point);
474     showMagnifier();
475
476     startMoveAnimator();
477
478     return true;
479 }
480
481 void TextSelection::textSelectionMove(const IntPoint& point, bool isStartedTextSelectionFromOutside)
482 {
483     if (!isTextSelectionMode()) {
484         stopMoveAnimator();
485         return;
486     }
487
488     WebCore::IntPoint viewPoint = m_viewImpl->transformFromScene().mapPoint(point);
489     EditorState editorState = m_page->editorState();
490     if (editorState.isContentEditable) {
491         IntRect mapRect = m_viewImpl->transformToScene().mapRect(editorState.editorRect);
492         if(mapRect.contains(point)) {
493             m_page->selectClosestWord(viewPoint, isStartedTextSelectionFromOutside);
494             updateMagnifier(point);
495         }
496     } else {
497         m_page->selectClosestWord(viewPoint, isStartedTextSelectionFromOutside);
498         updateMagnifier(point);
499     }
500     showMagnifier();
501 }
502
503 void TextSelection::textSelectionUp(const IntPoint& point, bool isStartedTextSelectionFromOutside)
504 {
505     stopMoveAnimator();
506
507     if (!isTextSelectionMode() || !isTextSelectionDowned())
508         return;
509
510     setIsTextSelectionDowned(false);
511     hideMagnifier();
512
513     EditorState editorState = m_page->editorState();
514     if (editorState.selectionIsRange || editorState.isContentEditable) {
515         if (editorState.selectionIsRange)
516             updateHandlers();
517
518         showContextMenu();
519     } else if (!isStartedTextSelectionFromOutside)
520         setIsTextSelectionMode(false);
521
522 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
523     m_page->getTextStyleStateForSelection();
524 #endif
525 }
526
527 #if ENABLE(TIZEN_WEBKIT2_FOR_MOVING_TEXT_SELECTION_HANDLE_FROM_OSP)
528 TextSelectionHandle* TextSelection::getSelectedHandle(const IntPoint& position)
529 {
530     WebCore::IntRect leftHandleRect = m_leftHandle->getHandleRect();
531     if (leftHandleRect.contains(position))
532         return m_leftHandle;
533
534     WebCore::IntRect rightHandleRect = m_rightHandle->getHandleRect();
535     if (rightHandleRect.contains(position))
536         return m_rightHandle;
537
538     return 0;
539 }
540
541 void TextSelection::textSelectionHandleDown(const IntPoint& position)
542 {
543     TextSelectionHandle* selectedHandle = getSelectedHandle(position);
544     if (selectedHandle) {
545         selectedHandle->mouseDown(position);
546         if (selectedHandle->isMouseDowned())
547             return;
548     }
549
550     EditorState editorState = m_page->editorState();
551     if (editorState.isContentEditable)
552         setIsTextSelectionMode(false);
553 }
554
555 void TextSelection::textSelectionHandleMove(const IntPoint& position)
556 {
557     if (isTextSelectionHandleDowned()) {
558         TextSelectionHandle* selectedHandle = m_leftHandle->isMouseDowned() ? m_leftHandle : m_rightHandle;
559         selectedHandle->mouseMove(position);
560     }
561 }
562
563 void TextSelection::textSelectionHandleUp()
564 {
565     if (isTextSelectionHandleDowned()) {
566         TextSelectionHandle* selectedHandle = m_leftHandle->isMouseDowned() ? m_leftHandle : m_rightHandle;
567         selectedHandle->mouseUp();
568     }
569 }
570 #endif
571
572 bool TextSelection::isEnabled()
573 {
574     return ewk_settings_text_selection_enabled_get(ewk_view_settings_get(m_object));
575 }
576
577 bool TextSelection::isAutomaticClearEnabled()
578 {
579     return ewk_settings_clear_text_selection_automatically_get(ewk_view_settings_get(m_object));
580 }
581
582 } // namespace WebKit
583
584 #endif // TIZEN_WEBKIT2_TEXT_SELECTION