Revert "[CherryPick] Input Method upversion"
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / tizen / TextSelectionMagnifier.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 "TextSelectionMagnifier.h"
30
31 #include "WebImage.h"
32 #include "ewk_util.h"
33 #include <Elementary.h>
34 #include <WebCore/IntRect.h>
35 #include <cairo.h>
36
37 using namespace WebCore;
38
39 namespace WebKit {
40
41 TextSelectionMagnifier::TextSelectionMagnifier(Evas_Object* object, WebPageProxy* page, PageClientImpl* pageClient)
42     : m_object(object),
43       m_image(0),
44       m_magnifier(0),
45       m_magnifierBackground(0),
46       m_magnifierFrame(0),
47       m_width(300),
48       m_height(100),
49       m_page(page),
50       m_pageClient(pageClient)
51 {
52 }
53
54 TextSelectionMagnifier::~TextSelectionMagnifier()
55 {
56     if (m_image)
57         evas_object_del(m_image);
58     evas_object_del(m_magnifier);
59 }
60
61 void TextSelectionMagnifier::update(const IntPoint& point)
62 {
63     if (!m_magnifier) {
64         Evas_Object* topWidget = elm_object_top_widget_get(elm_object_parent_widget_get(m_object));
65         if (!topWidget)
66             topWidget = m_object;
67
68         m_magnifier = elm_layout_add(topWidget);
69         elm_layout_file_set(m_magnifier, EDJE_DIR"/Magnifier.edj", "magnifier");
70     }
71
72     m_magnifierBackground = elm_image_add(m_magnifier);
73     elm_image_file_set(m_magnifierBackground, MAGNIFIER_IMAGE_PATH, 0);
74     elm_object_part_content_set(m_magnifier, "magnifier_bg", m_magnifierBackground);
75     evas_object_show(m_magnifierBackground);
76
77     m_magnifierFrame = elm_frame_add(m_magnifier);
78
79     int viewX, viewY, viewWidth, viewHeight;
80     evas_object_geometry_get(m_pageClient->viewWidget(), &viewX, &viewY, &viewWidth, &viewHeight);
81
82     float zoomLevel = 1.5f;
83     IntRect rect(point.x() - viewX - (ceil(m_width/zoomLevel)/2), point.y() - viewY - (ceil(m_height/zoomLevel)/2), ceil(m_width/zoomLevel), ceil(m_height/zoomLevel));
84
85     if (rect.x() < 0)
86         rect.setX(0);
87     if (rect.y() < 0)
88         rect.setY(0);
89
90     int overSize = rect.maxX() - (viewX + viewWidth);
91     if (overSize > 0)
92         rect.setX(rect.x() - overSize);
93     overSize = rect.maxY() - (viewY + viewHeight);
94     if (overSize > 0)
95         rect.setY(rect.y() - overSize);
96
97     RefPtr<WebImage> webImage = m_page->createSnapshot(rect, zoomLevel);
98     if (!webImage || !webImage->bitmap())
99         return;
100     RefPtr<cairo_surface_t> cairoSurface = webImage->bitmap()->createCairoSurface();
101     if (!cairoSurface)
102         return;
103
104     if (m_image)
105         evas_object_del(m_image);
106
107     m_image = ewk_util_image_from_cairo_surface_add(evas_object_evas_get(m_object), cairoSurface.get());
108     evas_object_size_hint_min_set(m_image, m_width, m_height);
109     evas_object_resize(m_image, m_width, m_height);
110     evas_object_image_filled_set(m_image, true);
111     evas_object_show(m_image);
112
113     elm_object_content_unset(m_magnifierFrame);
114     elm_object_content_set(m_magnifierFrame, m_image);
115
116     elm_object_style_set(m_magnifierFrame, "pad_small");
117     elm_object_part_content_set(m_magnifier, "magnifier_frame", m_magnifierFrame);
118     evas_object_show(m_magnifierFrame);
119 }
120
121 void TextSelectionMagnifier::show()
122 {
123     if (isVisible())
124         return;
125
126     evas_object_show(m_magnifier);
127 }
128
129 void TextSelectionMagnifier::hide()
130 {
131     if (!isVisible())
132         return;
133
134     evas_object_hide(m_magnifier);
135 }
136
137 void TextSelectionMagnifier::move(const IntPoint& point)
138 {
139     evas_object_move(m_magnifier, point.x(), point.y() -130);
140 }
141
142 bool TextSelectionMagnifier::isVisible()
143 {
144     return evas_object_visible_get(m_magnifier);
145 }
146
147 } // namespace WebKit
148
149 #endif // TIZEN_WEBKIT2_TEXT_SELECTION