+static void
+activate_bubble_cb (GtkWidget *item,
+ GtkEntry *entry)
+{
+ const gchar *signal = g_object_get_data (G_OBJECT (item), "gtk-signal");
+ g_signal_emit_by_name (entry, signal);
+ _gtk_bubble_window_popdown (GTK_BUBBLE_WINDOW (entry->priv->selection_bubble));
+}
+
+static void
+append_bubble_action (GtkEntry *entry,
+ GtkWidget *toolbar,
+ const gchar *stock_id,
+ const gchar *signal,
+ gboolean sensitive)
+{
+ GtkToolItem *item = gtk_tool_button_new_from_stock (stock_id);
+ g_object_set_data (G_OBJECT (item), I_("gtk-signal"), (char *)signal);
+ g_signal_connect (item, "clicked", G_CALLBACK (activate_bubble_cb), entry);
+ gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
+ gtk_widget_show (GTK_WIDGET (item));
+ gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
+}
+
+static void
+bubble_targets_received (GtkClipboard *clipboard,
+ GtkSelectionData *data,
+ 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;
+ gboolean has_clipboard;
+ DisplayMode mode;
+ GtkWidget *toolbar;
+
+ has_selection = gtk_editable_get_selection_bounds (GTK_EDITABLE (entry),
+ NULL, NULL);
+ if (!has_selection && !priv->editable)
+ {
+ priv->selection_bubble_timeout_id = 0;
+ return;
+ }
+
+ if (priv->selection_bubble)
+ gtk_widget_destroy (priv->selection_bubble);
+
+ priv->selection_bubble = _gtk_bubble_window_new ();
+ toolbar = GTK_WIDGET (gtk_toolbar_new ());
+ gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_TEXT);
+ gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), FALSE);
+ gtk_widget_show (toolbar);
+ gtk_container_add (GTK_CONTAINER (priv->selection_bubble), toolbar);
+
+ has_clipboard = gtk_selection_data_targets_include_text (data);
+ mode = gtk_entry_get_display_mode (entry);
+
+ append_bubble_action (entry, toolbar, GTK_STOCK_CUT, "cut-clipboard",
+ priv->editable && has_selection && mode == DISPLAY_NORMAL);
+
+ append_bubble_action (entry, toolbar, GTK_STOCK_COPY, "copy-clipboard",
+ has_selection && mode == DISPLAY_NORMAL);
+
+ append_bubble_action (entry, toolbar, GTK_STOCK_PASTE, "paste-clipboard",
+ priv->editable && has_clipboard);
+
+ if (priv->populate_all)
+ g_signal_emit (entry, signals[POPULATE_POPUP], 0, toolbar);
+
+ 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;
+}
+
+static gboolean
+gtk_entry_selection_bubble_popup_cb (gpointer user_data)
+{
+ GtkEntry *entry = user_data;
+
+ gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
+ gdk_atom_intern_static_string ("TARGETS"),
+ bubble_targets_received,
+ entry);
+ return G_SOURCE_REMOVE;
+}
+
+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 (1000, gtk_entry_selection_bubble_popup_cb, entry);
+}
+