' Spinbutton.bas
' Translated from C to FB by TinyCla, 2006
'
' Reviewed by TJF (2011-2014)
' Details: http://developer.gnome.org/gtk/

#INCLUDE ONCE "Gir/Gtk-3.0.bi"
'#INCLUDE ONCE "Gir/Gtk-2.0.bi"
#INCLUDE ONCE "Gir/_GObjectMacros-2.0.bi"
#INCLUDE ONCE "vbcompat.bi"

DIM SHARED AS GtkWidget PTR spinner1

SUB toggle_snap CDECL(BYVAL widget AS GtkWidget PTR, BYVAL spin AS GtkSpinButton PTR)

  VAR active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON (widget))
  gtk_spin_button_set_snap_to_ticks (spin, active)

END SUB

SUB toggle_numeric CDECL(BYVAL widget AS GtkWidget PTR, BYVAL spin AS GtkSpinButton PTR)

  VAR active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON (widget))
  gtk_spin_button_set_numeric (spin, active)

END SUB

SUB change_digits CDECL(BYVAL widget AS GtkWidget PTR, BYVAL spin AS GtkSpinButton PTR)

  VAR n_digits = gtk_spin_button_get_value_as_int (spin)
  gtk_spin_button_set_digits (GTK_SPIN_BUTTON (spinner1), n_digits)

END SUB

SUB get_value CDECL(BYVAL widget AS GtkWidget PTR, BYVAL param AS gchar PTR)

  DIM AS STRING buf
  VAR str_format = "###,###,##0"
  VAR spin = GTK_SPIN_BUTTON (spinner1)
  VAR label = GTK_LABEL (g_object_get_data (G_OBJECT (widget), "user_data"))

  IF *param = "1" THEN
    VAR val_spin = gtk_spin_button_get_value_as_int (spin)
    buf = FORMAT(val_spin, str_format)
  ELSE
    VAR val_spin_dbl = gtk_spin_button_get_value (spin)
    VAR num_digit = gtk_spin_button_get_digits (spin)
    IF num_digit > 0 THEN
      str_format += "."
      FOR j AS INTEGER = 1 TO num_digit
        str_format += "0"
      NEXT
    END IF
    buf = FORMAT(val_spin_dbl, str_format)
  END IF
  gtk_label_set_text (label, SADD(buf))

END SUB


' ==============================================
' Main
' ==============================================

' Initialize the toolkit
gtk_init(@__FB_ARGC__, @__FB_ARGV__)

VAR win = gtk_window_new (GTK_WINDOW_TOPLEVEL)

g_signal_connect (G_OBJECT (win), "destroy", _
                  G_CALLBACK (@gtk_main_quit), NULL)

gtk_window_set_title (GTK_WINDOW (win), "Spin Button")

VAR main_vbox = gtk_vbox_new (FALSE, 5)
gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 10)
gtk_container_add (GTK_CONTAINER (win), main_vbox)

VAR frame = gtk_frame_new ("Not accelerated")
gtk_box_pack_start (GTK_BOX (main_vbox), frame, TRUE, TRUE, 0)

VAR vbox = gtk_vbox_new (FALSE, 0)
gtk_container_set_border_width (GTK_CONTAINER (vbox), 5)
gtk_container_add (GTK_CONTAINER (frame), vbox)

' Day, month, year spinners
VAR hbox = gtk_hbox_new (FALSE, 0)
gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 5)

VAR vbox2 = gtk_vbox_new (FALSE, 0)
gtk_box_pack_start (GTK_BOX (hbox), vbox2, TRUE, TRUE, 5)

VAR label = gtk_label_new ("Day :")
gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5)
gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, TRUE, 0)

VAR adj = CAST(GtkAdjustment PTR, _
           gtk_adjustment_new (1.0, 1.0, 31.0, 1.0, 5.0, 0.0))
VAR spinner = gtk_spin_button_new (adj, 0, 0)
gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (spinner), TRUE)
gtk_box_pack_start (GTK_BOX (vbox2), spinner, FALSE, TRUE, 0)

vbox2 = gtk_vbox_new (FALSE, 0)
gtk_box_pack_start (GTK_BOX (hbox), vbox2, TRUE, TRUE, 5)

label = gtk_label_new ("Month :")
gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5)
gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, TRUE, 0)

adj = CAST(GtkAdjustment PTR, _
           gtk_adjustment_new (1.0, 1.0, 12.0, 1.0, 5.0, 0.0))
spinner = gtk_spin_button_new (adj, 0, 0)
gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (spinner), TRUE)
gtk_box_pack_start (GTK_BOX (vbox2), spinner, FALSE, TRUE, 0)

vbox2 = gtk_vbox_new (FALSE, 0)
gtk_box_pack_start (GTK_BOX (hbox), vbox2, TRUE, TRUE, 5)

label = gtk_label_new ("Year :")
gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5)
gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, TRUE, 0)

adj = CAST(GtkAdjustment PTR, _
           gtk_adjustment_new (1998.0, 0.0, 2100.0, 1.0, 100.0, 0.0))
spinner = gtk_spin_button_new (adj, 0, 0)
gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (spinner), FALSE)
gtk_widget_set_size_request (spinner, 55, -1)
gtk_box_pack_start (GTK_BOX (vbox2), spinner, FALSE, TRUE, 0)

frame = gtk_frame_new ("Accelerated")
gtk_box_pack_start (GTK_BOX (main_vbox), frame, TRUE, TRUE, 0)

vbox = gtk_vbox_new (FALSE, 0)
gtk_container_set_border_width (GTK_CONTAINER (vbox), 5)
gtk_container_add (GTK_CONTAINER (frame), vbox)

hbox = gtk_hbox_new (FALSE, 0)
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 5)

vbox2 = gtk_vbox_new (FALSE, 0)
gtk_box_pack_start (GTK_BOX (hbox), vbox2, TRUE, TRUE, 5)

label = gtk_label_new ("Value :")
gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5)
gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, TRUE, 0)

adj = CAST(GtkAdjustment PTR, _
           gtk_adjustment_new (0.0, -10000.0, 10000.0, 0.5, 100.0, 0.0))
spinner1 = gtk_spin_button_new (adj, 1.0, 2)
gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (spinner1), TRUE)
gtk_widget_set_size_request (spinner1, 100, -1)
gtk_box_pack_start (GTK_BOX (vbox2), spinner1, FALSE, TRUE, 0)

vbox2 = gtk_vbox_new (FALSE, 0)
gtk_box_pack_start (GTK_BOX (hbox), vbox2, TRUE, TRUE, 5)

label = gtk_label_new ("Digits :")
gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5)
gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, TRUE, 0)

adj = CAST(GtkAdjustment PTR, gtk_adjustment_new (2, 1, 5, 1, 1, 0))
VAR spinner2 = gtk_spin_button_new (adj, 0.0, 0)
gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (spinner2), TRUE)
g_signal_connect (G_OBJECT (adj), "value_changed", _
                  G_CALLBACK (@change_digits), CAST(gpointer, spinner2))
gtk_box_pack_start (GTK_BOX (vbox2), spinner2, FALSE, TRUE, 0)

hbox = gtk_hbox_new (FALSE, 0)
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 5)

VAR button = gtk_check_button_new_with_label ("Snap to 0.5-ticks")
g_signal_connect (G_OBJECT (button), "clicked", _
                  G_CALLBACK (@toggle_snap), spinner1)
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0)
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE)

button = gtk_check_button_new_with_label ("Numeric only input mode")
g_signal_connect (G_OBJECT (button), "clicked", _
                  G_CALLBACK (@toggle_numeric), spinner1)
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0)
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE)

VAR val_label = gtk_label_new ("")

hbox = gtk_hbox_new (FALSE, 0)
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 5)
button = gtk_button_new_with_label ("Value as Int")
g_object_set_data (G_OBJECT (button), "user_data", val_label)
g_signal_connect (G_OBJECT (button), "clicked", _
                  G_CALLBACK (@get_value), STRPTR("1"))
gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 5)

button = gtk_button_new_with_label ("Value as Float")
g_object_set_data (G_OBJECT (button), "user_data", val_label)
g_signal_connect (G_OBJECT (button), "clicked", _
                  G_CALLBACK (@get_value), STRPTR("2"))
gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 5)

gtk_box_pack_start (GTK_BOX (vbox), val_label, TRUE, TRUE, 0)
gtk_label_set_text (GTK_LABEL (val_label), "0")

hbox = gtk_hbox_new (FALSE, 0)
gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, TRUE, 0)

button = gtk_button_new_with_label ("Close")
g_signal_connect (G_OBJECT (button), "clicked", _
                  G_CALLBACK (@gtk_main_quit), NULL)
gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 5)

gtk_widget_show_all (win)

' Enter the event loop
gtk_main ()
