Commit febc2985 authored by Cosimo Cecchi's avatar Cosimo Cecchi

tooltip: add support for opacity and rounded corners for tooltips

Rounded corners now will always work, using XShape in case we're not
running a composite manager.
Also, setting an RGBA visual (if available) on the tooltip toplevel
enables them to be transparent if the theme specifies so.
parent d987a01d
...@@ -157,6 +157,9 @@ static void gtk_tooltip_dispose (GObject *object); ...@@ -157,6 +157,9 @@ static void gtk_tooltip_dispose (GObject *object);
static gboolean gtk_tooltip_paint_window (GtkTooltip *tooltip, static gboolean gtk_tooltip_paint_window (GtkTooltip *tooltip,
cairo_t *cr); cairo_t *cr);
static void gtk_tooltip_realize_window (GtkTooltip *tooltip,
GtkWidget *widget);
static void maybe_update_shape (GtkTooltip *tooltip);
static void gtk_tooltip_window_hide (GtkWidget *widget, static void gtk_tooltip_window_hide (GtkWidget *widget,
gpointer user_data); gpointer user_data);
static void gtk_tooltip_display_closed (GdkDisplay *display, static void gtk_tooltip_display_closed (GdkDisplay *display,
...@@ -186,6 +189,8 @@ gtk_tooltip_init (GtkTooltip *tooltip) ...@@ -186,6 +189,8 @@ gtk_tooltip_init (GtkTooltip *tooltip)
GtkWidget *box; GtkWidget *box;
GtkWidget *image; GtkWidget *image;
GtkWidget *label; GtkWidget *label;
GdkScreen *screen;
GdkVisual *visual;
tooltip->timeout_id = 0; tooltip->timeout_id = 0;
tooltip->browse_mode_timeout_id = 0; tooltip->browse_mode_timeout_id = 0;
...@@ -202,6 +207,12 @@ gtk_tooltip_init (GtkTooltip *tooltip) ...@@ -202,6 +207,12 @@ gtk_tooltip_init (GtkTooltip *tooltip)
tooltip->last_window = NULL; tooltip->last_window = NULL;
window = g_object_ref (gtk_window_new (GTK_WINDOW_POPUP)); window = g_object_ref (gtk_window_new (GTK_WINDOW_POPUP));
screen = gtk_widget_get_screen (window);
visual = gdk_screen_get_rgba_visual (screen);
if (visual != NULL)
gtk_widget_set_visual (window, visual);
gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_TOOLTIP); gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_TOOLTIP);
gtk_widget_set_app_paintable (window, TRUE); gtk_widget_set_app_paintable (window, TRUE);
gtk_window_set_resizable (GTK_WINDOW (window), FALSE); gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
...@@ -214,6 +225,10 @@ gtk_tooltip_init (GtkTooltip *tooltip) ...@@ -214,6 +225,10 @@ gtk_tooltip_init (GtkTooltip *tooltip)
g_signal_connect_swapped (window, "draw", g_signal_connect_swapped (window, "draw",
G_CALLBACK (gtk_tooltip_paint_window), tooltip); G_CALLBACK (gtk_tooltip_paint_window), tooltip);
g_signal_connect_swapped (window, "realize",
G_CALLBACK (gtk_tooltip_realize_window), tooltip);
g_signal_connect_swapped (window, "composited-changed",
G_CALLBACK (maybe_update_shape), tooltip);
/* FIXME: don't hardcode the padding */ /* FIXME: don't hardcode the padding */
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6); box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
...@@ -569,21 +584,78 @@ gtk_tooltip_reset (GtkTooltip *tooltip) ...@@ -569,21 +584,78 @@ gtk_tooltip_reset (GtkTooltip *tooltip)
tooltip->custom_was_reset = FALSE; tooltip->custom_was_reset = FALSE;
} }
static gboolean static void
gtk_tooltip_paint_window (GtkTooltip *tooltip, paint_background_and_frame (GtkTooltip *tooltip,
cairo_t *cr) cairo_t *cr)
{ {
GtkStyleContext *context; GtkStyleContext *context;
gint width, height;
width = gtk_widget_get_allocated_width (tooltip->window);
height = gtk_widget_get_allocated_height (tooltip->window);
context = gtk_widget_get_style_context (tooltip->window); context = gtk_widget_get_style_context (tooltip->window);
gtk_render_background (context, cr, 0, 0, gtk_render_background (context, cr,
gtk_widget_get_allocated_width (tooltip->window), 0, 0, width, height);
gtk_widget_get_allocated_height (tooltip->window)); gtk_render_frame (context, cr,
gtk_render_frame (context, cr, 0, 0, 0, 0, width, height);
static void
maybe_update_shape (GtkTooltip *tooltip)
cairo_t *cr;
cairo_surface_t *surface;
cairo_region_t *region;
gint width, height;
/* fallback to XShape only for non-composited clients */
if (gtk_widget_is_composited (tooltip->window))
gtk_widget_shape_combine_region (tooltip->window, NULL);
surface = gdk_window_create_similar_surface (gtk_widget_get_window (tooltip->window),
gtk_widget_get_allocated_width (tooltip->window), gtk_widget_get_allocated_width (tooltip->window),
gtk_widget_get_allocated_height (tooltip->window)); gtk_widget_get_allocated_height (tooltip->window));
cr = cairo_create (surface);
paint_background_and_frame (tooltip, cr);
cairo_destroy (cr);
region = gdk_cairo_region_create_from_surface (surface);
gtk_widget_shape_combine_region (tooltip->window, region);
cairo_surface_destroy (surface);
cairo_region_destroy (region);
static void
gtk_tooltip_realize_window (GtkTooltip *tooltip,
GtkWidget *widget)
maybe_update_shape (tooltip);
static gboolean
gtk_tooltip_paint_window (GtkTooltip *tooltip,
cairo_t *cr)
if (gtk_widget_is_composited (tooltip->window))
/* clear any background */
cairo_save (cr);
cairo_set_source_rgba (cr, 0, 0, 0, 0);
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_paint (cr);
cairo_restore (cr);
maybe_update_shape (tooltip);
paint_background_and_frame (tooltip, cr);
return FALSE; return FALSE;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment