Adsense code (2021-10-05)

Sunday, January 5, 2014

GTK+ example: Switch

GTK+ example: Switch
GTK+ example: Switch

#include <gtk/gtk.h>

static void
switch_active (GtkSwitch *myswitch, GParamSpec *pspec, GtkLabel *label)
{
    if (gtk_switch_get_active (myswitch)){
        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 *label1;
    GtkWidget *label2;
    GtkWidget *switch1;
    GtkWidget *switch2;

    GObject *context_object;

    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 labels
    label1 = gtk_label_new("label1");
    gtk_box_pack_start(GTK_BOX(box), label1, FALSE, FALSE, 3);
    label2 = gtk_label_new("label2");
    gtk_box_pack_start(GTK_BOX(box), label2, FALSE, FALSE, 3);

    //Create Switchs
    //switch1 with default actived
    switch1 = gtk_switch_new ();
    gtk_switch_set_active (GTK_SWITCH (switch1), TRUE);
    gtk_box_pack_start(GTK_BOX(box), switch1, FALSE, FALSE, 3);
    //switch2 with default in-actived
    switch2 = gtk_switch_new ();
    gtk_switch_set_active (GTK_SWITCH (switch2), FALSE);
    gtk_box_pack_start(GTK_BOX(box), switch2, FALSE, FALSE, 3);

    g_signal_connect(GTK_SWITCH (switch1), "notify::active", 
        G_CALLBACK (switch_active), label1);
    g_signal_connect(GTK_SWITCH (switch2), "notify::active", 
        G_CALLBACK (switch_active), label2);

    //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;
}