6

I am trying to create a custom widget in GTK 3. I noticed drawing problems which only appear with certain GTK themes, while all other themes work great. I narrowed down the problem to the code that draws the background by calling gtk_render_background(). For some themes, the background is rendered in solid black, although this is not the themes's default background color. Below is a simplified version of my draw 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 Ubuntu's Ambiance theme:

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

When using Ubuntu's HighContrast theme:

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

I now wonder if I use the new GtkStyleContext in a wrong way, or whether or the theme is broken. How can I narrow down the source of the problem?

I'd also appreciate it if someone can point my to a good introduction to GtkStyleContext. The official API documentation is not so helpful for understanding the fundamental concept.

3
  • I don't know where are you drawing, but you could only draw a widget in its draw virtual method. There's so many things in your code which will cause unexpected behavior, that anything could be doing that.
    – erick2red
    Commented Sep 4, 2012 at 12:28
  • The gtk_databox_ruler_draw_ticks function shown above is called from the widget's draw function (GtkWidgetClass->draw). Can you point out things in the above code that need to be fixed?
    – dasup
    Commented Sep 13, 2012 at 14:02
  • Dunno if it will help, but you could look here
    – erick2red
    Commented Sep 13, 2012 at 19:51

3 Answers 3

5

After more than one year has passed, I had to look into this problem again because it also occurs when using the GTK3 default (built-in) style, i.e. when my program is used on a system that has no themes etc. installed.

It looks like the source of the problem is that some GTK themes define a background color for the "default case" and others do not.

The GTK3 default theme:

…
* {
  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;
}
…

The Adwaita theme:

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

To get the background drawn, I simply have to select a widget class that has a background color defined across all (most) themes. I use the button class for that:

gtk_style_context_add_class(style_context, GTK_STYLE_CLASS_BUTTON); 
2

What version of Ubuntu and GTK3 are you using?

Perhaps you need to call gtk_style_context_set_junction_sides().

You may also be interested in this info from Benjamin Otte, one of the developers of GTK. How GTK styling works, also available GTK+3 Styling.

1
  • I'm using Ubuntu 12.04, libgtk is in version 3.4.2-0ubuntu0. I added gtk_style_context_set_junction_sides with GTK_JUNCTION_BOTTOM set after gtk_style_context_add_class but that did not fix it. Thanks for the link, but I think it focuses more on the CSS side while I need more info about the GtkStyleContext side (i.e. rendering, what does save/restore do, how do the classes work).
    – dasup
    Commented Sep 3, 2012 at 23:58
1

Part of the catch here may be that increasingly, there isn't necessarily a single background color any more.

Per the documentation of https://developer.gnome.org/gtk3/stable/GtkStyleContext.html#gtk-style-context-get-background-color:

"This function is far less useful than it seems, and it should not be used in newly written code. CSS has no concept of "background color", as a background can be an image, or a gradient, or any other pattern including solid colors."

1
  • Thanks for pointing this out! Please note that in the above code, I used gtk_style_context_get_background_color just for testing/debugging purposes. The real code calls gtk_render_background which I assume to deal with all kinds of backround definitions. However, calling gtk_render_background results in a solid black backround with certain themes.
    – dasup
    Commented Dec 1, 2015 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.