Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the colors of an arbitrary widget in GTK+?

Tags:

gtk

If I'm writing an application that wants to communicate some information through the use of color, how can I change the background and foreground colors of a given widget? I would like to know how to do this in glade if it's possible, as well as programmatically (to a computed color).

I want to know how to do this to a complex widget as well, for example, an HBox that contains a VBox that contains some Labels.

Ideally this would also include a solution solution that allows me to tint the widget's existing colors, and identify the average colors of any images in use by the theme, so that I can programmatically compensate for any color choices which might make text unreadable or otherwise clashing - but I would be happy if I could just turn a button red.

like image 563
Glyph Avatar asked Dec 18 '22 10:12

Glyph


1 Answers

Example program:

#include <gtk/gtk.h>

static void on_destroy(GtkWidget* widget, gpointer data)
{
        gtk_main_quit ();
}

int main (int argc, char* argv[])
{
        GtkWidget* window;
        GtkWidget* button;

        gtk_init(&argc, &argv);
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        g_signal_connect(G_OBJECT (window), "destroy",
                G_CALLBACK (on_destroy), NULL);
        button = gtk_button_new_with_label("Hello world!");
        GdkColor red = {0, 0xffff, 0x0000, 0x0000};
        GdkColor green = {0, 0x0000, 0xffff, 0x0000};
        GdkColor blue = {0, 0x0000, 0x0000, 0xffff};
        gtk_widget_modify_bg(button, GTK_STATE_NORMAL, &red);
        gtk_widget_modify_bg(button, GTK_STATE_PRELIGHT, &green);
        gtk_widget_modify_bg(button, GTK_STATE_ACTIVE, &blue);
        gtk_container_add(GTK_CONTAINER(window), button);
        gtk_widget_show_all(window);
        gtk_main();
        return 0;
}
like image 173
Tometzky Avatar answered Jan 13 '23 14:01

Tometzky