SHARE
TWEET

Untitled

a guest Nov 6th, 2018 134 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import gtk.Application : Application;
  2. import gtk.ApplicationWindow : ApplicationWindow;
  3. import gtk.HeaderBar : HeaderBar;
  4. import gtk.ListStore : ListStore;
  5. import gtk.TreeView : TreeView;
  6. import gtkc.gobjecttypes : GType;
  7. import gtk.TreeViewColumn : TreeViewColumn;
  8. import gtk.CellRendererText : CellRendererText;
  9. import gtk.TreeIter : TreeIter, GtkTreeIter;
  10. import gtk.TreeModelFilter : TreeModelFilter;
  11. import gobject.ObjectG : GDestroyNotify;
  12. import gtk.TreeModel : TreeModel, GtkTreeModel;
  13. import gtk.Box : Box;
  14. import gtkc.gtktypes : GtkOrientation;
  15. import gtk.Entry : Entry;
  16. import gtk.Label : Label;
  17. import gtk.Button : Button;
  18.  
  19. private class Album {
  20. private:
  21.     enum COLUMNS {
  22.         ARTIST,
  23.         SONG
  24.     }
  25.  
  26.     ListStore model;
  27.     TreeView view;
  28.     TreeModelFilter filter;
  29.  
  30.     static ListStore generateModel() {
  31.         return new ListStore([GType.STRING, GType.STRING]);
  32.     }
  33.  
  34.     static TreeView generateView() {
  35.         auto tv = new TreeView();
  36.  
  37.         auto artistColumn = new TreeViewColumn();
  38.         scope (success)
  39.             tv.appendColumn(artistColumn);
  40.         artistColumn.setTitle("Artist");
  41.  
  42.         auto artistColumnCell = new CellRendererText();
  43.         artistColumn.packStart(artistColumnCell, true);
  44.         artistColumn.addAttribute(artistColumnCell, "text", COLUMNS.ARTIST);
  45.  
  46.         auto songColumn = new TreeViewColumn();
  47.         scope (success)
  48.             tv.appendColumn(songColumn);
  49.         songColumn.setTitle("Song");
  50.  
  51.         auto songColumnCell = new CellRendererText();
  52.         songColumn.packStart(songColumnCell, true);
  53.         songColumn.addAttribute(songColumnCell, "text", COLUMNS.SONG);
  54.  
  55.         return tv;
  56.     }
  57.  
  58.     TreeModelFilter generateArtistFilter(in string artistName) {
  59.         // nested private function
  60.         static extern (C) int fn(GtkTreeModel* m, GtkTreeIter* i, string artistName) {
  61.             TreeModel model_ = new TreeModel(m);
  62.             TreeIter iter = new TreeIter(i);
  63.  
  64.             string name = model_.getValue(iter, COLUMNS.ARTIST).getString();
  65.             return name == artistName;
  66.         }
  67.  
  68.         auto filter = new TreeModelFilter(model, null);
  69.         filter.setVisibleFunc(&fn, cast(void*) artistName, cast(GDestroyNotify) null);
  70.         return filter;
  71.     }
  72.  
  73. public:
  74.     this() {
  75.         model = generateModel();
  76.         view = generateView();
  77.         filter = generateArtistFilter("Linkin park");
  78.         view.setModel(filter);
  79.     }
  80.  
  81.     void addSong(in string artistName, in string songTitle) {
  82.         auto iter = this.model.createIter();
  83.         model.setValue(iter, COLUMNS.ARTIST, artistName);
  84.         model.setValue(iter, COLUMNS.SONG, songTitle);
  85.     }
  86.  
  87.     TreeView getView() {
  88.         return view;
  89.     }
  90. }
  91.  
  92. class PrimaryWindow : ApplicationWindow {
  93. private:
  94.     HeaderBar titleBar;
  95.     Album album;
  96.     Box vbox;
  97.     Label filterLabel;
  98.     Button filterTrigger;
  99.     Entry filterEntry;
  100.  
  101.     static HeaderBar makeHeaderBar(string title = "GtkD MVC Demo") {
  102.         auto hb = new HeaderBar();
  103.         hb.setTitle(title);
  104.         hb.setShowCloseButton(true);
  105.         return hb;
  106.     }
  107.  
  108. public:
  109.     this(Application app) {
  110.         super(app);
  111.         titleBar = makeHeaderBar;
  112.         setTitlebar(titleBar);
  113.         setBorderWidth(20);
  114.  
  115.         vbox = new Box(GtkOrientation.VERTICAL, 5);
  116.         scope (success)
  117.             add(vbox);
  118.  
  119.         auto hbox0 = new Box(GtkOrientation.HORIZONTAL, 5);
  120.  
  121.         filterLabel = new Label("Artist search");
  122.         filterEntry = new Entry("Name");
  123.         filterTrigger = new Button("Search");
  124.  
  125.         hbox0.packStart(filterLabel, false, true, 0);
  126.         hbox0.packEnd(filterTrigger, false, false, 0);
  127.         hbox0.packEnd(filterEntry, true, true, 0);
  128.         vbox.packStart(hbox0, false, true, 0);
  129.  
  130.         auto hbox1 = new Box(GtkOrientation.HORIZONTAL, 5);
  131.  
  132.         album = new Album();
  133.         album.addSong("Linkin Park", "Leave Out All The Rest");
  134.         album.addSong("Johnny Cash", "Hurt");
  135.         album.addSong("Owl City", "Fireflies");
  136.         hbox1.packStart(album.getView(), true, true, 0);
  137.         vbox.packStart(hbox1, true, true, 0);
  138.  
  139.     }
  140. }
  141.  
  142. int main(string[] args) {
  143.     import gio.Application : GioApp = Application;
  144.     import gtkc.gtktypes : GApplicationFlags;
  145.  
  146.     auto app = new Application("org.gitlab.gtkdnotes", GApplicationFlags.FLAGS_NONE);
  147.     app.addOnActivate(delegate void(GioApp _) {
  148.         auto pw = new PrimaryWindow(app);
  149.         pw.showAll();
  150.     });
  151.     return app.run(args);
  152. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top