2daedit/source/app.d

574 lines
14 KiB
D
Raw Normal View History

2014-11-16 22:48:56 +00:00
import std.stdio;
2014-11-18 10:46:07 +00:00
import std.file;
2014-11-16 22:48:56 +00:00
import std.string;
import std.conv : to;
import gtk.Main;
import gtk.MainWindow;
2014-11-19 17:13:29 +00:00
import gtk.Widget;
2014-11-16 22:48:56 +00:00
import gtk.TreeView;
import gtk.ListStore;
import gtk.TreeViewColumn;
import gtk.TreeIter;
import gtk.Label;
import gtk.Entry;
import gtk.CellRenderer;
import gtk.CellRendererText;
2014-11-17 22:25:13 +00:00
import gtk.VBox;
2014-11-18 13:06:26 +00:00
import gtk.Statusbar;
2014-11-17 14:12:57 +00:00
import gtk.Button;
2014-11-18 13:53:05 +00:00
import gtk.ScrolledWindow;
2014-11-19 14:59:47 +00:00
import gtk.AccelGroup;
2014-11-18 10:46:07 +00:00
2014-11-18 13:06:26 +00:00
import gtk.HBox;
2014-11-18 10:46:07 +00:00
2014-11-19 15:24:46 +00:00
__gshared MainWindow window;
__gshared HBox statusbar;
2014-11-19 15:52:06 +00:00
__gshared Object header;
2014-11-16 22:48:56 +00:00
void main(string[] args)
{
Main.init(args);
2014-11-17 14:49:53 +00:00
//Window
window = new MainWindow("2DA-Edit");
2014-11-16 22:48:56 +00:00
2014-11-19 14:59:47 +00:00
auto accel = new AccelGroup();
window.addAccelGroup(accel);
2014-11-17 22:25:13 +00:00
auto cont = new VBox(false, 0);
window.add(cont);
2014-11-18 14:20:39 +00:00
cont.setSizeRequest(300, 200);
2014-11-17 22:25:13 +00:00
2014-11-18 17:20:56 +00:00
version(Windows){
auto buttonSave = new Button(StockID.SAVE, true);
2014-11-19 15:52:06 +00:00
auto buttonSaveAs = new Button(StockID.SAVE_AS, true);
2014-11-18 17:20:56 +00:00
auto buttonOpen = new Button(StockID.OPEN, true);
auto buttonInsert = new Button(StockID.JUMP_TO, true);
auto buttonDelete = new Button(StockID.DELETE, true);
auto buttonRenumber = new Button(StockID.INDEX, true);
auto buttonNewCol = new Button(StockID.NEW, true);
}
else{
auto buttonSave = new Button("document-save-symbolic", GtkIconSize.MENU);
2014-11-19 15:52:06 +00:00
auto buttonSaveAs = new Button("document-save-as-symbolic", GtkIconSize.MENU);
2014-11-18 17:20:56 +00:00
auto buttonOpen = new Button("document-open-symbolic", GtkIconSize.MENU);
auto buttonInsert = new Button("format-text-direction-ltr-symbolic", GtkIconSize.SMALL_TOOLBAR);
auto buttonDelete = new Button("user-trash-symbolic", GtkIconSize.SMALL_TOOLBAR);
auto buttonRenumber = new Button("view-list-symbolic", GtkIconSize.SMALL_TOOLBAR);
auto buttonNewCol = new Button("tab-new-symbolic", GtkIconSize.SMALL_TOOLBAR);
}
2014-11-18 13:53:05 +00:00
buttonSave.setTooltipText("Save");
2014-11-19 15:52:06 +00:00
buttonSaveAs.setTooltipText("Save as");
2014-11-18 13:53:05 +00:00
buttonOpen.setTooltipText("Open 2DA");
buttonInsert.setTooltipText("Insert row after");
buttonDelete.setTooltipText("Delete row");
buttonRenumber.setTooltipText("Renumber all rows");
buttonNewCol.setTooltipText("Add new column");
2014-11-18 10:46:07 +00:00
2014-11-19 15:52:06 +00:00
enum GDK_KEY_S = 0x053;
buttonSave.addAccelerator("clicked", accel, GDK_KEY_S, GdkModifierType.CONTROL_MASK, GtkAccelFlags.VISIBLE);
buttonSaveAs.addAccelerator("clicked", accel, GDK_KEY_S, GdkModifierType.CONTROL_MASK|GdkModifierType.SHIFT_MASK, GtkAccelFlags.VISIBLE);
2014-11-17 22:25:13 +00:00
version(Windows){
//Menu bar
import gtk.HBox;
2014-11-19 15:52:06 +00:00
header = new HBox(false, 0);
2014-11-19 17:13:29 +00:00
cont.packStart(cast(HBox)header, false, false, 0);
2014-11-17 22:25:13 +00:00
2014-11-19 17:13:29 +00:00
(cast(HBox)header).packStart(buttonOpen, false, false, 0);
(cast(HBox)header).packEnd(buttonSaveAs, false, false, 0);
(cast(HBox)header).packEnd(buttonSave, false, false, 0);
2014-11-17 22:25:13 +00:00
}
else{
//Header bar
import gtk.HeaderBar;
2014-11-19 15:52:06 +00:00
header = new HeaderBar();
window.setTitlebar(cast(HeaderBar)header);
(cast(HeaderBar)header).setTitle("2DAEdit");
(cast(HeaderBar)header).setProperty("show-close-button", true);
2014-11-16 22:48:56 +00:00
2014-11-19 15:52:06 +00:00
(cast(HeaderBar)header).packStart(buttonOpen);
(cast(HeaderBar)header).packEnd(buttonSaveAs);
(cast(HeaderBar)header).packEnd(buttonSave);
2014-11-18 10:46:07 +00:00
}
2014-11-16 22:48:56 +00:00
2014-11-18 13:06:26 +00:00
//Status bar
2014-11-19 15:24:46 +00:00
statusbar = new HBox(false, 0);
2014-11-18 13:06:26 +00:00
cont.packEnd(statusbar, false, false, 5);
statusbar.packStart(buttonRenumber, false, false, 5);
statusbar.packStart(buttonInsert, false, false, 5);
statusbar.packStart(buttonDelete, false, false, 5);
statusbar.packEnd(buttonNewCol, false, false, 5);
2014-11-18 13:06:26 +00:00
2014-11-17 14:49:53 +00:00
//TreeView to display database
2014-11-18 13:53:05 +00:00
auto scroll = new ScrolledWindow(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
cont.packEnd(scroll, true, true, 0);
2014-11-18 10:46:07 +00:00
auto tree = new TreeView();
2014-11-18 13:53:05 +00:00
scroll.add(tree);
2014-11-17 14:49:53 +00:00
2014-11-16 22:48:56 +00:00
tree.setHeadersVisible(true);
tree.setEnableSearch(true);
2014-11-17 10:50:11 +00:00
tree.setProperty("enable-grid-lines", GtkTreeViewGridLines.VERTICAL);
2014-11-17 12:52:50 +00:00
tree.setProperty("tooltip-column", 0);
tree.setProperty("reorderable", true);
tree.setProperty("headers-clickable", true);
2014-11-19 13:47:35 +00:00
tree.addOnColumnsChanged((TreeView tree){
auto store = cast(ListStore)tree.getModel();
2015-03-06 11:15:52 +00:00
if(store is null) return;
2014-11-19 13:47:35 +00:00
2014-11-19 14:15:59 +00:00
int si = GetColumnStoreIndex(tree, 0);
if(si>=0 && store.getColumnType(si) != GType.INT){
2014-11-19 13:47:35 +00:00
foreach(i ; 0..store.getNColumns()){
2014-11-19 14:15:59 +00:00
si = GetColumnStoreIndex(tree, i);
if(si>=0 && store.getColumnType(si)==GType.INT){
2014-11-19 13:47:35 +00:00
tree.moveColumnAfter(tree.getColumn(i), null);
break;
}
}
}
});
2014-11-17 12:52:50 +00:00
2014-11-18 10:46:07 +00:00
//Configure button callbacks
buttonSave.addOnClicked((Button){
Save(tree);
});
2014-11-19 15:52:06 +00:00
buttonSaveAs.addOnClicked((Button){
import gtk.Dialog;
import gtk.FileChooserDialog;
auto fc = new FileChooserDialog("Save 2DA as", window, FileChooserAction.SAVE);
auto res = fc.run();
if(res==GtkResponseType.OK){
string filename = fc.getFilename();
Save(tree, filename);
}
fc.destroy();
});
2014-11-18 10:46:07 +00:00
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();
2014-11-19 15:52:06 +00:00
Open(filename, tree);
2014-11-18 10:46:07 +00:00
}
fc.destroy();
});
2014-11-18 13:41:47 +00:00
buttonInsert.addOnClicked((Button){
TreeIter it = tree.getSelectedIter();
auto store = cast(ListStore)tree.getModel();
if(store !is null){
store.insertAfter(it, it);
store.setValue(it, 0, 0);
foreach(i ; 1..store.getNColumns()){
store.setValue(it, cast(int)i, "_");
}
}
});
buttonDelete.addOnClicked((Button){
TreeIter it = tree.getSelectedIter();
auto store = cast(ListStore)tree.getModel();
if(store !is null)
store.remove(it);
});
buttonRenumber.addOnClicked((Button){
auto store = cast(ListStore)tree.getModel();
if(store !is null){
TreeIter it = new TreeIter();
if(store.getIterFirst(it)){
int id = 0;
do{
store.setValue(it, 0, id++);
}while(store.iterNext(it));
}
}
});
buttonNewCol.addOnClicked((Button){
2014-11-19 07:58:47 +00:00
auto oldstore = cast(ListStore)tree.getModel();
2015-03-06 11:15:52 +00:00
if(oldstore is null) return;
2014-11-19 07:58:47 +00:00
int newColIndex = oldstore.getNColumns();
2014-11-20 08:08:14 +00:00
foreach(i ; 0..tree.getNColumns){
auto col = tree.getColumn(i);
writeln("col",i,"=",col, "(", col is null? "" : col.getTitle, ")");
}
GType[] types;
2014-11-19 07:58:47 +00:00
string[] titles;
2014-11-20 08:08:14 +00:00
int[] storeIndex;
foreach(i ; 0..tree.getNColumns){
writeln("Saving ",i);
2014-11-19 07:58:47 +00:00
if(i==0)types~= GType.INT;
else types~= GType.STRING;
2014-11-20 08:08:14 +00:00
storeIndex~= GetColumnStoreIndex(tree, 0)>=0?GetColumnStoreIndex(tree, 0):0;
auto col = tree.getColumn(0);
if(col !is null) titles~= tree.getColumn(0).getTitle;
else titles~= "ERR";
2014-11-19 07:58:47 +00:00
tree.removeColumn(tree.getColumn(0));
}
types~=GType.STRING;
titles~="new_col";
2014-11-19 07:58:47 +00:00
auto store = new ListStore(types);
tree.setModel(store);
2014-11-19 07:58:47 +00:00
//Fill them
TreeIter oldit = new TreeIter();
TreeIter newit = new TreeIter();
if(oldstore.getIterFirst(oldit)){
do{
store.append(newit);
foreach(i ; 0..newColIndex+1){
if(i<newColIndex){
2014-11-20 08:08:14 +00:00
if(types[i]==GType.INT) store.setValue(newit, i, oldstore.getValueInt(oldit, storeIndex[i]));
else store.setValue(newit, i, oldstore.getValueString(oldit, storeIndex[i]));
2014-11-19 07:58:47 +00:00
}
else
store.setValue(newit, cast(int)i, "_");
}
}while(oldstore.iterNext(oldit));
}
2014-11-19 14:15:59 +00:00
//setup cols
foreach(i ; 0..newColIndex+1){
tree.appendColumn(SetupColumn(tree, titles[i], i));
}
2014-11-19 07:58:47 +00:00
oldstore.destroy();
});
2014-11-18 13:41:47 +00:00
2014-11-18 10:46:07 +00:00
//Open if exists
if(args.length>=2 && exists(args[1])){
2014-11-19 15:52:06 +00:00
Open(args[1], tree);
2014-11-18 10:46:07 +00:00
}
window.showAll();
Main.run();
}
2014-11-19 13:47:35 +00:00
int GetColumnStoreIndex(TreeView tree, int colindex){
2014-11-19 14:15:59 +00:00
auto col = tree.getColumn(colindex);
if(col !is null)
return cast(int)(col.getData("colnumber"));
return -1;
2014-11-19 13:47:35 +00:00
}
2014-11-19 15:24:46 +00:00
void SaySomething(string msg){
import core.thread;
new Thread({
Thread.getThis.sleep(dur!"msecs"(100));
auto lbl = new Label("");
lbl.setMarkup("<i>"~msg~"</i>");
statusbar.packEnd(lbl, false, false, 5);
2014-11-19 17:13:29 +00:00
version(Windows){
lbl.show();
Thread.getThis.sleep(dur!"msecs"(1500));
2014-11-19 15:24:46 +00:00
}
2014-11-19 17:13:29 +00:00
else{
//Wow, much animation, very badass
lbl.setOpacity(0.0);
lbl.show();
foreach(i ; 0..20){
lbl.setOpacity(i/20.0);
Thread.getThis.sleep(dur!"msecs"(10));
}
Thread.getThis.sleep(dur!"msecs"(1500));
foreach(i ; 1..20){
lbl.setOpacity(1.0-i/20.0);
Thread.getThis.sleep(dur!"msecs"(10));
}
2014-11-19 15:24:46 +00:00
}
//Destroy
lbl.destroy();
}).start();
}
2014-11-19 15:52:06 +00:00
void Save(ref TreeView tree, string newpath=""){
2014-11-18 10:46:07 +00:00
auto store = cast(ListStore)tree.getModel();
if(store !is null){
2014-11-19 15:52:06 +00:00
if(newpath!=""){
openedFile = newpath;
SetTitle(openedFile);
}
2014-11-18 14:20:39 +00:00
2015-03-06 11:15:52 +00:00
//Detect column sizes
ulong colSize[];
colSize.length = tree.getNColumns;
foreach(i ; 0..tree.getNColumns)
colSize[i] = tree.getColumn(i).getTitle.length +1;//+1 space
TreeIter it = new TreeIter();
if(store.getIterFirst(it)){
do{
import std.math : log10;
int size0 = log10(store.getValueInt(it, 0)+1).to!int +1;//+1 space
if(size0>colSize[0])
colSize[0] = size0;
foreach(i ; 1..store.getNColumns()){
int size = store.getValueString(it, GetColumnStoreIndex(tree, i)).length.to!int +3;//+2 double quotes added, +1 space
if(size>colSize[i])
colSize[i] = size;
}
}while(store.iterNext(it));
}
//Write file
2014-11-18 14:20:39 +00:00
auto file = File(openedFile, "w");
foreach(i ; 0..tree.getNColumns)
2015-03-06 11:15:52 +00:00
file.write(leftJustify(tree.getColumn(i).getTitle, colSize[i]));
2014-11-18 14:20:39 +00:00
file.write("\n");
2015-03-06 11:15:52 +00:00
it = new TreeIter();
2014-11-18 13:41:47 +00:00
if(store.getIterFirst(it)){
do{
2015-03-06 11:15:52 +00:00
file.write(leftJustify(store.getValueInt(it, 0).to!string, colSize[0]));
2014-11-18 13:41:47 +00:00
foreach(i ; 1..store.getNColumns()){
2015-03-06 11:15:52 +00:00
file.write(leftJustify("\""~store.getValueString(it, GetColumnStoreIndex(tree, i))~"\"", colSize[i]));
2014-11-18 13:41:47 +00:00
}
2014-11-18 14:20:39 +00:00
file.write("\n");
2014-11-18 13:41:47 +00:00
}while(store.iterNext(it));
}
2014-11-19 15:24:46 +00:00
file.flush();
file.close();
SaySomething("Saved to "~openedFile);
2014-11-18 10:46:07 +00:00
}
else
2014-11-19 15:24:46 +00:00
SaySomething("Nothing to save !");
2014-11-18 10:46:07 +00:00
}
2014-11-19 15:52:06 +00:00
void SetTitle(string title){
2014-11-19 17:13:29 +00:00
version(Windows) window.setTitle(title);
2014-11-19 15:52:06 +00:00
else{
import gtk.HeaderBar;
(cast(HeaderBar)header).setSubtitle(title);
}
}
2014-11-19 17:13:29 +00:00
__gshared string openedFile;
2014-11-19 15:52:06 +00:00
void Open(string file, ref TreeView tree){
2014-11-18 10:46:07 +00:00
auto twoda = new TwoDA(file);
2014-11-18 14:20:39 +00:00
openedFile = file;
2014-11-19 15:52:06 +00:00
SetTitle(openedFile);
2014-11-18 10:46:07 +00:00
//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[];
2014-11-18 13:23:38 +00:00
type~=GType.INT;
foreach(i;1..twoda.header.length)type~=GType.STRING;
2014-11-18 10:46:07 +00:00
//Create new store
auto store = new ListStore(type);
tree.setModel(store);
2014-11-17 14:49:53 +00:00
//Setup TreeView columns
2014-11-16 22:48:56 +00:00
foreach(index, s ; twoda.header){
auto col = SetupColumn(tree, s, index);
2014-11-16 22:48:56 +00:00
tree.appendColumn(col);
}
2014-11-17 14:49:53 +00:00
//Fill database
2014-11-17 10:50:11 +00:00
TreeIter iter = new TreeIter();
2014-11-16 22:48:56 +00:00
for(int i=0 ; i<=twoda.lastLine ; i++){
2014-11-17 10:50:11 +00:00
store.append(iter);
2014-11-16 22:48:56 +00:00
store.setValue(iter, 0, i);
if(i in twoda.values){
foreach(index, v ; twoda.values[i]){
2014-11-17 10:50:11 +00:00
store.setValue(iter, cast(int)index+1, v);
2014-11-16 22:48:56 +00:00
}
}
else{
foreach(index ; 1..twoda.header.length){
2014-11-17 10:50:11 +00:00
store.setValue(iter, cast(int)index+1, "_");
2014-11-16 22:48:56 +00:00
}
}
}
2014-11-17 14:49:53 +00:00
//Autosize columns
2014-11-16 22:48:56 +00:00
tree.columnsAutosize();
2014-11-18 13:06:26 +00:00
tree.setSizeRequest(50, 50);
2014-11-16 22:48:56 +00:00
}
//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){
2014-11-18 16:10:05 +00:00
cr.setProperty("background-rgba", cast(ulong)(new GdkRGBA(0.36, 0.13, 0.4, 0.5)));
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){
2014-11-19 15:24:46 +00:00
SaySomething("Not a number !");
}
});
}
else{
cr.addOnEdited((string path, string newval, CellRendererText crt){
TreeIter t = new TreeIter(tree.getModel(), path);
2014-11-19 15:31:17 +00:00
if(newval.countchars("\"")!=0){
SaySomething("Double quotes are forbidden !");
}
else
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);
2014-11-19 13:11:53 +00:00
col.setData("colnumber", cast(void*)cast(int)index);
col.setResizable(true);
col.setMinWidth(10);
col.setClickable(true);
2014-11-19 13:11:53 +00:00
col.setReorderable(true);
col.addOnClicked((TreeViewColumn col){
2014-11-20 08:08:14 +00:00
import gtk.Dialog;
auto dlg = new Dialog("Column options", window, GtkDialogFlags.MODAL, ["Close"], [ResponseType.CANCEL]);
//Rename
dlg.getContentArea.packStart(new Label("Rename:"), false, false, 0);
auto renamebox = new HBox(false, 0);
dlg.getContentArea.packStart(renamebox, false, false, 0);
auto renameentry = new Entry(col.getTitle);
version(Windows) auto renamebutton = new Button(StockID.APPLY, true);
2015-03-06 11:15:52 +00:00
else auto renamebutton = new Button("object-select-symbolic", GtkIconSize.SMALL_TOOLBAR);
2014-11-20 08:08:14 +00:00
renamebutton.addOnClicked((Button){
auto newname = renameentry.getText.strip;
if(newname.countchars(" \t\n\r")==0)
col.setTitle(newname);
else
SaySomething("Spaces are forbidden in column name");
});
renamebox.packStart(renameentry, true, true, 0);
renamebox.packEnd(renamebutton, false, false, 0);
//Delete
auto deletebox = new HBox(false, 0);
dlg.getContentArea.packStart(deletebox, false, false, 0);
version(Windows) auto deletebutton = new Button(StockID.DELETE, true);
2015-03-06 11:15:52 +00:00
else auto deletebutton = new Button("user-trash-symbolic", GtkIconSize.SMALL_TOOLBAR);
2014-11-20 08:08:14 +00:00
deletebutton.addOnClicked((Button){
tree.removeColumn(col);
dlg.destroy();
});
deletebox.packStart(new Label("Delete column"), true, true, 0);
deletebox.packEnd(deletebutton, false, false, 0);
dlg.showAll();
dlg.run();
dlg.destroy();
});
return col;
}
2014-11-16 22:48:56 +00:00
class TwoDA{
import std.regex;
import std.file;
this(string filepath){
lastLine = 0;
foreach(index, line ; readText(filepath).splitLines()){
string data[];
auto results = matchAll(line, rgxField);
foreach(res ; results){
string s;
if(res[0][0]=='"') data~= res[2];
else data~= res[1];
}
if(index==0){
header = data;
//writeln(header);
}
else{
int nLine = data[0].to!int;
values[nLine] = data[1..$];
//writeln(values[nLine]);
if(nLine > lastLine)
lastLine = nLine;
}
}
}
string[] header;
string[][uint] values;
uint lastLine;
enum rgxField = ctRegex!"(?:\\b([^\\s]+?)\\b|\"([^\"]+?)\")";
}