Daniel
Daniel

Reputation: 13

gtk3 change button bg color

Today I start programming in GTK3, and I have a problem. I can't change the button background color, I want to change it after a click on the button. Here is my code:

void pushButton( GtkWidget* button )
{
    gtk_button_set_label( GTK_BUTTON( button ), "new_text" );
    GdkRGBA color = {.0, .0, .0, 1.0};
    gtk_widget_override_background_color ( button, GTK_STATE_NORMAL, &color );
}

gtk_button_set_label() is working well and changes the button label. But the color is still the same (should be black).

gtk_widget_override_background_color has been deprecated since version 3.16 and should not be used in newly-written code. If you wish to change the way a widget renders its background you should use a custom CSS style.

Upvotes: 1

Views: 5118

Answers (2)

DarkTrick
DarkTrick

Reputation: 3487

I'll take another approach. The question is to change the bg color for one single button. Therefore I would only change it for this one single button. (I'm not sure about best practices, though).

  • Add css
  • Don't forget to set background-image:none, otherwise color settings have no effect
  • Using a CSS class (in this case *, becuase the css is provided directly to the button (only). So everything (*) is only the button.

    void pushButton( GtkWidget* button )
    {
        // from your code
        gtk_button_set_label( GTK_BUTTON( button ), "new_text" );
    
        // You need an object to store css information: the CSS Provider
        GtkCssProvider * cssProvider = gtk_css_provider_new();
        // Load CSS into the object ("-1" says, that the css string is \0-terminated)
        gtk_css_provider_load_from_data(css, "* { background-image:none; background-color:red;}",-1,NULL); 
    
        // The "Style context" manages CSS providers (as there can be more of them)            
        GtkStyleContext * context = gtk_widget_get_style_context(button);   
        // So we want to add our CSS provider (that contains the CSS) to that "style manager".
        gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(css),GTK_STYLE_PROVIDER_PRIORITY_USER);
    
    
        // I'm not sure, if you need this. I took it from mame89's code
        g_object_unref (css);  
    }
    

Although: Be aware, that this code will create a new provider everytime the button was pushed. I guess it's better practice to store the provider somewhere and removing and adding it to the (style)context when needed.

Upvotes: 1

mame98
mame98

Reputation: 1341

You need to add custom CSS to the Button:

GdkDisplay *display;
GdkScreen *screen;
display = gdk_display_get_default ();
screen = gdk_display_get_default_screen (display);

GtkCssProvider *provider;

provider = gtk_css_provider_new ();

gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

gtk_css_provider_load_from_data(provider, CSS, -1, NULL);

g_object_unref (provider);

Where you replace CSS with some CSS to modify the Button

Upvotes: 1

Related Questions