GTK custom widget background is solid black

I'm trying to create my own widget in GTK 3. I noticed graphics problems that only appear with certain GTK themes, while all other themes work fine. I narrowed the problem down to code that draws the background by calling gtk_render_background(). For some themes, the background appears solid black, although this is not the default background color. Below is a simplified version of my drawing function.

static void gtk_databox_ruler_draw_ticks(GtkDataboxRuler *ruler)
{
    GtkWidget *widget;
    GtkStateFlags state;
    cairo_t *cr;
    GtkStyleContext *style_context;
    gint width, height;

    if (!gtk_widget_is_drawable(GTK_WIDGET(ruler))) {
        return;
    }

    widget = GTK_WIDGET(ruler);
    state = gtk_widget_get_state_flags(widget);
    style_context = gtk_widget_get_style_context(widget);

    gtk_style_context_save(style_context);
    gtk_style_context_add_class(style_context, GTK_STYLE_CLASS_DEFAULT);
    gtk_style_context_set_state(style_context, state);

    /* <test-code> */
    GdkRGBA test;
    gtk_style_context_get_background_color(style_context, gtk_widget_get_state_flags(widget), &test);
    /* </test-code> */

    width = gtk_widget_get_allocated_width(widget);
    height = gtk_widget_get_allocated_height(widget);

    cr = cairo_create(ruler->priv->backing_surface);

    gtk_render_background(style_context, cr, 0, 0, width, height);

    gtk_style_context_restore(style_context);
    cairo_destroy(cr);
}

I added some test code to query the background color and set a breakpoint in gdb:

When using the Ubuntu Ambiance theme:

(gdb) print test
$1: test = {red = 0.94901960784313721, green = 0.94509803921568625, 
  blue = 0.94117647058823528, alpha = 1}

When using the Ubuntu HighContrast theme:

(gdb) print test
$1: test = {red = 0, green = 0, blue = 0, alpha = 0}

Now I am wondering if I am using the new GtkStyleContext incorrectly or if the theme is broken. How can I narrow down the source of the problem?

, - GtkStyleContext. API .

+5
3

, , , () GTK3, , .. .

, , GTK " ", .

GTK3:


* {
  color: @fg_color;
  border-color: shade (@bg_color, 0.6);
  padding: 2px;
  -GtkWindow-resize-grip-width: 0;
  -GtkWindow-resize-grip-height: 0;
  -GtkWindow-decoration-button-layout: 'icon:minimize,maximize,close';
}

GtkWindow, .button, .slider {
  background-color: @bg_color;
}

Adwaita:


* {
    /* inherit the color from parent by default */
    color: inherit;
    background-color: @theme_bg_color;
}

, , , () . :

gtk_style_context_add_class(style_context, GTK_STYLE_CLASS_BUTTON); 
+4

Ubuntu GTK3 ?

, gtk_style_context_set_junction_sides().

Benjamin Otte, GTK. GTK, GTK + 3 Styling.

+2

All Articles