? src/P
Index: configure.ac
===================================================================
RCS file: /cvs/gnome/epiphany/configure.ac,v
retrieving revision 1.68.2.22
diff -p -u -u -p -r1.68.2.22 configure.ac
--- configure.ac	12 Jul 2005 20:54:18 -0000	1.68.2.22
+++ configure.ac	13 Aug 2005 12:56:21 -0000
@@ -413,6 +413,10 @@ CXXFLAGS=$_SAVE_CXXFLAGS
 
 AC_LANG_POP([C++])
 
+dnl Broken everywhere
+
+AC_DEFINE([GTKMOZEMBED_BROKEN_FOCUS],[1],[Define if GtkMozEmbed has broken focus handling])
+
 dnl Needed since 1.8b2
 dnl Define this down here so it doesn't affect the API checks above
 
Index: embed/mozilla/EphyBrowser.cpp
===================================================================
RCS file: /cvs/gnome/epiphany/embed/mozilla/EphyBrowser.cpp,v
retrieving revision 1.76.2.8
diff -p -u -u -p -r1.76.2.8 EphyBrowser.cpp
--- embed/mozilla/EphyBrowser.cpp	12 Jul 2005 20:54:18 -0000	1.76.2.8
+++ embed/mozilla/EphyBrowser.cpp	13 Aug 2005 12:56:21 -0000
@@ -482,6 +482,9 @@ nsresult EphyBrowser::Init (GtkMozEmbed 
 					 getter_AddRefs(mWebBrowser));
 	NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);
 
+	mWebBrowserFocus = do_QueryInterface (mWebBrowser);
+	NS_ENSURE_TRUE (mWebBrowserFocus, NS_ERROR_FAILURE);
+
 	mWebBrowser->GetContentDOMWindow (getter_AddRefs (mDOMWindow));
 	NS_ENSURE_TRUE (mDOMWindow, NS_ERROR_FAILURE);
 
@@ -803,13 +806,9 @@ nsresult EphyBrowser::GetTargetDocument 
 	}
 
 	/* Use the focused document */
-	nsCOMPtr<nsIWebBrowserFocus> webBrowserFocus;
-	webBrowserFocus = do_QueryInterface (mWebBrowser);
-	NS_ENSURE_TRUE (webBrowserFocus, NS_ERROR_FAILURE);
-
 	nsresult rv;
 	nsCOMPtr<nsIDOMWindow> DOMWindow;
-	rv = webBrowserFocus->GetFocusedWindow (getter_AddRefs(DOMWindow));
+	rv = mWebBrowserFocus->GetFocusedWindow (getter_AddRefs(DOMWindow));
 	if (NS_SUCCEEDED (rv) && DOMWindow)
 	{
 		return DOMWindow->GetDocument (aDOMDocument);
@@ -1295,3 +1294,21 @@ EphyBrowser::GetDocumentType ()
 
   return type;
 }
+
+#ifdef GTKMOZEMBED_BROKEN_FOCUS
+nsresult
+EphyBrowser::FocusActivate ()
+{
+	NS_ENSURE_STATE (mWebBrowserFocus);
+
+	return mWebBrowserFocus->Activate();
+}
+
+nsresult
+EphyBrowser::FocusDeactivate ()
+{
+	NS_ENSURE_STATE (mWebBrowserFocus);
+
+	return mWebBrowserFocus->Deactivate();
+}
+#endif /* GTKMOZEMBED_BROKEN_FOCUS */
Index: embed/mozilla/EphyBrowser.h
===================================================================
RCS file: /cvs/gnome/epiphany/embed/mozilla/EphyBrowser.h,v
retrieving revision 1.37.2.2
diff -p -u -u -p -r1.37.2.2 EphyBrowser.h
--- embed/mozilla/EphyBrowser.h	12 Jul 2005 20:54:18 -0000	1.37.2.2
+++ embed/mozilla/EphyBrowser.h	13 Aug 2005 12:56:21 -0000
@@ -34,6 +34,7 @@
 #include <nsIWebNavigation.h>
 #include <nsISHistory.h>
 #include <nsIWebBrowser.h>
+#include <nsIWebBrowserFocus.h>
 #include <nsIDOMDocument.h>
 #include <nsIDOMWindow.h>
 #include <nsIPrintSettings.h>
@@ -162,10 +163,16 @@ public:
 
         EphyEmbedDocumentType GetDocumentType ();
 
+#ifdef GTKMOZEMBED_BROKEN_FOCUS
+	nsresult FocusActivate ();
+	nsresult FocusDeactivate ();
+#endif
+
 	nsCOMPtr<nsIWebBrowser> mWebBrowser;
 private:
 	GtkWidget *mEmbed;
 
+	nsCOMPtr<nsIWebBrowserFocus> mWebBrowserFocus;
 	nsCOMPtr<nsIDOMDocument> mTargetDocument;
 	nsCOMPtr<nsIDOMEventTarget> mEventTarget;
 	nsCOMPtr<nsIDOMWindow> mDOMWindow;
Index: embed/mozilla/mozilla-embed.cpp
===================================================================
RCS file: /cvs/gnome/epiphany/embed/mozilla/mozilla-embed.cpp,v
retrieving revision 1.101.2.5
diff -p -u -u -p -r1.101.2.5 mozilla-embed.cpp
--- embed/mozilla/mozilla-embed.cpp	12 Jul 2005 20:54:18 -0000	1.101.2.5
+++ embed/mozilla/mozilla-embed.cpp	13 Aug 2005 12:56:22 -0000
@@ -88,12 +88,20 @@ struct MozillaEmbedPrivate
 {
 	EphyBrowser *browser;
 	MozillaEmbedLoadState load_state;
+#ifdef GTKMOZEMBED_BROKEN_FOCUS
+	guint focus_connected : 1;
+#endif /* GTKMOZEMBED_BROKEN_FOCUS */
 };
 
 #define WINDOWWATCHER_CONTRACTID "@mozilla.org/embedcomp/window-watcher;1"
 
 static GObjectClass *parent_class = NULL;
 
+#ifdef GTKMOZEMBED_BROKEN_FOCUS
+static guint fiesid = 0;
+static guint foesid = 0;
+#endif /* GTKMOZEMBED_BROKEN_FOCUS */
+
 static void
 impl_manager_do_command (EphyCommandManager *manager,
 			 const char *command) 
@@ -207,20 +215,98 @@ impl_find_set_properties (EphyEmbed *emb
 					   wrap_around); 
 }
 
+#ifdef GTKMOZEMBED_BROKEN_FOCUS
+static gboolean
+child_focus_in_event_cb (GtkWidget *child,
+			 GdkEventFocus *event,
+			 MozillaEmbed *embed)
+{
+	embed->priv->browser->FocusActivate ();
+
+	return FALSE;
+}
+
+static gboolean
+child_focus_out_event_cb (GtkWidget *child,
+			  GdkEventFocus *event,
+			  MozillaEmbed *embed)
+{
+	embed->priv->browser->FocusDeactivate ();
+
+	return FALSE;
+}
+#endif /* GTKMOZEMBED_BROKEN_FOCUS */
+
 static void
 mozilla_embed_realize (GtkWidget *widget)
 {
 	MozillaEmbedPrivate *mpriv = MOZILLA_EMBED (widget)->priv;
+	GtkBin *bin = GTK_BIN (widget);
 
-	(* GTK_WIDGET_CLASS(parent_class)->realize) (widget);
+	GTK_WIDGET_CLASS (parent_class)->realize (widget);
 
+	/* Initialise our helper class */
 	nsresult rv;
 	rv = mpriv->browser->Init (GTK_MOZ_EMBED (widget));
-
 	if (NS_FAILED (rv))
 	{
-               	g_warning ("EphyBrowser initialization failed for %p\n", widget);
+		g_warning ("EphyBrowser initialization failed for %p\n", widget);
+		return;
 	}
+
+#ifdef GTKMOZEMBED_BROKEN_FOCUS
+	/* HACK ALERT! This depends highly on undocumented interna of
+	 * GtkMozEmbed!
+	 *
+	 * GtkMozEmbed::realize installs focus-[in|out]-event handlers to
+	 * toplevel, and, on the first realize only, to the child.
+	 * GtkMozEmbed disconnects its focus-[in|out]-event handler
+	 * to the toplevel on unrealize, and leaves the ones to the child
+	 * in place. So we don't need to unblock the blocked handlers
+	 * and therefore need no ::unrealize handler.
+	*/
+
+	GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
+	gpointer data = ((GtkMozEmbed *) widget)->data;
+
+	guint n;
+
+	n = g_signal_handlers_block_matched (toplevel,
+					     (GSignalMatchType) (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DATA),
+					     fiesid, 0, NULL, NULL, data);
+	n += g_signal_handlers_block_matched (toplevel,
+					      (GSignalMatchType) (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DATA),
+					      foesid, 0, NULL, NULL, data);
+	if (n != 2)
+	{
+		g_warning ("Unexpected number (n=%d) of toplevel focus handlers found!\n", n);
+	}
+
+	if (mpriv->focus_connected) return;
+
+	GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
+	g_return_if_fail (child != NULL);
+
+	n = g_signal_handlers_block_matched (child,
+					     (GSignalMatchType) (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DATA),
+					     fiesid, 0, NULL, NULL, widget);
+	n += g_signal_handlers_block_matched (child,
+					      (GSignalMatchType) (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DATA),
+					      foesid, 0, NULL, NULL, widget);
+	if (n != 2)
+	{
+		g_warning ("Unexpected number (n=%d) of child focus handlers found!\n", n);
+	}
+
+	g_signal_connect_object (child, "focus-in-event",
+				 G_CALLBACK (child_focus_in_event_cb), widget,
+				 G_CONNECT_AFTER);
+	g_signal_connect_object (child, "focus-out-event",
+				 G_CALLBACK (child_focus_out_event_cb), widget,
+				 G_CONNECT_AFTER);
+
+	mpriv->focus_connected = TRUE;
+#endif /* GTKMOZEMBED_BROKEN_FOCUS */
 }
 
 static GObject *
@@ -251,6 +337,11 @@ mozilla_embed_class_init (MozillaEmbedCl
 	gtk_object_class->destroy = mozilla_embed_destroy;
 
 	widget_class->realize = mozilla_embed_realize;
+
+#ifdef GTKMOZEMBED_BROKEN_FOCUS
+	fiesid = g_signal_lookup ("focus-in-event", GTK_TYPE_WIDGET);
+	foesid = g_signal_lookup ("focus-out-event", GTK_TYPE_WIDGET);
+#endif /* GTKMOZEMBED_BROKEN_FOCUS */
 
 	g_type_class_add_private (object_class, sizeof(MozillaEmbedPrivate));
 }
Index: src/ephy-shell.c
===================================================================
RCS file: /cvs/gnome/epiphany/src/ephy-shell.c,v
retrieving revision 1.120.2.2
diff -p -u -u -p -r1.120.2.2 ephy-shell.c
--- src/ephy-shell.c	4 Jul 2005 12:14:56 -0000	1.120.2.2
+++ src/ephy-shell.c	13 Aug 2005 12:56:22 -0000
@@ -625,10 +625,25 @@ ephy_shell_new (void)
 	return EPHY_SHELL (g_object_new (EPHY_TYPE_SHELL, NULL));
 }
 
-static void
+static gboolean
+url_is_empty (const char *location)
+{
+	gboolean is_empty = FALSE;
+
+        if (location == NULL || location[0] == '\0' ||
+            strcmp (location, "about:blank") == 0)
+        {
+                is_empty = TRUE;
+        }
+
+        return is_empty;
+}
+
+static gboolean
 load_homepage (EphyEmbed *embed)
 {
 	char *home;
+	gboolean is_empty;
 
 	home = eel_gconf_get_string(CONF_GENERAL_HOMEPAGE);
 
@@ -639,9 +654,13 @@ load_homepage (EphyEmbed *embed)
 		home = g_strdup ("about:blank");
 	}
 
+	is_empty = url_is_empty (home);
+
 	ephy_embed_load_url (embed, home);
 
 	g_free (home);
+
+	return is_empty;
 }
 
 /**
@@ -675,6 +694,7 @@ ephy_shell_new_tab_full (EphyShell *shel
 	EphyEmbed *previous_embed = NULL;
 	GtkWidget *nb;
 	int position = -1;
+	gboolean is_empty = FALSE;
 	EphyToolbar *toolbar;
 
 	if (flags & EPHY_NEW_TAB_IN_NEW_WINDOW) in_new_window = TRUE;
@@ -733,13 +753,35 @@ ephy_shell_new_tab_full (EphyShell *shel
 	{
 		ephy_tab_set_location (tab, "", EPHY_TAB_ADDRESS_EXPIRE_NEXT);
 		ephy_toolbar_activate_location (toolbar);
-		load_homepage (embed);
+		is_empty = load_homepage (embed);
 	}
 	else if (flags & EPHY_NEW_TAB_OPEN_PAGE)
 	{
 		g_assert (url != NULL);
 		ephy_embed_load_url (embed, url);
+		is_empty = url_is_empty (url);
 	}
+
+        /* Make sure the initial focus is somewhere sensible and not, for
+         * example, on the reload button.
+         */
+        if (in_new_window || jump_to)
+        {
+                /* If the location entry is blank, focus that, except if the
+                 * page was a copy */
+                if (is_empty)
+                {
+                        /* empty page, focus location entry */
+			toolbar = EPHY_TOOLBAR (ephy_window_get_toolbar (window));
+			ephy_toolbar_activate_location (toolbar);
+                }
+                else if (embed != NULL) 
+                {
+			/* non-empty page, focus the page. but make sure the widget is realised first! */
+			gtk_widget_realize (GTK_WIDGET (embed));
+			ephy_embed_activate (embed);
+                }
+        }
 
 	return tab;
 }
Index: src/ephy-window.c
===================================================================
RCS file: /cvs/gnome/epiphany/src/ephy-window.c,v
retrieving revision 1.272.2.7
diff -p -u -u -p -r1.272.2.7 ephy-window.c
--- src/ephy-window.c	4 Jul 2005 12:14:56 -0000	1.272.2.7
+++ src/ephy-window.c	13 Aug 2005 12:56:24 -0000
@@ -2537,6 +2537,7 @@ ephy_window_open_link (EphyLink *link,
 		       EphyLinkFlags flags)
 {
 	EphyWindow *window = EPHY_WINDOW (link);
+	EphyWindowPrivate *priv = window->priv;
 	EphyTab *new_tab;
 
 	g_return_val_if_fail (address != NULL, NULL);
@@ -2578,7 +2579,15 @@ ephy_window_open_link (EphyLink *link,
 		embed = ephy_tab_get_embed (tab);
 
 		ephy_embed_load_url (embed, address);
-		ephy_embed_activate (embed);
+
+		if (address == NULL || address[0] == '\0' || strcmp (address, "about:blank") == 0)
+		{
+			ephy_toolbar_activate_location (priv->toolbar);
+		}
+		else
+		{
+			ephy_embed_activate (embed);
+		}
 
 		new_tab = tab;
 	}
