custom spin button format




I'm trying to create a GtkSpinButton with a customized
representation of the underlying numerical value (by attaching to
the "output" signal as described in the GTK manual) but have hit a
problem: I can't seem to get the spin button to go to the top of
its range.

Below is a minimal test program. I'd expect that, since the spin
button's range is set as 1 to 4, I ought to be able to display
"B", "C"  and "D" in turn by pressing the button's up-arrow. But
what actually happens is that the first up-press gives "B" and
then subsequent up-presses leave the displayed value at "B": I
can't make it show "C" or "D". Can anyone tell me what I'm doing
wrong? Thanks!

#include <gtk/gtk.h>

gchar *n_to_alpha (int n)
{
    if (n == 1) {
        return g_strdup("A");
    } else if (n == 2) {
        return g_strdup("B");
    } else if (n == 3) {
        return g_strdup("C");
    } else if (n == 4) {
        return g_strdup("D");
    } else {
        return g_strdup("X");
    }
}

gint alpha_output (GtkSpinButton *spin, gpointer p)
{
    gint n = gtk_spin_button_get_value_as_int(spin);
    gchar *s = n_to_alpha(n);

    printf("obs_button_output: %d -> '%s'\n", n, s);
    gtk_entry_set_text(GTK_ENTRY(spin), s);
    g_free(s);

    return TRUE; /* block the default output */
}

int main (int argc, char **argv)
{
    GtkWidget *w, *spinner;

    gtk_init(&argc, &argv);

    w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(w), 100, -1);
    gtk_container_set_border_width(GTK_CONTAINER(w), 5);
    g_signal_connect(G_OBJECT(w), "destroy",
                     gtk_main_quit, NULL);

    spinner = gtk_spin_button_new_with_range(1, 4, 1);
    gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(spinner), FALSE);
    g_signal_connect(G_OBJECT(spinner), "output",
                     G_CALLBACK(alpha_output), NULL);
    gtk_container_add(GTK_CONTAINER(w), spinner);

    gtk_widget_show_all(w);
    gtk_main();

    return 0;
}

-- 
Allin Cottrell
Department of Economics
Wake Forest University




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]