Merge branch 'master' of git.thibautcharles.net:akadoc/2daedit
This commit is contained in:
commit
3bdcd54e78
196
source/app.d
196
source/app.d
@ -18,10 +18,13 @@ import gtk.VBox;
|
|||||||
import gtk.Statusbar;
|
import gtk.Statusbar;
|
||||||
import gtk.Button;
|
import gtk.Button;
|
||||||
import gtk.ScrolledWindow;
|
import gtk.ScrolledWindow;
|
||||||
|
import gtk.AccelGroup;
|
||||||
|
|
||||||
import gtk.HBox;
|
import gtk.HBox;
|
||||||
|
|
||||||
MainWindow window;
|
__gshared MainWindow window;
|
||||||
|
__gshared HBox statusbar;
|
||||||
|
__gshared Object header;
|
||||||
|
|
||||||
void main(string[] args)
|
void main(string[] args)
|
||||||
{
|
{
|
||||||
@ -30,12 +33,16 @@ void main(string[] args)
|
|||||||
//Window
|
//Window
|
||||||
window = new MainWindow("2DA-Edit");
|
window = new MainWindow("2DA-Edit");
|
||||||
|
|
||||||
|
auto accel = new AccelGroup();
|
||||||
|
window.addAccelGroup(accel);
|
||||||
|
|
||||||
auto cont = new VBox(false, 0);
|
auto cont = new VBox(false, 0);
|
||||||
window.add(cont);
|
window.add(cont);
|
||||||
cont.setSizeRequest(300, 200);
|
cont.setSizeRequest(300, 200);
|
||||||
|
|
||||||
version(Windows){
|
version(Windows){
|
||||||
auto buttonSave = new Button(StockID.SAVE, true);
|
auto buttonSave = new Button(StockID.SAVE, true);
|
||||||
|
auto buttonSaveAs = new Button(StockID.SAVE_AS, true);
|
||||||
auto buttonOpen = new Button(StockID.OPEN, true);
|
auto buttonOpen = new Button(StockID.OPEN, true);
|
||||||
auto buttonInsert = new Button(StockID.JUMP_TO, true);
|
auto buttonInsert = new Button(StockID.JUMP_TO, true);
|
||||||
auto buttonDelete = new Button(StockID.DELETE, true);
|
auto buttonDelete = new Button(StockID.DELETE, true);
|
||||||
@ -44,6 +51,7 @@ void main(string[] args)
|
|||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
auto buttonSave = new Button("document-save-symbolic", GtkIconSize.MENU);
|
auto buttonSave = new Button("document-save-symbolic", GtkIconSize.MENU);
|
||||||
|
auto buttonSaveAs = new Button("document-save-as-symbolic", GtkIconSize.MENU);
|
||||||
auto buttonOpen = new Button("document-open-symbolic", GtkIconSize.MENU);
|
auto buttonOpen = new Button("document-open-symbolic", GtkIconSize.MENU);
|
||||||
auto buttonInsert = new Button("format-text-direction-ltr-symbolic", GtkIconSize.SMALL_TOOLBAR);
|
auto buttonInsert = new Button("format-text-direction-ltr-symbolic", GtkIconSize.SMALL_TOOLBAR);
|
||||||
auto buttonDelete = new Button("user-trash-symbolic", GtkIconSize.SMALL_TOOLBAR);
|
auto buttonDelete = new Button("user-trash-symbolic", GtkIconSize.SMALL_TOOLBAR);
|
||||||
@ -51,37 +59,44 @@ void main(string[] args)
|
|||||||
auto buttonNewCol = new Button("tab-new-symbolic", GtkIconSize.SMALL_TOOLBAR);
|
auto buttonNewCol = new Button("tab-new-symbolic", GtkIconSize.SMALL_TOOLBAR);
|
||||||
}
|
}
|
||||||
buttonSave.setTooltipText("Save");
|
buttonSave.setTooltipText("Save");
|
||||||
|
buttonSaveAs.setTooltipText("Save as");
|
||||||
buttonOpen.setTooltipText("Open 2DA");
|
buttonOpen.setTooltipText("Open 2DA");
|
||||||
buttonInsert.setTooltipText("Insert row after");
|
buttonInsert.setTooltipText("Insert row after");
|
||||||
buttonDelete.setTooltipText("Delete row");
|
buttonDelete.setTooltipText("Delete row");
|
||||||
buttonRenumber.setTooltipText("Renumber all rows");
|
buttonRenumber.setTooltipText("Renumber all rows");
|
||||||
buttonNewCol.setTooltipText("Add new column");
|
buttonNewCol.setTooltipText("Add new column");
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
version(Windows){
|
version(Windows){
|
||||||
//Menu bar
|
//Menu bar
|
||||||
import gtk.HBox;
|
import gtk.HBox;
|
||||||
|
|
||||||
auto cont2 = new HBox(false, 0);
|
header = new HBox(false, 0);
|
||||||
cont.packStart(cont2, false, false, 0);
|
cont.packStart(header, false, false, 0);
|
||||||
|
|
||||||
cont2.packStart(buttonOpen, false, false, 0);
|
header.packStart(buttonOpen, false, false, 0);
|
||||||
cont2.packEnd(buttonSave, false, false, 0);
|
header.packEnd(buttonSaveAs, false, false, 0);
|
||||||
|
header.packEnd(buttonSave, false, false, 0);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
//Header bar
|
//Header bar
|
||||||
import gtk.HeaderBar;
|
import gtk.HeaderBar;
|
||||||
|
|
||||||
auto header = new HeaderBar();
|
header = new HeaderBar();
|
||||||
window.setTitlebar(header);
|
window.setTitlebar(cast(HeaderBar)header);
|
||||||
header.setTitle("2DAEdit");
|
(cast(HeaderBar)header).setTitle("2DAEdit");
|
||||||
header.setProperty("show-close-button", true);
|
(cast(HeaderBar)header).setProperty("show-close-button", true);
|
||||||
|
|
||||||
header.packStart(buttonOpen);
|
(cast(HeaderBar)header).packStart(buttonOpen);
|
||||||
header.packEnd(buttonSave);
|
(cast(HeaderBar)header).packEnd(buttonSaveAs);
|
||||||
|
(cast(HeaderBar)header).packEnd(buttonSave);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Status bar
|
//Status bar
|
||||||
auto statusbar = new HBox(false, 0);
|
statusbar = new HBox(false, 0);
|
||||||
cont.packEnd(statusbar, false, false, 5);
|
cont.packEnd(statusbar, false, false, 5);
|
||||||
statusbar.packStart(buttonRenumber, false, false, 5);
|
statusbar.packStart(buttonRenumber, false, false, 5);
|
||||||
statusbar.packStart(buttonInsert, false, false, 5);
|
statusbar.packStart(buttonInsert, false, false, 5);
|
||||||
@ -101,12 +116,38 @@ void main(string[] args)
|
|||||||
tree.setProperty("tooltip-column", 0);
|
tree.setProperty("tooltip-column", 0);
|
||||||
tree.setProperty("reorderable", true);
|
tree.setProperty("reorderable", true);
|
||||||
tree.setProperty("headers-clickable", true);
|
tree.setProperty("headers-clickable", true);
|
||||||
|
tree.addOnColumnsChanged((TreeView tree){
|
||||||
|
auto store = cast(ListStore)tree.getModel();
|
||||||
|
|
||||||
|
int si = GetColumnStoreIndex(tree, 0);
|
||||||
|
if(si>=0 && store.getColumnType(si) != GType.INT){
|
||||||
|
foreach(i ; 0..store.getNColumns()){
|
||||||
|
si = GetColumnStoreIndex(tree, i);
|
||||||
|
if(si>=0 && store.getColumnType(si)==GType.INT){
|
||||||
|
tree.moveColumnAfter(tree.getColumn(i), null);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
//Configure button callbacks
|
//Configure button callbacks
|
||||||
buttonSave.addOnClicked((Button){
|
buttonSave.addOnClicked((Button){
|
||||||
Save(tree);
|
Save(tree);
|
||||||
});
|
});
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
|
||||||
buttonOpen.addOnClicked((Button){
|
buttonOpen.addOnClicked((Button){
|
||||||
import gtk.Dialog;
|
import gtk.Dialog;
|
||||||
@ -116,8 +157,7 @@ void main(string[] args)
|
|||||||
auto res = fc.run();
|
auto res = fc.run();
|
||||||
if(res==GtkResponseType.OK){
|
if(res==GtkResponseType.OK){
|
||||||
string filename = fc.getFilename();
|
string filename = fc.getFilename();
|
||||||
version(Windows) Open(filename, tree, cast(Object)window);
|
Open(filename, tree);
|
||||||
else Open(filename, tree, cast(Object)header);
|
|
||||||
}
|
}
|
||||||
fc.destroy();
|
fc.destroy();
|
||||||
});
|
});
|
||||||
@ -159,37 +199,57 @@ void main(string[] args)
|
|||||||
});
|
});
|
||||||
|
|
||||||
buttonNewCol.addOnClicked((Button){
|
buttonNewCol.addOnClicked((Button){
|
||||||
auto store = cast(ListStore)tree.getModel();
|
auto oldstore = cast(ListStore)tree.getModel();
|
||||||
int newColIndex = store.getNColumns();
|
int newColIndex = oldstore.getNColumns();
|
||||||
|
|
||||||
|
|
||||||
GType[] types;
|
GType[] types;
|
||||||
foreach(i ; 0..newColIndex+1)
|
string[] titles;
|
||||||
types~= store.getColumnType(i);
|
foreach(i ; 0..newColIndex){
|
||||||
store.setColumnTypes(types);
|
if(i==0)types~= GType.INT;
|
||||||
|
else types~= GType.STRING;
|
||||||
|
titles~= tree.getColumn(0).getTitle;
|
||||||
|
tree.removeColumn(tree.getColumn(0));
|
||||||
|
}
|
||||||
|
types~=GType.STRING;
|
||||||
|
titles~="new_col";
|
||||||
|
|
||||||
writeln(types.length);
|
auto store = new ListStore(types);
|
||||||
|
tree.setModel(store);
|
||||||
|
|
||||||
|
//Fill them
|
||||||
|
TreeIter oldit = new TreeIter();
|
||||||
|
TreeIter newit = new TreeIter();
|
||||||
|
if(oldstore.getIterFirst(oldit)){
|
||||||
|
do{
|
||||||
|
store.append(newit);
|
||||||
|
|
||||||
//auto col = SetupColumn(tree, "new_col", newColIndex);
|
foreach(i ; 0..newColIndex+1){
|
||||||
|
if(i<newColIndex){
|
||||||
|
if(types[i]==GType.INT) store.setValue(newit, cast(int)i, oldstore.getValueInt(oldit, i));
|
||||||
|
else store.setValue(newit, cast(int)i, oldstore.getValueString(oldit, i));
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
store.setValue(newit, cast(int)i, "_");
|
||||||
|
}
|
||||||
|
|
||||||
//TreeIter it = new TreeIter();
|
}while(oldstore.iterNext(oldit));
|
||||||
//if(store.getIterFirst(it)){
|
}
|
||||||
// do{
|
|
||||||
// store.setValue(it, newColIndex, "_");
|
|
||||||
// }while(store.iterNext(it));
|
|
||||||
//}
|
|
||||||
|
|
||||||
//tree.appendColumn(col);
|
//setup cols
|
||||||
|
foreach(i ; 0..newColIndex+1){
|
||||||
|
tree.appendColumn(SetupColumn(tree, titles[i], i));
|
||||||
|
}
|
||||||
|
|
||||||
|
oldstore.destroy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Open if exists
|
//Open if exists
|
||||||
if(args.length>=2 && exists(args[1])){
|
if(args.length>=2 && exists(args[1])){
|
||||||
version(Windows) Open(args[1], tree, cast(Object)window);
|
Open(args[1], tree);
|
||||||
else Open(args[1], tree, cast(Object)header);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
window.showAll();
|
window.showAll();
|
||||||
@ -198,9 +258,46 @@ void main(string[] args)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Save(ref TreeView tree){
|
int GetColumnStoreIndex(TreeView tree, int colindex){
|
||||||
|
auto col = tree.getColumn(colindex);
|
||||||
|
if(col !is null)
|
||||||
|
return cast(int)(col.getData("colnumber"));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
//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));
|
||||||
|
}
|
||||||
|
//Destroy
|
||||||
|
lbl.destroy();
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Save(ref TreeView tree, string newpath=""){
|
||||||
auto store = cast(ListStore)tree.getModel();
|
auto store = cast(ListStore)tree.getModel();
|
||||||
if(store !is null){
|
if(store !is null){
|
||||||
|
if(newpath!=""){
|
||||||
|
openedFile = newpath;
|
||||||
|
SetTitle(openedFile);
|
||||||
|
}
|
||||||
|
|
||||||
auto file = File(openedFile, "w");
|
auto file = File(openedFile, "w");
|
||||||
|
|
||||||
@ -213,33 +310,36 @@ void Save(ref TreeView tree){
|
|||||||
do{
|
do{
|
||||||
file.write(store.getValueInt(it, 0));
|
file.write(store.getValueInt(it, 0));
|
||||||
foreach(i ; 1..store.getNColumns()){
|
foreach(i ; 1..store.getNColumns()){
|
||||||
file.write("\t\"",store.getValueString(it, i),"\"");
|
file.write("\t\"", store.getValueString(it, GetColumnStoreIndex(tree, i)), "\"");
|
||||||
}
|
}
|
||||||
file.write("\n");
|
file.write("\n");
|
||||||
}while(store.iterNext(it));
|
}while(store.iterNext(it));
|
||||||
|
}
|
||||||
|
|
||||||
file.flush();
|
file.flush();
|
||||||
file.close();
|
file.close();
|
||||||
writeln("File written: ",openedFile);
|
|
||||||
}
|
SaySomething("Saved to "~openedFile);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
writeln("Nothing to save !");
|
SaySomething("Nothing to save !");
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetTitle(string title){
|
||||||
|
version(Windows) (cast(MainWindow)header).setTitle(title);
|
||||||
|
else{
|
||||||
|
import gtk.HeaderBar;
|
||||||
|
(cast(HeaderBar)header).setSubtitle(title);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
string openedFile;
|
string openedFile;
|
||||||
void Open(string file, ref TreeView tree, Object header){
|
void Open(string file, ref TreeView tree){
|
||||||
|
|
||||||
auto twoda = new TwoDA(file);
|
auto twoda = new TwoDA(file);
|
||||||
|
|
||||||
openedFile = file;
|
openedFile = file;
|
||||||
|
SetTitle(openedFile);
|
||||||
version(Windows) (cast(MainWindow)header).setTitle(file);
|
|
||||||
else{
|
|
||||||
import gtk.HeaderBar;
|
|
||||||
(cast(HeaderBar)header).setSubtitle(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Delete old store
|
//Delete old store
|
||||||
auto oldstore = cast(ListStore)tree.getModel();
|
auto oldstore = cast(ListStore)tree.getModel();
|
||||||
@ -300,7 +400,7 @@ auto ref SetupColumn(TreeView tree, string sName, size_t index){
|
|||||||
CellRendererText cr = new CellRendererText();
|
CellRendererText cr = new CellRendererText();
|
||||||
cr.setProperty("editable", true);
|
cr.setProperty("editable", true);
|
||||||
if(index==0){
|
if(index==0){
|
||||||
cr.setProperty("background-rgba", cast(ulong)(new GdkRGBA(0.36, 0.13, 0.4, 1.0)));
|
cr.setProperty("background-rgba", cast(ulong)(new GdkRGBA(0.36, 0.13, 0.4, 0.5)));
|
||||||
cr.setProperty("background-set", true);
|
cr.setProperty("background-set", true);
|
||||||
|
|
||||||
cr.addOnEdited((string path, string newval, CellRendererText crt){
|
cr.addOnEdited((string path, string newval, CellRendererText crt){
|
||||||
@ -310,7 +410,7 @@ auto ref SetupColumn(TreeView tree, string sName, size_t index){
|
|||||||
store.setValue(t, cast(int)crt.getData("colnumber"), n);
|
store.setValue(t, cast(int)crt.getData("colnumber"), n);
|
||||||
}
|
}
|
||||||
catch(Exception e){
|
catch(Exception e){
|
||||||
writeln("Not a number");
|
SaySomething("Not a number !");
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
@ -318,6 +418,10 @@ auto ref SetupColumn(TreeView tree, string sName, size_t index){
|
|||||||
else{
|
else{
|
||||||
cr.addOnEdited((string path, string newval, CellRendererText crt){
|
cr.addOnEdited((string path, string newval, CellRendererText crt){
|
||||||
TreeIter t = new TreeIter(tree.getModel(), path);
|
TreeIter t = new TreeIter(tree.getModel(), path);
|
||||||
|
if(newval.countchars("\"")!=0){
|
||||||
|
SaySomething("Double quotes are forbidden !");
|
||||||
|
}
|
||||||
|
else
|
||||||
store.setValue(t, cast(int)crt.getData("colnumber"), newval);
|
store.setValue(t, cast(int)crt.getData("colnumber"), newval);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -326,9 +430,11 @@ auto ref SetupColumn(TreeView tree, string sName, size_t index){
|
|||||||
|
|
||||||
|
|
||||||
auto col = new TreeViewColumn(sName, cr, "text", cast(int)index);
|
auto col = new TreeViewColumn(sName, cr, "text", cast(int)index);
|
||||||
|
col.setData("colnumber", cast(void*)cast(int)index);
|
||||||
col.setResizable(true);
|
col.setResizable(true);
|
||||||
col.setMinWidth(10);
|
col.setMinWidth(10);
|
||||||
col.setClickable(true);
|
col.setClickable(true);
|
||||||
|
col.setReorderable(true);
|
||||||
col.addOnClicked((TreeViewColumn col){
|
col.addOnClicked((TreeViewColumn col){
|
||||||
import gtk.Dialog;
|
import gtk.Dialog;
|
||||||
auto dlg = new Dialog("Rename column", window, GtkDialogFlags.MODAL, ["Cancel","Rename"], [ResponseType.CANCEL, ResponseType.OK]);
|
auto dlg = new Dialog("Rename column", window, GtkDialogFlags.MODAL, ["Cancel","Rename"], [ResponseType.CANCEL, ResponseType.OK]);
|
||||||
@ -341,7 +447,7 @@ auto ref SetupColumn(TreeView tree, string sName, size_t index){
|
|||||||
if(newname.countchars(" \t\n\r")==0)
|
if(newname.countchars(" \t\n\r")==0)
|
||||||
col.setTitle(newname);
|
col.setTitle(newname);
|
||||||
else
|
else
|
||||||
writeln("Unauthorized caracters in column name");
|
SaySomething("Spaces are forbidden in column name");
|
||||||
}
|
}
|
||||||
|
|
||||||
dlg.destroy();
|
dlg.destroy();
|
||||||
|
Loading…
Reference in New Issue
Block a user