libgtk/0001-New-tooltip-style.patch

328 lines
10 KiB
Diff

diff -up gtk+-2.91.3/gtk/gtktooltip.c.fresh-tooltips gtk+-2.91.3/gtk/gtktooltip.c
--- gtk+-2.91.3/gtk/gtktooltip.c.fresh-tooltips 2010-10-31 14:28:19.000000000 -0400
+++ gtk+-2.91.3/gtk/gtktooltip.c 2010-11-01 14:12:41.027410023 -0400
@@ -35,6 +35,9 @@
#include "gtkalignment.h"
#include "gtksizerequest.h"
+#ifdef GDK_WINDOWING_X11
+#include "gdk/x11/gdkx.h"
+#endif
/**
* SECTION:gtktooltip
@@ -165,6 +168,7 @@ static void gtk_tooltip_display_cl
GtkTooltip *tooltip);
static void gtk_tooltip_set_last_window (GtkTooltip *tooltip,
GdkWindow *window);
+static void update_shape (GtkTooltip *tooltip);
G_DEFINE_TYPE (GtkTooltip, gtk_tooltip, G_TYPE_OBJECT);
@@ -180,6 +184,40 @@ gtk_tooltip_class_init (GtkTooltipClass
}
static void
+on_composited_changed (GtkWidget *window,
+ GtkTooltip *tooltip)
+{
+ update_shape (tooltip);
+}
+
+static void
+on_screen_changed (GtkWidget *window,
+ GdkScreen *previous,
+ GtkTooltip *tooltip)
+{
+ GdkScreen *screen;
+ GdkVisual *visual;
+
+ screen = gtk_widget_get_screen (window);
+
+
+ visual = NULL;
+ if (gdk_screen_is_composited (screen))
+ visual = gdk_screen_get_rgba_visual (screen);
+ if (visual == NULL)
+ visual = gdk_screen_get_system_visual (screen);
+
+ gtk_widget_set_visual (window, visual);
+}
+
+static void
+on_realized (GtkWidget *window,
+ GtkTooltip *tooltip)
+{
+ update_shape (tooltip);
+}
+
+static void
gtk_tooltip_init (GtkTooltip *tooltip)
{
GtkStyle *style;
@@ -199,8 +237,12 @@ gtk_tooltip_init (GtkTooltip *tooltip)
tooltip->last_window = NULL;
tooltip->window = g_object_ref (gtk_window_new (GTK_WINDOW_POPUP));
+
+ on_screen_changed (tooltip->window, NULL, tooltip);
+
gtk_window_set_type_hint (GTK_WINDOW (tooltip->window),
GDK_WINDOW_TYPE_HINT_TOOLTIP);
+
gtk_widget_set_app_paintable (tooltip->window, TRUE);
gtk_window_set_resizable (GTK_WINDOW (tooltip->window), FALSE);
gtk_widget_set_name (tooltip->window, "gtk-tooltip");
@@ -234,6 +276,13 @@ gtk_tooltip_init (GtkTooltip *tooltip)
gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->label,
FALSE, FALSE, 0);
+ g_signal_connect (tooltip->window, "composited-changed",
+ G_CALLBACK (on_composited_changed), tooltip);
+ g_signal_connect (tooltip->window, "screen-changed",
+ G_CALLBACK (on_screen_changed), tooltip);
+ g_signal_connect (tooltip->window, "realize",
+ G_CALLBACK (on_realized), tooltip);
+
tooltip->custom_widget = NULL;
}
@@ -583,19 +632,209 @@ gtk_tooltip_window_style_set (GtkTooltip
gtk_widget_queue_draw (tooltip->window);
}
+static void
+draw_round_rect (cairo_t *cr,
+ gdouble aspect,
+ gdouble x,
+ gdouble y,
+ gdouble corner_radius,
+ gdouble width,
+ gdouble height)
+{
+ gdouble radius = corner_radius / aspect;
+
+ cairo_move_to (cr, x + radius, y);
+
+ /* top-right, left of the corner */
+ cairo_line_to (cr, x + width - radius, y);
+
+ /* top-right, below the corner */
+ cairo_arc (cr,
+ x + width - radius, y + radius, radius,
+ -90.0f * G_PI / 180.0f, 0.0f * G_PI / 180.0f);
+
+ /* bottom-right, above the corner */
+ cairo_line_to (cr, x + width, y + height - radius);
+
+ /* bottom-right, left of the corner */
+ cairo_arc (cr,
+ x + width - radius, y + height - radius, radius,
+ 0.0f * G_PI / 180.0f, 90.0f * G_PI / 180.0f);
+
+ /* bottom-left, right of the corner */
+ cairo_line_to (cr, x + radius, y + height);
+
+ /* bottom-left, above the corner */
+ cairo_arc (cr,
+ x + radius, y + height - radius, radius,
+ 90.0f * G_PI / 180.0f, 180.0f * G_PI / 180.0f);
+
+ /* top-left, below the corner */
+ cairo_line_to (cr, x, y + radius);
+
+ /* top-left, right of the corner */
+ cairo_arc (cr,
+ x + radius, y + radius, radius,
+ 180.0f * G_PI / 180.0f, 270.0f * G_PI / 180.0f);
+
+ cairo_close_path (cr);
+}
+
+static void
+fill_background (GtkWidget *widget,
+ cairo_t *cr)
+{
+ GdkColor color;
+ gdouble r, g, b;
+ gint radius;
+ gdouble background_alpha;
+ GtkStyle *style;
+ GtkAllocation allocation;
+
+ if (gdk_screen_is_composited (gtk_widget_get_screen (widget)))
+ background_alpha = 0.90;
+ else
+ background_alpha = 1.0;
+
+ style = gtk_widget_get_style (widget);
+
+ radius = MIN (style->xthickness, style->ythickness);
+ radius = MAX (radius, 1);
+
+ cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
+ cairo_paint (cr);
+ cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
+
+ gtk_widget_get_allocation (widget, &allocation);
+
+ draw_round_rect (cr,
+ 1.0, 0.5, 0.5, radius,
+ allocation.width - 1,
+ allocation.height - 1);
+
+ color = style->bg [GTK_STATE_NORMAL];
+ r = (float)color.red / 65535.0;
+ g = (float)color.green / 65535.0;
+ b = (float)color.blue / 65535.0;
+ cairo_set_source_rgba (cr, r, g, b, background_alpha);
+ cairo_fill_preserve (cr);
+
+ color = style->bg [GTK_STATE_SELECTED];
+ r = (float) color.red / 65535.0;
+ g = (float) color.green / 65535.0;
+ b = (float) color.blue / 65535.0;
+
+ cairo_set_source_rgba (cr, r, g, b, background_alpha);
+ cairo_set_line_width (cr, 1.0);
+ cairo_stroke (cr);
+}
+
+static void
+update_shape (GtkTooltip *tooltip)
+{
+ GdkScreen *screen;
+ cairo_t *cr;
+ cairo_surface_t *surface;
+ cairo_region_t *region;
+ gint width, height;
+ gboolean new_style;
+ gint radius;
+ GtkStyle *style;
+
+ gtk_widget_style_get (tooltip->window, "new-tooltip-style", &new_style, NULL);
+
+ if (!new_style)
+ {
+ gtk_widget_shape_combine_region (tooltip->window, NULL);
+ return;
+ }
+
+ screen = gtk_widget_get_screen (tooltip->window);
+ style = gtk_widget_get_style (tooltip->window);
+
+ gtk_window_get_size (GTK_WINDOW (tooltip->window), &width, &height);
+
+ if (gdk_screen_is_composited (screen))
+ {
+ const char *wm;
+
+ gtk_widget_shape_combine_region (tooltip->window, NULL);
+#ifdef GDK_WINDOWING_X11
+ /* This is a hack to keep the Metacity compositor from slapping a
+ * non-shaped shadow around the shaped tooltip
+ */
+ if (!gtk_widget_get_mapped (tooltip->window))
+ {
+ wm = gdk_x11_screen_get_window_manager_name (screen);
+ if (g_strcmp0 (wm, "Metacity") == 0)
+ gtk_window_set_type_hint (GTK_WINDOW (tooltip->window),
+ GDK_WINDOW_TYPE_HINT_DND);
+ }
+#endif
+ return;
+ }
+
+ radius = MIN (style->xthickness, style->ythickness);
+ radius = MAX (radius, 1);
+ surface = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height);
+ cr = cairo_create (surface);
+ if (cairo_status (cr) == CAIRO_STATUS_SUCCESS)
+ {
+ cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
+ cairo_paint (cr);
+
+ cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
+ cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
+ draw_round_rect (cr, 1.0, 0, 0, radius + 1, width, height);
+ cairo_fill (cr);
+ region = gdk_cairo_region_create_from_surface (surface);
+ gtk_widget_shape_combine_region (tooltip->window, region);
+ cairo_region_destroy (region);
+ }
+ cairo_destroy (cr);
+ cairo_surface_destroy (surface);
+}
+
static gboolean
gtk_tooltip_paint_window (GtkTooltip *tooltip,
- cairo_t *cr)
+ cairo_t *context)
{
- gtk_paint_flat_box (gtk_widget_get_style (tooltip->window),
- cr,
- GTK_STATE_NORMAL,
- GTK_SHADOW_OUT,
- tooltip->window,
- "tooltip",
- 0, 0,
- gtk_widget_get_allocated_width (tooltip->window),
- gtk_widget_get_allocated_height (tooltip->window));
+ gboolean new_style;
+
+ gtk_widget_style_get (tooltip->window, "new-tooltip-style", &new_style, NULL);
+
+ if (new_style)
+ {
+ cairo_surface_t *surface;
+ cairo_t *cr;
+
+ cairo_set_operator (context, CAIRO_OPERATOR_SOURCE);
+ surface = cairo_surface_create_similar (cairo_get_target (context),
+ CAIRO_CONTENT_COLOR_ALPHA,
+ gtk_widget_get_allocated_width (tooltip->window),
+ gtk_widget_get_allocated_height (tooltip->window));
+ cr = cairo_create (surface);
+ fill_background (tooltip->window, cr);
+ cairo_destroy (cr);
+
+ cairo_set_source_surface (context, surface, 0, 0);
+ cairo_paint (context);
+ cairo_surface_destroy (surface);
+
+ update_shape (tooltip);
+ }
+ else
+ {
+ gtk_paint_flat_box (gtk_widget_get_style (tooltip->window),
+ context,
+ GTK_STATE_NORMAL,
+ GTK_SHADOW_OUT,
+ tooltip->window,
+ "tooltip",
+ 0, 0,
+ gtk_widget_get_allocated_width (tooltip->window),
+ gtk_widget_get_allocated_height (tooltip->window));
+ }
return FALSE;
}
diff -up gtk+-2.91.3/gtk/gtkwidget.c.fresh-tooltips gtk+-2.91.3/gtk/gtkwidget.c
--- gtk+-2.91.3/gtk/gtkwidget.c.fresh-tooltips 2010-11-01 09:17:21.000000000 -0400
+++ gtk+-2.91.3/gtk/gtkwidget.c 2010-11-01 14:11:38.921410003 -0400
@@ -3190,6 +3190,14 @@ gtk_widget_class_init (GtkWidgetClass *k
1, G_MAXINT, 16,
GTK_PARAM_READABLE));
+ gtk_widget_class_install_style_property (klass,
+ g_param_spec_boolean ("new-tooltip-style",
+ NULL,
+ NULL,
+ FALSE,
+ GTK_PARAM_READABLE));
+
+
g_type_class_add_private (klass, sizeof (GtkWidgetPrivate));
}