Open button and re-open functions

This commit is contained in:
Crom (Thibaut CHARLES) 2014-11-18 11:46:07 +01:00
parent 64f7b92679
commit f93488a975
2 changed files with 91 additions and 39 deletions

View File

@ -1,4 +1,5 @@
import std.stdio;
import std.file;
import std.string;
import std.conv : to;
@ -17,19 +18,12 @@ import gtk.VBox;
import gtk.Button;
void[] Serialize(VT...)(VT data){
void[] ret;
foreach(d ; data){
ret~=cast(void[])([d]);
}
return ret;
}
void main(string[] args)
{
//Parse 2da
auto twoda = new TwoDA(args[1]);
Main.init(args);
//Window
@ -38,7 +32,12 @@ void main(string[] args)
auto cont = new VBox(false, 0);
window.add(cont);
Button saveButton = new Button(StockID.SAVE);
auto buttonSave = new Button("document-save-symbolic", GtkIconSize.MENU);
auto buttonOpen = new Button("document-open-symbolic", GtkIconSize.MENU);
//"user-trash-symbolic"
//"format-text-direction-ltr-symbolic"
//"view-list-symbolic"
version(Windows){
//Menu bar
import gtk.HBox;
@ -46,7 +45,8 @@ void main(string[] args)
auto cont2 = new HBox(false, 0);
cont.packStart(cont2, false, false, 0);
cont2.packEnd(saveButton, false, false, 0);
cont2.packStart(buttonOpen, false, false, 0);
cont2.packEnd(buttonSave, false, false, 0);
}
else{
//Header bar
@ -57,29 +57,92 @@ void main(string[] args)
header.setTitle("2DAEdit");
header.setSubtitle(args[1]);
header.setProperty("show-close-button", true);
saveButton = new Button(StockID.SAVE);
header.packEnd(saveButton);
header.packStart(buttonOpen);
header.packEnd(buttonSave);
}
//Database for 2da GTK table
GType type[];
type~=GType.STRING;
foreach(i;1..twoda.header.length)type~=GType.STRING;
auto store = new ListStore(type);
saveButton.addOnClicked((Button){Save(store, twoda.header.length);});
//TreeView to display database
auto tree = new TreeView(store);
auto tree = new TreeView();
cont.packEnd(tree, true, true, 0);
tree.setHeadersVisible(true);
tree.setEnableSearch(true);
tree.setModel(store);
tree.setProperty("enable-grid-lines", GtkTreeViewGridLines.VERTICAL);
tree.setProperty("tooltip-column", 0);
tree.setProperty("reorderable", true);
tree.setProperty("headers-clickable", true);
//Configure button callbacks
buttonSave.addOnClicked((Button){
Save(tree);
});
buttonOpen.addOnClicked((Button){
import gtk.Dialog;
import gtk.FileChooserDialog;
auto fc = new FileChooserDialog("Open 2DA", window, FileChooserAction.OPEN);
auto res = fc.run();
if(res==GtkResponseType.OK){
string filename = fc.getFilename();
Open(filename, tree);
}
fc.destroy();
});
//Open if exists
if(exists(args[1])){
Open(args[1], tree);
}
window.showAll();
Main.run();
}
void Save(ref TreeView tree){
auto store = cast(ListStore)tree.getModel();
if(store !is null){
TreeIter it = new TreeIter();
store.getIterFirst(it);//TODO: fail if first is null
do{
for(int i=0 ; i<store.getNColumns() ; i++){
std.stdio.write("\t",store.getValueString(it, i));
}
writeln();
}while(store.iterNext(it));
}
else
writeln("Nothing to save !");
}
void Open(string file, ref TreeView tree){
auto twoda = new TwoDA(file);
//Delete old store
auto oldstore = cast(ListStore)tree.getModel();
if(oldstore !is null)
oldstore.destroy();
//Remove columns from TreeView
foreach(i ; 0..tree.getNColumns)
tree.removeColumn(tree.getColumn(0));
//Set store types
GType type[];
foreach(i;0..twoda.header.length)type~=GType.STRING;
//Create new store
auto store = new ListStore(type);
tree.setModel(store);
//Setup TreeView columns
foreach(index, s ; twoda.header){
@ -130,22 +193,6 @@ void main(string[] args)
//Autosize columns
tree.columnsAutosize();
window.showAll();
Main.run();
}
void Save(ref ListStore store, size_t columns){
TreeIter it = new TreeIter();
store.getIterFirst(it);//TODO: fail if first is null
do{
for(int i=0 ; i<columns ; i++){
write("\t",store.getValueString(it, i));
}
writeln();
}while(store.iterNext(it));
}

5
test2.2da Normal file
View File

@ -0,0 +1,5 @@
id name description
0 "inspection du domicile" "révèle le rôle d'un personnage"
1 "galinette sandrée" "inflige 80 de dégats à un personnage"
2 "étrange rituel druidique" "rammène un mort (il peut être mort dans la même nuit), à la vie, peut utiliser le tchat en son nom, ne peut pas voter pour lui"
3 "farfouinnage" "révèle le rôle d'un personnage"