--- emoticon-orig.c	2015-11-05 16:29:53.445414868 +0800
+++ emoticon.c	2015-11-05 16:30:11.362350812 +0800
@@ -718,9 +718,10 @@
 emoticon_tool_button_popup (EEmoticonToolButton *button)
 {
 	GtkToggleToolButton *tool_button;
+	GtkWidget *toplevel;
 	GdkWindow *window;
 	gboolean grab_status;
-	GdkDevice *device, *mouse, *keyboard;
+	GdkDevice *device, *mouse = NULL, *keyboard = NULL;
 	guint32 activate_time;
 
 	device = gtk_get_current_event_device ();
@@ -735,18 +736,25 @@
 	activate_time = gtk_get_current_event_time ();
 	if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD) {
 		keyboard = device;
-		mouse = gdk_device_get_associated_device (device);
 	} else {
-		keyboard = gdk_device_get_associated_device (device);
 		mouse = device;
 	}
 
 	/* Position the window over the button. */
 	emoticon_tool_button_reposition_window (button);
 
-	/* Show the pop-up. */
-	gtk_widget_show (button->priv->window);
+	toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
+	if (GTK_IS_WINDOW (toplevel)) {
+		gtk_window_group_add_window (
+			gtk_window_get_group (GTK_WINDOW (toplevel)),
+			GTK_WINDOW (button->priv->window));
+		gtk_window_set_transient_for (
+			GTK_WINDOW (button->priv->window),
+			GTK_WINDOW (toplevel));
+	}
+
 	gtk_widget_grab_focus (button->priv->window);
+	gtk_widget_realize (button->priv->window);
 
 	/* Activate the tool button. */
 	tool_button = GTK_TOGGLE_TOOL_BUTTON (button);
@@ -770,6 +778,9 @@
 			gdk_device_ungrab (keyboard, activate_time);
 	}
 
+	/* Show the pop-up. */
+	gtk_widget_show (button->priv->window);
+
 	if (grab_status) {
 		gtk_device_grab_add (button->priv->window, mouse, TRUE);
 		button->priv->grab_keyboard = keyboard;
@@ -933,7 +944,6 @@
 e_emoticon_tool_button_init (EEmoticonToolButton *button)
 {
 	EEmoticonChooser *chooser;
-	GtkWidget *toplevel;
 	GtkWidget *container;
 	GtkWidget *widget;
 	GtkWidget *window;
@@ -950,15 +960,6 @@
 		GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_COMBO);
 	button->priv->window = g_object_ref_sink (window);
 
-	toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
-	if (GTK_IS_WINDOW (toplevel)) {
-		gtk_window_group_add_window (
-			gtk_window_get_group (GTK_WINDOW (toplevel)),
-			GTK_WINDOW (window));
-		gtk_window_set_transient_for (
-			GTK_WINDOW (window), GTK_WINDOW (toplevel));
-	}
-
 	g_signal_connect_swapped (
 		window, "show",
 		G_CALLBACK (emoticon_tool_button_child_show_cb), button);
