Skip to content

Instantly share code, notes, and snippets.

@aferust
Last active October 20, 2020 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aferust/47cb782b55cdab1f7775b7755bc50982 to your computer and use it in GitHub Desktop.
Save aferust/47cb782b55cdab1f7775b7755bc50982 to your computer and use it in GitHub Desktop.
module app;
import std.stdio;
import std.experimental.logger: trace;
import std.conv;
import gio.Application : GApplication = Application;
import gtk.Main;
import gtk.Application;
import gtk.ListBox;
import glib.ListG;
import gtk.ListBoxRow;
class GtkDApp : Application {
public:
this(){
ApplicationFlags flags = ApplicationFlags.FLAGS_NONE;
super("org.gnome.projectname", flags);
this.addOnActivate(&onAppActivate);
this.window = null;
}
private:
AppWindow window;
void onAppActivate(GApplication app){
trace("Activate App Signal");
if (!app.getIsRemote()){
this.window = new AppWindow(this);
}
this.window.present();
}
}
void main(string[] args) {
Main.init(args);
auto app = new GtkDApp();
app.run(args);
}
import core.stdc.stdlib;
import std.functional;
import std.experimental.logger;
import gtk.Widget, gdk.Event;
import gtk.ApplicationWindow;
import gtk.Box;
import gtk.Entry;
import gtk.Label;
import gtk.Button;
import gtk.Statusbar, gtk.Separator;
class AppWindow: ApplicationWindow {
Box vbox1;
Label label1;
Button button1;
ListBox listbox;
Box fake_header;
Box mcontent;
this(gtk.Application.Application application){
super(application);
vbox1 = new Box(GtkOrientation.VERTICAL, 5);
this.addOnDelete(delegate bool(Event e, Widget w) {
exit(0);
return true;
});
listbox = new ListBox;
listbox.setSelectionMode(GtkSelectionMode.NONE);
populateList();
mcontent = new Box(GtkOrientation.VERTICAL, 3);
vbox1.add(mcontent);
vbox1.add(listbox);
makeHeader();
mcontent.packStart(fake_header, false, true, 0);
mcontent.reorderChild(fake_header, 0);
mcontent.showAll();
listbox.showAll();
auto ename = new Entry();
auto eage = new Entry();
auto addBut = new Button("Add");
addBut.addOnClicked((Button aux){
addRecord(ename.getText, eage.getText.to!uint);
});
auto hbox1 = new Box(GtkOrientation.HORIZONTAL, 5);
hbox1.add(ename);
hbox1.add(eage);
hbox1.add(addBut);
vbox1.add(hbox1);
add(vbox1);
showAll();
}
void populateList(){
auto a = new ListBoxRowWithData(this, "Alain", 20);
auto b = new ListBoxRowWithData(this, "Eddy", 30);
auto c = new ListBoxRowWithData(this, "Foo", 39);
listbox.add(cast(ListBoxRow)a);
listbox.add(cast(ListBoxRow)b);
listbox.add(cast(ListBoxRow)c);
}
void addRecord(string name, uint age){
auto a = new ListBoxRowWithData(this, name, age);
listbox.add(cast(ListBoxRow)a);
}
void makeHeader(){
if(fake_header is null){
fake_header = new Box(Orientation.HORIZONTAL, 0);
auto it = new Label("Name");
auto mi = new Label("Age");
auto bu = new Label("Action");
fake_header.packStart (it, true, true, 0);
fake_header.packStart (mi, true, true, 0);
fake_header.packStart (bu, true, true, 0);
fake_header.addOnShow(delegate void(Widget wid){
auto row = cast(ListBoxRowWithData)(listbox.getRowAtIndex(0));
row.init_widths();
it.setSizeRequest(ListBoxRowWithData.name_width, -1);
mi.setSizeRequest(ListBoxRowWithData.age_width, -1);
bu.setSizeRequest(ListBoxRowWithData.bt_width, -1);
});
fake_header.showAll();
}
}
}
class ListBoxRowWithData: ListBoxRow {
private:
AppWindow ctx;
public:
string name;
uint age;
Label labelName;
Label labelAge;
Button delEntry;
static int name_width;
static int age_width;
static int bt_width;
this(AppWindow ctx, string name, uint age) {
this.ctx = ctx;
this.name = name;
this.age = age;
labelName = new Label(name); labelName.setSizeRequest(100, -1);
labelAge = new Label(age.to!string); labelAge.setSizeRequest(100, -1);
delEntry = new Button(); delEntry.setLabel("delete"); delEntry.setSizeRequest(100, -1);
delEntry.addOnClicked(delegate void(Button _){
ctx.listbox.remove(this);
});
Box inner_box = new Box (Orientation.HORIZONTAL, 0);
inner_box.packStart (labelName, true, true, 0);
inner_box.packStart (labelAge, true, true, 0);
inner_box.packStart (delEntry, true, true, 0);
this.add(inner_box);
showAll();
}
void init_widths(){
if(name_width == 0){
name_width = labelName.getAllocatedWidth() > name_width ? labelName.getAllocatedWidth(): name_width;
age_width = labelAge.getAllocatedWidth() > age_width ? labelAge.getAllocatedWidth(): age_width;
bt_width = delEntry.getAllocatedWidth() > bt_width ? delEntry.getAllocatedWidth(): bt_width;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment