From 6b14d9e8830756574d99e4e990bab953ea0b76fe Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Tue, 13 Mar 2007 12:51:15 +0100 Subject: [PATCH] Get the image uri from the DOM --- lib/python/_sugar.override | 12 +++++++--- lib/src/sugar-browser.cpp | 49 ++++++++++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/lib/python/_sugar.override b/lib/python/_sugar.override index ff3f9d9a..09114528 100644 --- a/lib/python/_sugar.override +++ b/lib/python/_sugar.override @@ -19,7 +19,7 @@ headers extern Pycairo_CAPI_t *Pycairo_CAPI; %% -modulename _sugar +modulename gecko %% import gobject.GObject as PyGObject_Type import gtk.Entry as PyGtkEntry_Type @@ -187,8 +187,14 @@ _wrap_sugar_browser_event_tp_getattr(PyObject *self, char *attr) if (!strcmp(attr, "__members__")) return Py_BuildValue("[s]", "image_uri"); - if (!strcmp(attr, "image_uri")) - return PyString_FromString(event->image_uri); + if (!strcmp(attr, "image_uri")) { + if (event->image_uri) { + return PyString_FromString(event->image_uri); + } else { + Py_INCREF(Py_None); + return Py_None; + } + } return NULL; } diff --git a/lib/src/sugar-browser.cpp b/lib/src/sugar-browser.cpp index 29e41347..cf1d9edb 100644 --- a/lib/src/sugar-browser.cpp +++ b/lib/src/sugar-browser.cpp @@ -37,6 +37,9 @@ #include #include #include +#include +#include +#include enum { PROP_0, @@ -370,15 +373,53 @@ location_cb(GtkMozEmbed *embed) } static gboolean -dom_mouse_click_cb(GtkMozEmbed *embed, nsIDOMMouseEvent *dom_event) +dom_mouse_click_cb(GtkMozEmbed *embed, nsIDOMMouseEvent *mouseEvent) { SugarBrowser *browser = SUGAR_BROWSER(embed); SugarBrowserEvent *event; gint return_value = FALSE; + nsCOMPtr eventTarget; + mouseEvent->GetTarget(getter_AddRefs(eventTarget)); + NS_ENSURE_TRUE(mouseEvent, FALSE); + + nsCOMPtr targetNode; + targetNode = do_QueryInterface(eventTarget); + NS_ENSURE_TRUE(targetNode, FALSE); + event = sugar_browser_event_new(); - event->image_uri = g_strdup("testimage"); - + + nsresult rv; + + PRUint16 type; + rv = targetNode->GetNodeType(&type); + if (NS_FAILED(rv)) return NS_ERROR_FAILURE; + + nsCOMPtr element = do_QueryInterface(targetNode); + if ((nsIDOMNode::ELEMENT_NODE == type) && element) { + nsString uTag; + rv = element->GetLocalName(uTag); + if (NS_FAILED(rv)) return NS_ERROR_FAILURE; + + nsCString tag; + NS_UTF16ToCString (uTag, NS_CSTRING_ENCODING_UTF8, tag); + + if (g_ascii_strcasecmp (tag.get(), "img") == 0) { + nsString img; + + nsCOMPtr image; + image = do_QueryInterface(targetNode, &rv); + if (NS_FAILED(rv) || !image) return NS_ERROR_FAILURE; + + rv = image->GetSrc(img); + if (NS_FAILED(rv)) return NS_ERROR_FAILURE; + + nsCString cImg; + NS_UTF16ToCString (img, NS_CSTRING_ENCODING_UTF8, cImg); + event->image_uri = g_strdup(cImg.get()); + } + } + g_signal_emit(browser, signals[MOUSE_CLICK], 0, event, &return_value); return return_value; @@ -421,7 +462,7 @@ sugar_browser_scroll_pixels(SugarBrowser *browser, nsCOMPtr DOMWindow; webBrowserFocus->GetFocusedWindow (getter_AddRefs(DOMWindow)); if (!DOMWindow) { - webBrowser->GetContentDOMWindow (getter_AddRefs (DOMWindow)); + webBrowser->GetContentDOMWindow (getter_AddRefs(DOMWindow)); } NS_ENSURE_TRUE (DOMWindow, );