Spin Button

Gamma/GTK: Using Gamma™ with GTK, Version 0.9
Prev | Appendix A. Example Code | Next

# A.15. Spin Button

    function make_adj (low, up, step, pg_inc, pg_size)
    {
      local adj = new(GtkAdjustment);
      adj.lower = low;
      adj.upper = up;
      adj.step_increment = step;
      adj.page_increment = pg_inc;
      adj.page_size = pg_size;
      adj;
    }

    function change_digits(spinner, spin)
    {
      spinner.digits = spin.get_value_as_int();
    }

    function get_value(spinner, data, label)
    {
      if (data == 1)
        label.set_text(format("%d", spinner.get_value_as_int()));
      else
        label.set_text(format("%f", spinner.get_value_as_float()));
    }

    function create_spins ()
    {
      local frame, hbox, main_vbox, vbox, vbox2, spinner2, spinner, button,
      label, val_label, adj;

      win_spins = new (GtkWindow);
      win_spins.signal ("destroy", #win_spins = nil);
      win_spins.title = "GtkSpinButton";
      win_spins.border_width = 0;

      main_vbox = new(GtkVBox);
      main_vbox.spacing = 5;
      main_vbox.border_width = 10;
      win_spins.add(main_vbox);

      frame = new(GtkFrame);
      frame.label = "Not accelerated";
      main_vbox.pack_start(frame, TRUE, TRUE, 0);

      vbox = new(GtkVBox);
      vbox.border_width = 5;
      frame.add(vbox);

      /* Day, month, year spinners */

      hbox = new(GtkHBox);
      vbox.pack_start(hbox, TRUE, TRUE, 5);

      vbox2 = new(GtkVBox);
      hbox.pack_start(vbox2, TRUE, TRUE, 5);

      label = new(GtkLabel);
      label.set_text("Day :");
      label.set_alignment(0, 0.5);
      vbox2.pack_start(label, FALSE, TRUE, 0);

      adj = make_adj(1.0, 31.0, 1.0, 5.0, 0.0);
      spinner = new(GtkSpinButton);
      spinner.configure(adj, 0, 0);
      spinner.set_value(1);
      spinner.set_wrap(TRUE);
      spinner.set_shadow_type(GTK_SHADOW_OUT);
      vbox2.pack_start(spinner, FALSE, TRUE, 0);

      vbox2 = new(GtkVBox);
      hbox.pack_start(vbox2, TRUE, TRUE, 5);

      label = new(GtkLabel);
      label.set_text("Month :");
      label.set_alignment(0, 0.5);
      vbox2.pack_start(label, FALSE, TRUE, 0);

      adj = make_adj(1.0, 12.0, 1.0, 5.0, 0.0);
      spinner = new(GtkSpinButton);
      spinner.configure(adj, 0, 0);
      spinner.set_value(1);
      spinner.set_wrap(TRUE);
      spinner.set_shadow_type(GTK_SHADOW_ETCHED_IN);
      vbox2.pack_start(spinner, FALSE, TRUE, 0);

      vbox2 = new(GtkVBox);
      hbox.pack_start(vbox2, TRUE, TRUE, 5);

      label = new(GtkLabel);
      label.set_text("Year :");
      label.set_alignment(0, 0.5);
      vbox2.pack_start(label, FALSE, TRUE, 0);

      adj = make_adj(0.0, 2100.0, 1.0, 100.0, 0.0);
      spinner = new(GtkSpinButton);
      spinner.configure(adj, 0, 0);
      spinner.set_value(1998);
      spinner.set_wrap(TRUE);
      spinner.set_shadow_type(GTK_SHADOW_IN);
      spinner.set_usize(55, 0);
      vbox2.pack_start(spinner, FALSE, TRUE, 0);

      frame = new(GtkFrame);
      frame.label = "Accelerated";
      main_vbox.pack_start(frame, TRUE, TRUE, 0);

      vbox = new(GtkVBox);
      vbox.border_width = 5;
      frame.add(vbox);

      hbox = new(GtkHBox);
      vbox.pack_start(hbox, FALSE, TRUE, 5);

      vbox2 = new(GtkVBox);
      hbox.pack_start(vbox2, TRUE, TRUE, 5);

      label = new(GtkLabel);
      label.set_text("Value :");
      label.set_alignment(0, 0.5);
      vbox2.pack_start(label, FALSE, TRUE, 0);

      adj = make_adj(-100000.0, 10000.0, 0.5, 100.0, 0.0);
      spinner1 = new(GtkSpinButton);
      spinner1.configure(adj, 1.0, 2);
      spinner1.set_value(0.0);
      spinner1.set_wrap(TRUE);
      spinner1.set_usize(100, 0);
      vbox2.pack_start(spinner1, FALSE, TRUE, 0);

      vbox2 = new(GtkVBox);
      hbox.pack_start(vbox2, TRUE, TRUE, 5);

      label = new(GtkLabel);
      label.set_text("Digits :");
      label.set_alignment(0, 0.5);
      vbox2.pack_start(label, FALSE, TRUE, 0);

      adj = make_adj(1, 5, 1, 1, 0);
      spinner2 = new(GtkSpinButton);
      spinner2.configure(adj, 0.0, 0);
      spinner2.set_value(2);
      spinner2.set_wrap(TRUE);
      adj.signal("value_changed", `change_digits(@spinner1, @spinner2));
      vbox2.pack_start(spinner2, FALSE, TRUE, 0);

      hbox = new(GtkHBox);
      vbox.pack_start(hbox, FALSE, TRUE, 5);

      button = new(GtkCheckButton);
      button.label = "Snap to 0.5-ticks";
      button.signal("clicked",
    		`(@spinner1).set_snap_to_ticks((@button).get_active()));
      vbox.pack_start(button, TRUE, TRUE, 0);
      button.set_active(TRUE);

      button = new(GtkCheckButton);
      button.label = "Numeric only input mode";
      button.signal("clicked",
    		`(@spinner1).set_numeric((@button).get_active()));
      vbox.pack_start(button, TRUE, TRUE, 0);
      button.set_active(TRUE);

      val_label = new(GtkLabel);

      hbox = new(GtkHBox);
      vbox.pack_start(hbox, FALSE, TRUE, 5);

      button = new(GtkButton);
      button.label = "Value as Int";
      button.user_data = val_label;
      button.signal("clicked", `get_value(@spinner1, 1, @val_label));
      hbox.pack_start(button, TRUE, TRUE, 5);

      button = new(GtkButton);
      button.label = "Value as Float";
      button.user_data = val_label;
      button.signal("clicked", `get_value(@spinner1, 2, @val_label));
      hbox.pack_start(button, TRUE, TRUE, 5);

      vbox.pack_start(val_label, TRUE, TRUE, 0);
      val_label.set_text("0");

      hbox = new(GtkHBox);
      main_vbox.pack_start(hbox, FALSE, TRUE, 0);

      button = new(GtkButton);
      button.label = "Close";
      button.signal("clicked", `(@win_spins).destroy());
      hbox.pack_start(button, TRUE, TRUE, 5);

      win_spins.show_all();
      win_spins;
    }

    function main ()
    {
      local window, win_spins;
      TRUE=1;
      FALSE=0;
      window = create_spins ();
      window.signal ("destroy", #exit_program(0));
      init_ipc("spins", "spinsq");
      gtk_main ();
    }

Prev | Home | Next
Scrolled Windows | Up | Text

Copyright 1995-2002 by Cogent Real-Time Systems, Inc.