Monday, January 6, 2014

GTK+ example: Button

GTK+ example: Button
GTK+ example: Button

#include <gtk/gtk.h>

static void
button_toggled (GtkWidget *button, gpointer data)
{
    GtkLabel *label = data;

    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(button)))
        gtk_label_set_text (label, "Active");
    else {
        gtk_label_set_text (label, "In-Active");
    }
}

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

    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);

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

    //Create Button
    button = gtk_toggle_button_new_with_label ("Button");
    gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 3);

    g_signal_connect (GTK_TOGGLE_BUTTON (button), "toggled", 
                    G_CALLBACK (button_toggled), label);

    //Connects GCallback function gtk_main_quit to "destroy" signal for "window"
    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;
}