Monday, January 27, 2014

GtkColorChooser example

GtkColorChooser
GtkColorChooser

#include <gtk/gtk.h>

static void 
chooser_color_activated(GtkColorChooser *chooser, 
 GdkRGBA *color, gpointer user_data)
{
 GtkLabel *label = user_data;
 gtk_label_set_text (label, gdk_rgba_to_string(color));
}

int main( int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *label;
    GtkWidget *chooser;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 250);
    gtk_window_set_title(GTK_WINDOW(window), "Linux-buddy.blogspot.com");

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);

 label = gtk_label_new("label");
    gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 3);

 //Create GtkColorChooser
    chooser = gtk_color_chooser_widget_new();
    gtk_box_pack_start(GTK_BOX(box), chooser, FALSE, FALSE, 3);
    g_signal_connect (GTK_COLOR_CHOOSER(chooser),
                    "color-activated", 
                    G_CALLBACK (chooser_color_activated), 
                    label);

    g_signal_connect(G_OBJECT(window), "destroy", 
        G_CALLBACK(gtk_main_quit), NULL);

    gtk_container_add(GTK_CONTAINER(window), box);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}


Note: double click the color buttons to activate color change.