Skip to content

Instantly share code, notes, and snippets.

@JohnCoconut
Created May 11, 2018 08:46
Show Gist options
  • Save JohnCoconut/0ded49c3646b2dddcc098c782cfef7e5 to your computer and use it in GitHub Desktop.
Save JohnCoconut/0ded49c3646b2dddcc098c782cfef7e5 to your computer and use it in GitHub Desktop.
module gtkdemo.SpinnerDemo;
import std.random : randomSample;
import std.conv : to;
import gio.Application : GioApplication = Application;
import gtk.Application;
import gtk.ApplicationWindow;
import gtk.TreeView;
import gtk.ListStore;
import gtk.CellRendererSpinner;
import gtk.CellRendererText;
import gtk.TreeIter;
import gtk.TreeViewColumn;
import gtk.TreeModelIF;
import glib.Timeout;
import gobject.ObjectG;
class TreeViewDemo : TreeView
{
ListStore ls;
CellRendererSpinner spinner;
this()
{
ls = new ListStore([GType.BOOLEAN, GType.INT]);
TreeIter iter;
bool[] bools = [true, true, false];
for (int i = 0; i < 10; ++i)
{
ls.append(iter);
bool b = randomSample(bools, 1).front;
ls.setValue(iter, 0, b);
ls.setValue(iter, 1, i);
}
setModel(ls);
spinner = new CellRendererSpinner();
TreeViewColumn colBool = new TreeViewColumn("bool", spinner, "active", 0);
appendColumn(colBool);
CellRendererText text = new CellRendererText();
TreeViewColumn colInt = new TreeViewColumn("int", text, "text", 1);
appendColumn(colInt);
onSpinnerPulse();
}
bool onSpinnerPulse()
{
TreeModelIF model = getModel();
TreeIter iter;
model.getIterFirst(iter);
uint pulse;
do {
bool isActive = model.getValue(iter, 0).getBoolean();
if (isActive) {
pulse = model.getValue(iter, 1).getInt();
if (pulse == 150) {
ls.setValue(iter, 1, 0);
} else {
ls.setValue(iter, 1, pulse + 1);
}
}
} while (model.iterNext(iter));
ObjectG obj = spinner;
obj.setProperty("pulse", pulse);
return true;
}
}
class WindowDemo : ApplicationWindow
{
this(Application application)
{
super(application);
setTitle("GtkD SpinnerDemo");
setBorderWidth(10);
TreeViewDemo demo = new TreeViewDemo();
add(demo);
Timeout timeout = new Timeout(100, &demo.onSpinnerPulse, true);
showAll();
}
}
int main(string[] args)
{
Application application =
new Application("com.example.gtkdemo.spinnerdemo", GApplicationFlags.FLAGS_NONE);
application.
addOnActivate(delegate void(GioApplication app) {
{ new WindowDemo(application); }
});
return application.run(args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment