added spinbutton.nim

This commit is contained in:
Stefan Salewski 2020-01-03 19:22:40 +01:00
commit 2898ff05ab
2 changed files with 95 additions and 0 deletions

View file

@ -687,6 +687,59 @@ an illegal CSS statement -- if the statement is seriously wrong, then you should
loadFromData(). loadFromData().
== SpinButton
This widget is used for entering numerical values. We can type in the value with the keyboard, click on the
+/- symbols or use the scroll wheel of the mouse. This example also shows that we can use vertical or horizontal
orientation for this widget, and how we can use bindProperty() to bind a property of one widget to another widget.
Here we use a button to control wrapping behaviour of the spin buttons.
[[spinbutton.nim]]
[source,nim]
.spinbutton.nim
----
## https://github.com/GNOME/gtk/blob/gtk-3-24/tests/testspinbutton.c
## gcc `pkg-config gtk+-3.0 --cflags` spinbutton.c -o spinbutton `pkg-config --libs gtk+-3.0`
import gintro/[gtk, gdk, glib, gobject]
var numWindows: int
proc onDeleteEvent(w: gtk.Window; event: gdk.Event): bool =
dec(numWindows)
if numWindows == 0:
gtk.mainQuit()
return EVENT_PROPAGATE # false
proc prepareWindowForOrientation(orientation: gtk.Orientation) =
let window = newWindow()
discard connect(window, "delete_event", onDeleteEvent)
let mainbox = gtk.newBox(if orientation == gtk.Orientation.horizontal: Orientation.vertical else: Orientation.horizontal, 2)
window.add(mainbox)
let wrapButton = newToggleButtonWithLabel("Wrap")
mainbox.add(wrapButton)
var max = 0
while max <= 999999999:
let adj = newAdjustment(max.float, 1, max.float, 1, (max.float + 1) * 0.1, 0)
let spin = newSpinButton(adj, 1, 0)
spin.setOrientation(orientation)
spin.setHalign(gtk.Align.center)
discard bindProperty(wrapButton, "active", spin, "wrap", {BindingFlag.syncCreate})
let hbox = newBox(gtk.Orientation.horizontal, 2)
hbox.packStart(spin, false, false, 2)
mainbox.add(hbox)
max = max * 10 + 9
window.showAll()
inc(numWindows)
proc main =
gtk.init()
prepareWindowForOrientation(gtk.Orientation.horizontal)
prepareWindowForOrientation(gtk.Orientation.vertical)
gtk.main()
main()
----
== GTK Builder -- user interfaces created with the glade tool == GTK Builder -- user interfaces created with the glade tool

View file

@ -0,0 +1,42 @@
## https://github.com/GNOME/gtk/blob/gtk-3-24/tests/testspinbutton.c
## gcc `pkg-config gtk+-3.0 --cflags` spinbutton.c -o spinbutton `pkg-config --libs gtk+-3.0`
import gintro/[gtk, gdk, glib, gobject]
var numWindows: int
proc onDeleteEvent(w: gtk.Window; event: gdk.Event): bool =
dec(numWindows)
if numWindows == 0:
gtk.mainQuit()
return EVENT_PROPAGATE # false
proc prepareWindowForOrientation(orientation: gtk.Orientation) =
let window = newWindow()
discard connect(window, "delete_event", onDeleteEvent)
let mainbox = gtk.newBox(if orientation == gtk.Orientation.horizontal: Orientation.vertical else: Orientation.horizontal, 2)
window.add(mainbox)
let wrapButton = newToggleButtonWithLabel("Wrap")
mainbox.add(wrapButton)
var max = 0
while max <= 999999999:
let adj = newAdjustment(max.float, 1, max.float, 1, (max.float + 1) * 0.1, 0)
let spin = newSpinButton(adj, 1, 0)
spin.setOrientation(orientation)
spin.setHalign(gtk.Align.center)
discard bindProperty(wrapButton, "active", spin, "wrap", {BindingFlag.syncCreate})
let hbox = newBox(gtk.Orientation.horizontal, 2)
hbox.packStart(spin, false, false, 2)
mainbox.add(hbox)
max = max * 10 + 9
window.showAll()
inc(numWindows)
proc main =
gtk.init()
prepareWindowForOrientation(gtk.Orientation.horizontal)
prepareWindowForOrientation(gtk.Orientation.vertical)
gtk.main()
main()