Tuesday, December 31, 2013

GTK+ example: RadioButton

GTK+ example: RadioButton
GTK+ example: RadioButton

#include <gtk/gtk.h>

int main( int argc, char *argv[])
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *radioButton1;
    GtkWidget *radioButton2;
    GtkWidget *radioButton3;
    GtkWidget *radioButton4;
    GtkWidget *radioButton5;

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

    //new RadioButton1, 2, 3 in same group
    radioButton1 = gtk_radio_button_new_with_label (
        NULL, "Radio Button 1");
    radioButton2 = gtk_radio_button_new_with_label_from_widget (
        GTK_RADIO_BUTTON (radioButton1), "Radio Button 2");
    radioButton3 = gtk_radio_button_new_with_label_from_widget (
        GTK_RADIO_BUTTON (radioButton1), "Radio Button 3");

    //new RadioButton4, 5 in another group
    radioButton5 = gtk_radio_button_new_with_label (
        NULL, "Another Group Button 5");
    radioButton4 = gtk_radio_button_new_with_label_from_widget (
        GTK_RADIO_BUTTON (radioButton5), "Another Group Button 4");

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_box_pack_start(GTK_BOX(box), radioButton1, FALSE, FALSE, 3);
    gtk_box_pack_start(GTK_BOX(box), radioButton2, FALSE, FALSE, 3);
    gtk_box_pack_start(GTK_BOX(box), radioButton3, FALSE, FALSE, 3);
    gtk_box_pack_start(GTK_BOX(box), radioButton4, FALSE, FALSE, 3);
    gtk_box_pack_start(GTK_BOX(box), radioButton5, FALSE, FALSE, 3);

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

    return 0;

Next: Handle "toggled" signal of RadioButton and determine the active selected RadioButton in group