initial import
[vuplus_webkit] / Source / WebKit / gtk / webkit / webkithittestresult.cpp
1 /*
2  * Copyright (C) 2009 Collabora Ltd.
3  * Copyright (C) 2009 Igalia S.L.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22 #include "webkithittestresult.h"
23
24 #include "GOwnPtr.h"
25 #include "GRefPtr.h"
26 #include "HitTestResult.h"
27 #include "KURL.h"
28 #include "WebKitDOMBinding.h"
29 #include "WebKitDOMNode.h"
30 #include "webkitenumtypes.h"
31 #include "webkitglobals.h"
32 #include "webkitglobalsprivate.h"
33 #include <glib/gi18n-lib.h>
34 #include <wtf/text/CString.h>
35
36 /**
37  * SECTION:webkithittestresult
38  * @short_description: The target of a mouse event
39  *
40  * This class holds context information about the coordinates
41  * specified by a GDK event.
42  */
43
44 G_DEFINE_TYPE(WebKitHitTestResult, webkit_hit_test_result, G_TYPE_OBJECT)
45
46 struct _WebKitHitTestResultPrivate {
47     guint context;
48     char* linkURI;
49     char* imageURI;
50     char* mediaURI;
51     GRefPtr<WebKitDOMNode> innerNode;
52 };
53
54 enum {
55     PROP_0,
56
57     PROP_CONTEXT,
58     PROP_LINK_URI,
59     PROP_IMAGE_URI,
60     PROP_MEDIA_URI,
61     PROP_INNER_NODE
62 };
63
64 static void webkit_hit_test_result_finalize(GObject* object)
65 {
66     WebKitHitTestResult* web_hit_test_result = WEBKIT_HIT_TEST_RESULT(object);
67     WebKitHitTestResultPrivate* priv = web_hit_test_result->priv;
68
69     g_free(priv->linkURI);
70     g_free(priv->imageURI);
71     g_free(priv->mediaURI);
72
73     G_OBJECT_CLASS(webkit_hit_test_result_parent_class)->finalize(object);
74 }
75
76 static void webkit_hit_test_result_dispose(GObject* object)
77 {
78     WEBKIT_HIT_TEST_RESULT(object)->priv->~WebKitHitTestResultPrivate();
79
80     G_OBJECT_CLASS(webkit_hit_test_result_parent_class)->dispose(object);
81 }
82
83 static void webkit_hit_test_result_get_property(GObject* object, guint propertyID, GValue* value, GParamSpec* pspec)
84 {
85     WebKitHitTestResult* web_hit_test_result = WEBKIT_HIT_TEST_RESULT(object);
86     WebKitHitTestResultPrivate* priv = web_hit_test_result->priv;
87
88     switch(propertyID) {
89     case PROP_CONTEXT:
90         g_value_set_flags(value, priv->context);
91         break;
92     case PROP_LINK_URI:
93         g_value_set_string(value, priv->linkURI);
94         break;
95     case PROP_IMAGE_URI:
96         g_value_set_string(value, priv->imageURI);
97         break;
98     case PROP_MEDIA_URI:
99         g_value_set_string(value, priv->mediaURI);
100         break;
101     case PROP_INNER_NODE:
102         g_value_set_object(value, priv->innerNode.get());
103         break;
104     default:
105         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
106     }
107 }
108
109 static void webkit_hit_test_result_set_property(GObject* object, guint propertyID, const GValue* value, GParamSpec* pspec)
110 {
111     WebKitHitTestResult* web_hit_test_result = WEBKIT_HIT_TEST_RESULT(object);
112     WebKitHitTestResultPrivate* priv = web_hit_test_result->priv;
113
114     switch(propertyID) {
115     case PROP_CONTEXT:
116         priv->context = g_value_get_flags(value);
117         break;
118     case PROP_LINK_URI:
119         g_free (priv->linkURI);
120         priv->linkURI = g_value_dup_string(value);
121         break;
122     case PROP_IMAGE_URI:
123         g_free (priv->imageURI);
124         priv->imageURI = g_value_dup_string(value);
125         break;
126     case PROP_MEDIA_URI:
127         g_free (priv->mediaURI);
128         priv->mediaURI = g_value_dup_string(value);
129         break;
130     case PROP_INNER_NODE:
131         priv->innerNode = static_cast<WebKitDOMNode*>(g_value_get_object(value));
132         break;
133     default:
134         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
135     }
136 }
137
138 static void webkit_hit_test_result_class_init(WebKitHitTestResultClass* webHitTestResultClass)
139 {
140     GObjectClass* objectClass = G_OBJECT_CLASS(webHitTestResultClass);
141
142     objectClass->finalize = webkit_hit_test_result_finalize;
143     objectClass->dispose = webkit_hit_test_result_dispose;
144     objectClass->get_property = webkit_hit_test_result_get_property;
145     objectClass->set_property = webkit_hit_test_result_set_property;
146
147     webkitInit();
148
149     /**
150      * WebKitHitTestResult:context:
151      *
152      * Flags indicating the kind of target that received the event.
153      *
154      * Since: 1.1.15
155      */
156     g_object_class_install_property(objectClass, PROP_CONTEXT,
157                                     g_param_spec_flags("context",
158                                                        _("Context"),
159                                                        _("Flags indicating the kind of target that received the event."),
160                                                        WEBKIT_TYPE_HIT_TEST_RESULT_CONTEXT,
161                                                        WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
162                                                        static_cast<GParamFlags>((WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY))));
163
164     /**
165      * WebKitHitTestResult:link-uri:
166      *
167      * The URI to which the target that received the event points, if any.
168      *
169      * Since: 1.1.15
170      */
171     g_object_class_install_property(objectClass, PROP_LINK_URI,
172                                     g_param_spec_string("link-uri",
173                                                         _("Link URI"),
174                                                         _("The URI to which the target that received the event points, if any."),
175                                                         NULL,
176                                                         static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)));
177
178     /**
179      * WebKitHitTestResult:image-uri:
180      *
181      * The URI of the image that is part of the target that received the event, if any.
182      *
183      * Since: 1.1.15
184      */
185     g_object_class_install_property(objectClass, PROP_IMAGE_URI,
186                                     g_param_spec_string("image-uri",
187                                                         _("Image URI"),
188                                                         _("The URI of the image that is part of the target that received the event, if any."),
189                                                         NULL,
190                                                         static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)));
191
192     /**
193      * WebKitHitTestResult:media-uri:
194      *
195      * The URI of the media that is part of the target that received the event, if any.
196      *
197      * Since: 1.1.15
198      */
199     g_object_class_install_property(objectClass, PROP_MEDIA_URI,
200                                     g_param_spec_string("media-uri",
201                                                         _("Media URI"),
202                                                         _("The URI of the media that is part of the target that received the event, if any."),
203                                                         NULL,
204                                                         static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)));
205
206     /**
207      * WebKitHitTestResult:inner-node:
208      *
209      * The DOM node at the coordinates where the hit test
210      * happened. Keep in mind that the node might not be
211      * representative of the information given in the context
212      * property, since WebKit uses a series of heuristics to figure
213      * out that information. One common example is inner-node having
214      * the text node inside the anchor (&lt;a&gt;) tag; WebKit knows the
215      * whole context and will put WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK
216      * in the 'context' property, but the user might be confused by
217      * the lack of any link tag in 'inner-node'.
218      *
219      * Since: 1.3.2
220      */
221     g_object_class_install_property(objectClass, PROP_INNER_NODE,
222                                     g_param_spec_object("inner-node",
223                                                         _("Inner node"),
224                                                         _("The inner DOM node associated with the hit test result."),
225                                                         WEBKIT_TYPE_DOM_NODE,
226                                                         static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)));
227
228     g_type_class_add_private(webHitTestResultClass, sizeof(WebKitHitTestResultPrivate));
229 }
230
231 static void webkit_hit_test_result_init(WebKitHitTestResult* web_hit_test_result)
232 {
233     web_hit_test_result->priv = G_TYPE_INSTANCE_GET_PRIVATE(web_hit_test_result, WEBKIT_TYPE_HIT_TEST_RESULT, WebKitHitTestResultPrivate);
234     new (web_hit_test_result->priv) WebKitHitTestResultPrivate();
235 }
236
237 namespace WebKit {
238
239 WebKitHitTestResult* kit(const WebCore::HitTestResult& result)
240 {
241     guint context = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT;
242     GOwnPtr<char> linkURI(0);
243     GOwnPtr<char> imageURI(0);
244     GOwnPtr<char> mediaURI(0);
245     WebKitDOMNode* node = 0;
246
247     if (!result.absoluteLinkURL().isEmpty()) {
248         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK;
249         linkURI.set(g_strdup(result.absoluteLinkURL().string().utf8().data()));
250     }
251
252     if (!result.absoluteImageURL().isEmpty()) {
253         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE;
254         imageURI.set(g_strdup(result.absoluteImageURL().string().utf8().data()));
255     }
256
257     if (!result.absoluteMediaURL().isEmpty()) {
258         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA;
259         mediaURI.set(g_strdup(result.absoluteMediaURL().string().utf8().data()));
260     }
261
262     if (result.isSelected())
263         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION;
264
265     if (result.isContentEditable())
266         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE;
267
268     if (result.innerNonSharedNode())
269         node = kit(result.innerNonSharedNode());
270
271     return WEBKIT_HIT_TEST_RESULT(g_object_new(WEBKIT_TYPE_HIT_TEST_RESULT,
272                                                "link-uri", linkURI.get(),
273                                                "image-uri", imageURI.get(),
274                                                "media-uri", mediaURI.get(),
275                                                "context", context,
276                                                "inner-node", node,
277                                                NULL));
278 }
279
280 }