From 2898ff05abdd29e6a1842aa53a4e15024e382ed1 Mon Sep 17 00:00:00 2001 From: Stefan Salewski Date: Fri, 3 Jan 2020 19:22:40 +0100 Subject: [PATCH] added spinbutton.nim --- README.adoc | 53 ++++++++++++++++++++++++++++++++++++ examples/gtk3/spinbutton.nim | 42 ++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 examples/gtk3/spinbutton.nim diff --git a/README.adoc b/README.adoc index 9d3c7db..720f040 100644 --- a/README.adoc +++ b/README.adoc @@ -687,6 +687,59 @@ an illegal CSS statement -- if the statement is seriously wrong, then you should 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 diff --git a/examples/gtk3/spinbutton.nim b/examples/gtk3/spinbutton.nim new file mode 100644 index 0000000..95bf9c8 --- /dev/null +++ b/examples/gtk3/spinbutton.nim @@ -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() +