+static gboolean
+gtk_entry_selection_bubble_popup_cb (gpointer user_data)
+{
+ GtkEntry *entry = user_data;
+ GtkEntryPrivate *priv = entry->priv;
+ cairo_rectangle_int_t rect;
+ GtkAllocation allocation;
+ gint start_x, end_x;
+ gboolean has_selection;
+
+ has_selection = gtk_editable_get_selection_bounds (GTK_EDITABLE (entry),
+ NULL, NULL);
+ if (!has_selection && !priv->editable)
+ {
+ priv->selection_bubble_timeout_id = 0;
+ return FALSE;
+ }
+
+ if (priv->selection_bubble)
+ gtk_widget_destroy (priv->selection_bubble);
+
+ priv->selection_bubble = gtk_selection_window_new ();
+ g_signal_connect_swapped (priv->selection_bubble, "cut",
+ G_CALLBACK (gtk_entry_cut_clipboard),
+ entry);
+ g_signal_connect_swapped (priv->selection_bubble, "copy",
+ G_CALLBACK (gtk_entry_copy_clipboard),
+ entry);
+ g_signal_connect_swapped (priv->selection_bubble, "paste",
+ G_CALLBACK (gtk_entry_paste_clipboard),
+ entry);
+
+ gtk_selection_window_set_editable (GTK_SELECTION_WINDOW (priv->selection_bubble),
+ priv->editable);
+ gtk_selection_window_set_has_selection (GTK_SELECTION_WINDOW (priv->selection_bubble),
+ has_selection);
+
+ gtk_widget_get_allocation (GTK_WIDGET (entry), &allocation);
+
+ gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &start_x, NULL);
+
+ start_x -= priv->scroll_offset;
+ start_x = CLAMP (start_x, 0, gdk_window_get_width (priv->text_area));
+
+ rect.y = 0;
+ rect.height = gdk_window_get_height (priv->text_area);
+
+ if (has_selection)
+ {
+ end_x = gtk_entry_get_selection_bound_location (entry) - priv->scroll_offset;
+ end_x = CLAMP (end_x, 0, gdk_window_get_width (priv->text_area));
+
+ rect.x = MIN (start_x, end_x);
+ rect.width = MAX (start_x, end_x) - rect.x;
+ }
+ else
+ {
+ rect.x = start_x;
+ rect.width = 0;
+ }
+
+ gtk_bubble_window_popup (GTK_BUBBLE_WINDOW (priv->selection_bubble),
+ priv->text_area, &rect, GTK_POS_TOP);
+
+ priv->selection_bubble_timeout_id = 0;
+ return FALSE;
+}
+
+static void
+gtk_entry_selection_bubble_popup_unset (GtkEntry *entry)
+{
+ GtkEntryPrivate *priv;
+
+ priv = entry->priv;
+
+ if (priv->selection_bubble)
+ gtk_bubble_window_popdown (GTK_BUBBLE_WINDOW (priv->selection_bubble));
+
+ if (priv->selection_bubble_timeout_id)
+ {
+ g_source_remove (priv->selection_bubble_timeout_id);
+ priv->selection_bubble_timeout_id = 0;
+ }
+}
+
+static void
+gtk_entry_selection_bubble_popup_set (GtkEntry *entry)
+{
+ GtkEntryPrivate *priv;
+
+ priv = entry->priv;
+
+ if (priv->selection_bubble_timeout_id)
+ g_source_remove (priv->selection_bubble_timeout_id);
+
+ priv->selection_bubble_timeout_id =
+ gdk_threads_add_timeout_seconds (1, gtk_entry_selection_bubble_popup_cb,
+ entry);
+}
+