+static void
+gtk_text_view_get_selection_rect (GtkTextView *text_view,
+ cairo_rectangle_int_t *rect)
+{
+ cairo_rectangle_int_t rect_cursor, rect_bound;
+ GtkTextIter cursor, bound;
+ GtkTextBuffer *buffer;
+ gint x1, y1, x2, y2;
+
+ buffer = get_buffer (text_view);
+ gtk_text_buffer_get_iter_at_mark (buffer, &cursor,
+ gtk_text_buffer_get_insert (buffer));
+ gtk_text_buffer_get_iter_at_mark (buffer, &bound,
+ gtk_text_buffer_get_selection_bound (buffer));
+
+ gtk_text_view_get_cursor_locations (text_view, &cursor, &rect_cursor, NULL);
+ gtk_text_view_get_cursor_locations (text_view, &bound, &rect_bound, NULL);
+
+ x1 = MIN (rect_cursor.x, rect_bound.x);
+ x2 = MAX (rect_cursor.x, rect_bound.x);
+ y1 = MIN (rect_cursor.y, rect_bound.y);
+ y2 = MAX (rect_cursor.y + rect_cursor.height, rect_bound.y + rect_bound.height);
+
+ rect->x = x1;
+ rect->y = y1;
+ rect->width = x2 - x1;
+ rect->height = y2 - y1;
+}
+
+static gboolean
+gtk_text_view_selection_bubble_popup_cb (gpointer user_data)
+{
+ GtkTextView *text_view = user_data;
+ GtkTextViewPrivate *priv = text_view->priv;
+ cairo_rectangle_int_t rect;
+ gboolean has_selection;
+ GdkWindow *window;
+
+ has_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
+ NULL, NULL);
+ if (!priv->editable && !has_selection)
+ {
+ priv->selection_bubble_timeout_id = 0;
+ return FALSE;
+ }
+
+ if (priv->selection_bubble)
+ gtk_widget_destroy (priv->selection_bubble);
+
+ window = gtk_widget_get_window (GTK_WIDGET (text_view));
+ priv->selection_bubble = gtk_selection_window_new ();
+ 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);
+
+ g_signal_connect_swapped (priv->selection_bubble, "cut",
+ G_CALLBACK (gtk_text_view_cut_clipboard),
+ text_view);
+ g_signal_connect_swapped (priv->selection_bubble, "copy",
+ G_CALLBACK (gtk_text_view_copy_clipboard),
+ text_view);
+ g_signal_connect_swapped (priv->selection_bubble, "paste",
+ G_CALLBACK (gtk_text_view_paste_clipboard),
+ text_view);
+
+ gtk_text_view_get_selection_rect (text_view, &rect);
+ rect.x -= priv->xoffset;
+ rect.y -= priv->yoffset;
+ gtk_bubble_window_popup (GTK_BUBBLE_WINDOW (priv->selection_bubble),
+ window, &rect, GTK_POS_TOP);
+
+ priv->selection_bubble_timeout_id = 0;
+ return FALSE;
+}
+
+static void
+gtk_text_view_selection_bubble_popup_unset (GtkTextView *text_view)
+{
+ GtkTextViewPrivate *priv;
+
+ priv = text_view->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_text_view_selection_bubble_popup_set (GtkTextView *text_view)
+{
+ GtkTextViewPrivate *priv;
+
+ priv = text_view->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_text_view_selection_bubble_popup_cb,
+ text_view);
+}
+