Re: An indicator widget



Grace Yeung wrote:

What GTK widget and function(s) should I use to create a button-like
indicator that changes from one color (ex. green) to another (ex. red)
to indicate an application state?  Thanks.
Look at attached sample.

   Olexiy
#include <gtk/gtk.h>

static void change_image_colour(GtkWidget *button, GtkWidget *image)
{
GdkGC           *gc;
GdkColor        color;
GdkPixmap       *pixmap;

        pixmap = gdk_pixmap_new(
                                button->window,
                                24,16,
                                -1
        );

        gc = gdk_gc_new(pixmap);
        color.red = rand()&0x7fff + 0x8000;
        color.green = rand()&0x7fff + 0x8000;
        color.blue = rand()&0x7fff + 0x8000;

        gdk_gc_set_rgb_fg_color(gc, &color);

        gdk_draw_rectangle(pixmap, gc, TRUE, 0,0,-1,-1);

        g_object_unref(gc);

        gtk_image_set_from_pixmap(GTK_IMAGE(image), pixmap,NULL);

        g_object_unref(pixmap);
}

int main(int argc, char **argv)
{
GtkWidget *window;
GtkWidget *image;
GtkWidget *hbox;
GtkWidget *label;
GtkWidget *button;

        gtk_init(&argc, &argv);

        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_container_set_border_width(GTK_CONTAINER(window), 4);
        g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), 0);

        button = gtk_button_new();
        gtk_container_add(GTK_CONTAINER(window), button);

        hbox = gtk_hbox_new(FALSE, 4);
        gtk_container_add(GTK_CONTAINER(button), hbox);

        image = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_MENU);
        gtk_box_pack_start(GTK_BOX(hbox), image, FALSE,FALSE, 0);

        g_signal_connect(button, "clicked", G_CALLBACK(change_image_colour), image);

        gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new("Press me"), FALSE,FALSE, 0);

        gtk_widget_show_all(window);

        gtk_main();
        return 0;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]