Column conf in separate fun, global window, try at column adding

master
Crom (Thibaut CHARLES) 8 years ago
parent 8c3bbad2c2
commit df832b023b

@ -21,12 +21,14 @@ import gtk.ScrolledWindow;
import gtk.HBox;
MainWindow window;
void main(string[] args)
{
Main.init(args);
//Window
auto window = new MainWindow("2DA-Edit");
window = new MainWindow("2DA-Edit");
auto cont = new VBox(false, 0);
window.add(cont);
@ -42,6 +44,8 @@ void main(string[] args)
buttonDelete.setTooltipText("Delete row");
auto buttonRenumber = new Button("view-list-symbolic", GtkIconSize.SMALL_TOOLBAR);
buttonRenumber.setTooltipText("Renumber all rows");
auto buttonNewCol = new Button("tab-new-symbolic", GtkIconSize.SMALL_TOOLBAR);
buttonNewCol.setTooltipText("Add new column");
version(Windows){
//Menu bar
@ -72,6 +76,7 @@ void main(string[] args)
statusbar.packStart(buttonRenumber, false, false, 5);
statusbar.packStart(buttonInsert, false, false, 5);
statusbar.packStart(buttonDelete, false, false, 5);
statusbar.packEnd(buttonNewCol, false, false, 5);
//TreeView to display database
auto scroll = new ScrolledWindow(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
@ -101,8 +106,8 @@ void main(string[] args)
auto res = fc.run();
if(res==GtkResponseType.OK){
string filename = fc.getFilename();
version(Windows) Open(filename, window, tree, cast(Object)window);
else Open(filename, window, tree, cast(Object)header);
version(Windows) Open(filename, tree, cast(Object)window);
else Open(filename, tree, cast(Object)header);
}
fc.destroy();
});
@ -143,12 +148,38 @@ void main(string[] args)
}
});
buttonNewCol.addOnClicked((Button){
auto store = cast(ListStore)tree.getModel();
int newColIndex = store.getNColumns();
GType[] types;
foreach(i ; 0..newColIndex+1)
types~= store.getColumnType(i);
store.setColumnTypes(types);
writeln(types.length);
//auto col = SetupColumn(tree, "new_col", newColIndex);
//TreeIter it = new TreeIter();
//if(store.getIterFirst(it)){
// do{
// store.setValue(it, newColIndex, "_");
// }while(store.iterNext(it));
//}
//tree.appendColumn(col);
});
//Open if exists
if(args.length>=2 && exists(args[1])){
version(Windows) Open(args[1], window, tree, cast(Object)window);
else Open(args[1], window, tree, cast(Object)header);
version(Windows) Open(args[1], tree, cast(Object)window);
else Open(args[1], tree, cast(Object)header);
}
window.showAll();
@ -188,7 +219,7 @@ void Save(ref TreeView tree){
}
string openedFile;
void Open(string file, ref MainWindow win, ref TreeView tree, Object header){
void Open(string file, ref TreeView tree, Object header){
auto twoda = new TwoDA(file);
@ -222,55 +253,7 @@ void Open(string file, ref MainWindow win, ref TreeView tree, Object header){
//Setup TreeView columns
foreach(index, s ; twoda.header){
CellRendererText cr = new CellRendererText();
cr.setProperty("editable", true);
if(index==0){
cr.setProperty("background-rgba", cast(ulong)(new GdkRGBA(0.36, 0.13, 0.4, 1.0)));
cr.setProperty("background-set", true);
cr.addOnEdited((string path, string newval, CellRendererText crt){
try{
int n = newval.to!int;
TreeIter t = new TreeIter(tree.getModel(), path);
store.setValue(t, cast(int)crt.getData("colnumber"), n);
}
catch(Exception e){
writeln("Not a number");
}
});
}
else{
cr.addOnEdited((string path, string newval, CellRendererText crt){
TreeIter t = new TreeIter(tree.getModel(), path);
store.setValue(t, cast(int)crt.getData("colnumber"), newval);
});
}
cr.setData("colnumber", cast(void*)cast(int)index);
auto col = new TreeViewColumn(s, cr, "text", cast(int)index);
col.setResizable(true);
col.setClickable(true);
col.addOnClicked((TreeViewColumn col){
import gtk.Dialog;
auto dlg = new Dialog("Rename column", win, GtkDialogFlags.MODAL, ["Cancel","Rename"], [ResponseType.CANCEL, ResponseType.OK]);
auto entry = new Entry(col.getTitle);
dlg.getContentArea.packStart(entry, false, false, 5);
entry.show();
if(dlg.run()==ResponseType.OK){
auto newname = entry.getText.strip;
if(newname.countchars(" \t\n\r")==0)
col.setTitle(newname);
else
writeln("Unauthorized caracters in column name");
}
dlg.destroy();
});
auto col = SetupColumn(tree, s, index);
tree.appendColumn(col);
}
@ -300,6 +283,66 @@ void Open(string file, ref MainWindow win, ref TreeView tree, Object header){
}
//Note: tree is only used on events (click)
auto ref SetupColumn(TreeView tree, string sName, size_t index){
auto store = cast(ListStore)tree.getModel();
CellRendererText cr = new CellRendererText();
cr.setProperty("editable", true);
if(index==0){
cr.setProperty("background-rgba", cast(ulong)(new GdkRGBA(0.36, 0.13, 0.4, 1.0)));
cr.setProperty("background-set", true);
cr.addOnEdited((string path, string newval, CellRendererText crt){
try{
int n = newval.to!int;
TreeIter t = new TreeIter(tree.getModel(), path);
store.setValue(t, cast(int)crt.getData("colnumber"), n);
}
catch(Exception e){
writeln("Not a number");
}
});
}
else{
cr.addOnEdited((string path, string newval, CellRendererText crt){
TreeIter t = new TreeIter(tree.getModel(), path);
store.setValue(t, cast(int)crt.getData("colnumber"), newval);
});
}
cr.setData("colnumber", cast(void*)cast(int)index);
auto col = new TreeViewColumn(sName, cr, "text", cast(int)index);
col.setResizable(true);
col.setMinWidth(10);
col.setClickable(true);
col.addOnClicked((TreeViewColumn col){
import gtk.Dialog;
auto dlg = new Dialog("Rename column", window, GtkDialogFlags.MODAL, ["Cancel","Rename"], [ResponseType.CANCEL, ResponseType.OK]);
auto entry = new Entry(col.getTitle);
dlg.getContentArea.packStart(entry, false, false, 5);
entry.show();
if(dlg.run()==ResponseType.OK){
auto newname = entry.getText.strip;
if(newname.countchars(" \t\n\r")==0)
col.setTitle(newname);
else
writeln("Unauthorized caracters in column name");
}
dlg.destroy();
});
return col;
}
class TwoDA{
import std.regex;

Loading…
Cancel
Save