Adsense code (2021-10-05)

Wednesday, January 22, 2014

GTK+ example: Scale

GTK+ example: Scale
GTK+ example: Scale

#include <gtk/gtk.h>

static gboolean
scale_changed (GtkRange *range, gpointer data)
{
    gchar *text;
    gdouble value;
    GtkLabel *label;

    label = data;
    value = gtk_range_get_value (range);
    text = g_strdup_printf ("%.1f", 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;
    GtkWidget *scale;

    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 Scale
    scale = gtk_scale_new_with_range (
        GTK_ORIENTATION_HORIZONTAL, 
        0,      //gdouble min
        10,     //gdouble max
        0.5);     //gdouble step
    gtk_widget_set_hexpand (scale, TRUE);
    g_signal_connect(G_OBJECT(scale), "value-changed", 
        G_CALLBACK(scale_changed), label);

    gtk_box_pack_start(GTK_BOX(box), scale, 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;
}