Re: custom spin button format



Hello.

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!

If you do anything fancy inside your GtkSpinButton::output signal
handler (this basically means that you set spin button to hold
non-numeric data like in your case), you need to also provide handler
for GtkSpinButton::input signal. Input signal is not properly
documented, but it's function is to take the contents of the spin
button and convert it to gdouble number.

I updated your sample code to show you how this fits together. API
docs will be updated soonish (I do have a patch, but it must wait
until docs are migrated from template files to sources).

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

static gboolean
alpha_input (GtkSpinButton *spin,
             gdouble       *new_val,
             gpointer       p)
{
  gchar current;

  current = (gtk_entry_get_text (GTK_ENTRY (spin)))[0];
  *new_val = current - 'A' + 1;

  return TRUE;
}

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), "input",
                    G_CALLBACK(alpha_input), NULL);
   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;
}
---------

Tadej

-- 
Tadej BorovÅak
tadeboro.blogspot.com
tadeboro gmail com
tadej borovsak gmail com



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