Saturday, January 11, 2014

GTK+ example: SpinButton

SpinButton
SpinButton displayed on Ubuntu
#include <gtk/gtk.h>

static gboolean
spinButton_changed (GtkSpinButton *spin, gpointer data)
{
    gchar *text;
    int value;
    GtkLabel *label;

    label = data;
    value = gtk_spin_button_get_value_as_int (spin);
    text = g_strdup_printf ("%02d", value);
    gtk_label_set_text (label, text);
    g_free (text);

    return TRUE;
}

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

    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 SpinButton
    spinButton = gtk_spin_button_new_with_range  (
        0,      //gdouble min
        10,     //gdouble max
        2);     //gdouble step
    gtk_widget_set_hexpand (spinButton, TRUE);
    g_signal_connect(G_OBJECT(spinButton), "value-changed", 
        G_CALLBACK(spinButton_changed), label);

    gtk_box_pack_start(GTK_BOX(box), spinButton, 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);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}



Remark: wrong icon displayed on Lubuntu:
SpinButton on Lubuntu
SpinButton with wrong icon displayed on Lubuntu