Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gtk.Application : Application;
- import gtk.ApplicationWindow : ApplicationWindow;
- import gtk.HeaderBar : HeaderBar;
- import gtk.ListStore : ListStore;
- import gtk.TreeView : TreeView;
- import gtkc.gobjecttypes : GType;
- import gtk.TreeViewColumn : TreeViewColumn;
- import gtk.CellRendererText : CellRendererText;
- import gtk.TreeIter : TreeIter, GtkTreeIter;
- import gtk.TreeModelFilter : TreeModelFilter;
- import gobject.ObjectG : GDestroyNotify;
- import gtk.TreeModel : TreeModel, GtkTreeModel;
- import gtk.Box : Box;
- import gtkc.gtktypes : GtkOrientation;
- import gtk.Entry : Entry;
- import gtk.Label : Label;
- import gtk.Button : Button;
- private class Album {
- private:
- enum COLUMNS {
- ARTIST,
- SONG
- }
- ListStore model;
- TreeView view;
- TreeModelFilter filter;
- static ListStore generateModel() {
- return new ListStore([GType.STRING, GType.STRING]);
- }
- static TreeView generateView() {
- auto tv = new TreeView();
- auto artistColumn = new TreeViewColumn();
- scope (success)
- tv.appendColumn(artistColumn);
- artistColumn.setTitle("Artist");
- auto artistColumnCell = new CellRendererText();
- artistColumn.packStart(artistColumnCell, true);
- artistColumn.addAttribute(artistColumnCell, "text", COLUMNS.ARTIST);
- auto songColumn = new TreeViewColumn();
- scope (success)
- tv.appendColumn(songColumn);
- songColumn.setTitle("Song");
- auto songColumnCell = new CellRendererText();
- songColumn.packStart(songColumnCell, true);
- songColumn.addAttribute(songColumnCell, "text", COLUMNS.SONG);
- return tv;
- }
- TreeModelFilter generateArtistFilter(in string artistName) {
- // nested private function
- static extern (C) int fn(GtkTreeModel* m, GtkTreeIter* i, string artistName) {
- TreeModel model_ = new TreeModel(m);
- TreeIter iter = new TreeIter(i);
- string name = model_.getValue(iter, COLUMNS.ARTIST).getString();
- return name == artistName;
- }
- auto filter = new TreeModelFilter(model, null);
- filter.setVisibleFunc(&fn, cast(void*) artistName, cast(GDestroyNotify) null);
- return filter;
- }
- public:
- this() {
- model = generateModel();
- view = generateView();
- filter = generateArtistFilter("Linkin park");
- view.setModel(filter);
- }
- void addSong(in string artistName, in string songTitle) {
- auto iter = this.model.createIter();
- model.setValue(iter, COLUMNS.ARTIST, artistName);
- model.setValue(iter, COLUMNS.SONG, songTitle);
- }
- TreeView getView() {
- return view;
- }
- }
- class PrimaryWindow : ApplicationWindow {
- private:
- HeaderBar titleBar;
- Album album;
- Box vbox;
- Label filterLabel;
- Button filterTrigger;
- Entry filterEntry;
- static HeaderBar makeHeaderBar(string title = "GtkD MVC Demo") {
- auto hb = new HeaderBar();
- hb.setTitle(title);
- hb.setShowCloseButton(true);
- return hb;
- }
- public:
- this(Application app) {
- super(app);
- titleBar = makeHeaderBar;
- setTitlebar(titleBar);
- setBorderWidth(20);
- vbox = new Box(GtkOrientation.VERTICAL, 5);
- scope (success)
- add(vbox);
- auto hbox0 = new Box(GtkOrientation.HORIZONTAL, 5);
- filterLabel = new Label("Artist search");
- filterEntry = new Entry("Name");
- filterTrigger = new Button("Search");
- hbox0.packStart(filterLabel, false, true, 0);
- hbox0.packEnd(filterTrigger, false, false, 0);
- hbox0.packEnd(filterEntry, true, true, 0);
- vbox.packStart(hbox0, false, true, 0);
- auto hbox1 = new Box(GtkOrientation.HORIZONTAL, 5);
- album = new Album();
- album.addSong("Linkin Park", "Leave Out All The Rest");
- album.addSong("Johnny Cash", "Hurt");
- album.addSong("Owl City", "Fireflies");
- hbox1.packStart(album.getView(), true, true, 0);
- vbox.packStart(hbox1, true, true, 0);
- }
- }
- int main(string[] args) {
- import gio.Application : GioApp = Application;
- import gtkc.gtktypes : GApplicationFlags;
- auto app = new Application("org.gitlab.gtkdnotes", GApplicationFlags.FLAGS_NONE);
- app.addOnActivate(delegate void(GioApp _) {
- auto pw = new PrimaryWindow(app);
- pw.showAll();
- });
- return app.run(args);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement