summaryrefslogtreecommitdiff
path: root/tests/test-browser.cc
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test-browser.cc')
-rw-r--r--tests/test-browser.cc336
1 files changed, 336 insertions, 0 deletions
diff --git a/tests/test-browser.cc b/tests/test-browser.cc
new file mode 100644
index 0000000..3e50853
--- /dev/null
+++ b/tests/test-browser.cc
@@ -0,0 +1,336 @@
+#include "config.h"
+
+#define MOZILLA_CLIENT
+
+#include <hildon/hildon.h>
+#include <stdlib.h>
+
+#include <gtkmozembed.h>
+#include <gtkmozembed_internal.h>
+
+#include <nsCOMPtr.h>
+#include <nsIDOMWindow.h>
+#include <nsIDOMWindowUtils.h>
+#include <nsIInterfaceRequestorUtils.h>
+#include <nsEmbedString.h>
+
+static gboolean
+expose_event_cb (GtkWidget *widget, GdkEventExpose *event)
+{
+
+ cairo_t *cr = gdk_cairo_create (widget->window);
+
+ GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
+ gdk_cairo_set_source_pixmap (cr, child->window, child->allocation.x, child->allocation.y);
+
+ /* draw no more than our expose event intersects our child */
+ GdkRegion *region = gdk_region_rectangle (&child->allocation);
+ gdk_region_intersect (region, event->region);
+ gdk_cairo_region (cr, region);
+ cairo_clip (cr);
+
+ /* composite, with a 50% opacity */
+ cairo_paint_with_alpha (cr, 254./255);
+
+ /* scollbars */
+ cairo_set_source_rgba (cr, 0.8, 0.8, 0.8, 0.8);
+
+ cairo_rectangle (cr,
+ child->allocation.x + child->allocation.width -
+ HILDON_MARGIN_DEFAULT - HILDON_MARGIN_HALF,
+ child->allocation.y + HILDON_MARGIN_HALF,
+ HILDON_MARGIN_DEFAULT,
+ 0.6 * (child->allocation.height - HILDON_MARGIN_DOUBLE));
+ cairo_rectangle (cr,
+ child->allocation.x + HILDON_MARGIN_HALF,
+ child->allocation.y + child->allocation.height -
+ HILDON_MARGIN_DEFAULT - HILDON_MARGIN_HALF,
+ 0.6 * (child->allocation.width - HILDON_MARGIN_DOUBLE),
+ HILDON_MARGIN_DEFAULT);
+
+ cairo_fill (cr);
+
+ cairo_destroy (cr);
+ return FALSE;
+}
+
+static GdkEvent *last_button_event = NULL;
+
+#if 0
+#include <gdk/gdkx.h>
+
+static const char *
+event_name (GdkEventType type)
+{
+ static GEnumClass *enum_class = NULL;
+ GEnumValue *value;
+
+ if (!enum_class)
+ enum_class = static_cast<GEnumClass *> (g_type_class_ref (GDK_TYPE_EVENT_TYPE));
+
+ value = g_enum_get_value (enum_class, type);
+ return (value ? value->value_nick : "unknown");
+}
+
+static void
+send_motion_event (GtkWidget *widget, GdkEventMotion *event)
+{
+ XEvent xev;
+
+ xev.xmotion.type = MotionNotify;
+ xev.xmotion.serial = 0;
+ xev.xmotion.send_event = True;
+ xev.xmotion.display = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (widget));
+ xev.xmotion.window = GDK_WINDOW_XWINDOW (gtk_widget_get_window (widget));
+ xev.xmotion.root = GDK_WINDOW_XWINDOW (gtk_widget_get_root_window (widget));
+ xev.xmotion.subwindow = GDK_WINDOW_XWINDOW (gtk_widget_get_window (widget));
+ xev.xmotion.time = event->time;
+ xev.xmotion.x = event->x;
+ xev.xmotion.y = event->y;
+ xev.xmotion.x_root = event->x_root;
+ xev.xmotion.y_root = event->y_root;
+ xev.xmotion.same_screen = True;
+
+ xev.xmotion.state = event->state;
+ xev.xmotion.is_hint = event->is_hint;
+
+ XSendEvent(xev.xbutton.display, xev.xbutton.window, True, 0, &xev);
+}
+
+static void
+send_crossing_event (GtkWidget *widget, GdkEventCrossing *event)
+{
+ XEvent xev;
+
+ switch (event->type)
+ {
+ case GDK_ENTER_NOTIFY:
+ xev.xcrossing.type = EnterNotify;
+ break;
+ case GDK_LEAVE_NOTIFY:
+ xev.xcrossing.type = LeaveNotify;
+ break;
+ default:
+ g_return_if_reached ();
+ }
+
+ xev.xcrossing.serial = 0;
+ xev.xcrossing.send_event = True;
+ xev.xcrossing.display = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (widget));
+ xev.xcrossing.window = GDK_WINDOW_XWINDOW (gtk_widget_get_window (widget));
+ xev.xcrossing.root = GDK_WINDOW_XWINDOW (gtk_widget_get_root_window (widget));
+ xev.xcrossing.subwindow = GDK_WINDOW_XWINDOW (gtk_widget_get_window (widget));
+ xev.xcrossing.time = event->time;
+ xev.xcrossing.x = event->x;
+ xev.xcrossing.y = event->y;
+ xev.xcrossing.x_root = event->x_root;
+ xev.xcrossing.y_root = event->y_root;
+ xev.xcrossing.same_screen = True;
+
+ xev.xcrossing.mode = event->mode;
+ xev.xcrossing.detail = event->detail;
+ xev.xcrossing.focus = event->focus;
+ xev.xcrossing.state = event->state;
+
+ XSendEvent(xev.xbutton.display, xev.xbutton.window, True, 0, &xev);
+}
+
+static gboolean
+event_cb (GtkWidget *widget, GdkEvent *event)
+{
+ GtkWidget *child;
+
+ g_print ("%s: %s event (%d) for %s (%p/%p)\n",
+ G_STRFUNC, event_name (event->type),
+ event->type, gtk_widget_get_name (widget),
+ widget->window, event->any.window);
+
+ if (!GTK_IS_BIN (widget) || widget->window != event->any.window)
+ return FALSE;
+
+ child = gtk_bin_get_child (GTK_BIN (widget));
+
+ switch (event->type)
+ {
+ case GDK_MOTION_NOTIFY:
+ send_motion_event (child, &event->motion);
+ break;
+
+ case GDK_ENTER_NOTIFY:
+ case GDK_LEAVE_NOTIFY:
+ send_crossing_event (child, &event->crossing);
+ break;
+
+ default:
+ break;
+ }
+
+ return FALSE;
+}
+#endif
+
+static void
+send_mouse_event (GtkWidget *embed, GdkEvent *event, const nsAString &name)
+{
+ nsCOMPtr<nsIWebBrowser> browser;
+ gtk_moz_embed_get_nsIWebBrowser (GTK_MOZ_EMBED (embed), getter_AddRefs (browser));
+
+ nsCOMPtr<nsIDOMWindow> window;
+ browser->GetContentDOMWindow (getter_AddRefs (window));
+
+ nsCOMPtr<nsIDOMWindowUtils> utils (do_GetInterface (window));
+
+ utils->SendMouseEvent(name,
+ float(event->button.x), float(event->button.y),
+ event->button.button, 1, 0, PR_FALSE);
+}
+
+static gboolean
+button_press_cb (GtkWidget *widget, GdkEvent *event)
+{
+ g_print ("%s: event-type=%d\n", G_STRFUNC, event->type);
+
+ if (last_button_event)
+ gdk_event_free (last_button_event);
+
+ last_button_event = gdk_event_copy (event);
+
+ GtkWidget *embed = gtk_bin_get_child (GTK_BIN (widget));
+ send_mouse_event (embed, event, NS_LITERAL_STRING("mousedown"));
+
+ return TRUE;
+}
+
+static gboolean
+button_release_cb (GtkWidget *widget, GdkEvent *event)
+{
+ g_print ("%s: event-type=%d, %p %p\n", G_STRFUNC, event->type, widget->window, event->any.window);
+
+ GtkWidget *embed = gtk_bin_get_child (GTK_BIN (widget));
+ send_mouse_event (embed, event, NS_LITERAL_STRING("mouseup"));
+
+ if (last_button_event)
+ {
+ int dx = abs (event->button.x - last_button_event->button.x);
+ int dy = abs (event->button.y - last_button_event->button.y);
+
+ if (3 >= dx && 3 >= dy)
+ {
+ g_print ("foo\n");
+ send_mouse_event (embed, event, NS_LITERAL_STRING("click"));
+#if 0
+XEvent xev;
+xev.xbutton.type = ButtonPress;
+xev.xbutton.serial = 0;
+xev.xbutton.send_event = True;
+xev.xbutton.display = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (child));
+xev.xbutton.window = GDK_WINDOW_XWINDOW (gtk_widget_get_window (child));
+xev.xbutton.root = GDK_WINDOW_XWINDOW (gtk_widget_get_root_window (child));
+xev.xbutton.subwindow = GDK_WINDOW_XWINDOW (gtk_widget_get_window (child));
+xev.xbutton.time = last_button_event->button.time;
+xev.xbutton.x = last_button_event->button.x;
+xev.xbutton.y = last_button_event->button.y;
+xev.xbutton.x_root = last_button_event->button.x_root;
+xev.xbutton.y_root = last_button_event->button.y_root;
+xev.xbutton.state = last_button_event->button.state;
+xev.xbutton.button = last_button_event->button.button;
+xev.xbutton.same_screen = True;
+XSendEvent(xev.xbutton.display, xev.xbutton.window, True, 0, &xev);
+
+xev.xbutton.type = ButtonRelease;
+xev.xbutton.serial = 0;
+xev.xbutton.send_event = True;
+xev.xbutton.display = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (child));
+xev.xbutton.window = GDK_WINDOW_XWINDOW (gtk_widget_get_window (child));
+xev.xbutton.root = GDK_WINDOW_XWINDOW (gtk_widget_get_root_window (child));
+xev.xbutton.subwindow = GDK_WINDOW_XWINDOW (gtk_widget_get_window (child));
+xev.xbutton.time = event->button.time;
+xev.xbutton.x = event->button.x;
+xev.xbutton.y = event->button.y;
+xev.xbutton.x_root = event->button.x_root;
+xev.xbutton.y_root = event->button.y_root;
+xev.xbutton.state = event->button.state;
+xev.xbutton.button = event->button.button;
+xev.xbutton.same_screen = True;
+XSendEvent(xev.xbutton.display, xev.xbutton.window, True, 0, &xev);
+#endif
+#if 0
+ last_button_event->button.window = child->window;
+ gtk_widget_event (child, last_button_event);
+
+ event = gdk_event_copy (event);
+ event->button.window = child->window;
+ gtk_widget_event (child, event);
+ //gdk_event_free (event);
+#endif
+ }
+ else
+ gdk_event_free (last_button_event);
+
+ last_button_event = NULL;
+ }
+
+ return FALSE;
+}
+
+static gboolean
+moz_button_press_cb (GtkWidget *widget, GdkEvent *event)
+{
+ g_print ("%s: window=%p/%p, event=%p/%p\n", G_STRFUNC, widget->window, event->any.window, event, last_button_event);
+ return TRUE;
+}
+
+static gboolean
+moz_button_release_cb (GtkWidget *widget, GdkEvent *event)
+{
+ g_print ("%s: window=%p/%p, event=%p/%p\n", G_STRFUNC, widget->window, event->any.window, event, last_button_event);
+ return TRUE;
+}
+
+static int
+open_uri_cb (GtkMozEmbed *embed, const char *aURI)
+{
+ g_print ("%s: %s\n", __func__, aURI);
+ return 0;
+}
+
+int
+main (int argc, char **argv)
+{
+ guint32 chrome;
+
+ hildon_gtk_init (&argc, &argv);
+
+ GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+ g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
+ g_signal_connect_after (window, "expose-event", G_CALLBACK (expose_event_cb), NULL);
+
+ GtkWidget *ebox = gtk_event_box_new ();
+ gtk_container_add (GTK_CONTAINER (window), ebox);
+ gtk_event_box_set_above_child (GTK_EVENT_BOX (ebox), TRUE);
+ //g_signal_connect (ebox, "event", G_CALLBACK (event_cb), NULL);
+ g_signal_connect (ebox, "button-press-event", G_CALLBACK (button_press_cb), NULL);
+ g_signal_connect (ebox, "button-release-event", G_CALLBACK (button_release_cb), NULL);
+ gtk_widget_add_events (ebox, GDK_POINTER_MOTION_MASK);
+
+ gtk_moz_embed_set_path (MOZILLA_LIBDIR);
+ GtkWidget *embed = gtk_moz_embed_new ();
+
+ chrome = gtk_moz_embed_get_chrome_mask (GTK_MOZ_EMBED (embed));
+ gtk_moz_embed_set_chrome_mask (GTK_MOZ_EMBED (embed), chrome & ~GTK_MOZ_EMBED_FLAG_SCROLLBARSON);
+ gtk_moz_embed_load_url (GTK_MOZ_EMBED (embed), "file:///home/mathias/.dashboard.html");
+ g_signal_connect (embed, "button-press-event", G_CALLBACK (moz_button_press_cb), NULL);
+ g_signal_connect (embed, "button-release-event", G_CALLBACK (moz_button_release_cb), NULL);
+ g_signal_connect (embed, "open-uri", G_CALLBACK (open_uri_cb), NULL);
+ //g_signal_connect (embed, "event", G_CALLBACK (event_cb), NULL);
+ gtk_container_add (GTK_CONTAINER (ebox), embed);
+
+ gtk_widget_show_all (window);
+
+ gdk_window_set_composited (ebox->window, TRUE);
+
+ gtk_main ();
+
+ return 0;
+
+}