Save as & global header

This commit is contained in:
Crom (Thibaut CHARLES) 2014-11-19 16:52:06 +01:00
parent 04f9086315
commit 22f249b10a

View File

@ -24,6 +24,7 @@ import gtk.HBox;
__gshared MainWindow window;
__gshared HBox statusbar;
__gshared Object header;
void main(string[] args)
{
@ -41,6 +42,7 @@ void main(string[] args)
version(Windows){
auto buttonSave = new Button(StockID.SAVE, true);
auto buttonSaveAs = new Button(StockID.SAVE_AS, true);
auto buttonOpen = new Button(StockID.OPEN, true);
auto buttonInsert = new Button(StockID.JUMP_TO, true);
auto buttonDelete = new Button(StockID.DELETE, true);
@ -49,6 +51,7 @@ void main(string[] args)
}
else{
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 buttonInsert = new Button("format-text-direction-ltr-symbolic", GtkIconSize.SMALL_TOOLBAR);
auto buttonDelete = new Button("user-trash-symbolic", GtkIconSize.SMALL_TOOLBAR);
@ -56,35 +59,40 @@ void main(string[] args)
auto buttonNewCol = new Button("tab-new-symbolic", GtkIconSize.SMALL_TOOLBAR);
}
buttonSave.setTooltipText("Save");
enum GDK_KEY_S = 0x053;
buttonSave.addAccelerator("clicked", accel, GDK_KEY_S, GdkModifierType.CONTROL_MASK, GtkAccelFlags.VISIBLE);
buttonSaveAs.setTooltipText("Save as");
buttonOpen.setTooltipText("Open 2DA");
buttonInsert.setTooltipText("Insert row after");
buttonDelete.setTooltipText("Delete row");
buttonRenumber.setTooltipText("Renumber all rows");
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){
//Menu bar
import gtk.HBox;
auto cont2 = new HBox(false, 0);
cont.packStart(cont2, false, false, 0);
header = new HBox(false, 0);
cont.packStart(header, false, false, 0);
cont2.packStart(buttonOpen, false, false, 0);
cont2.packEnd(buttonSave, false, false, 0);
header.packStart(buttonOpen, false, false, 0);
header.packEnd(buttonSaveAs, false, false, 0);
header.packEnd(buttonSave, false, false, 0);
}
else{
//Header bar
import gtk.HeaderBar;
auto header = new HeaderBar();
window.setTitlebar(header);
header.setTitle("2DAEdit");
header.setProperty("show-close-button", true);
header = new HeaderBar();
window.setTitlebar(cast(HeaderBar)header);
(cast(HeaderBar)header).setTitle("2DAEdit");
(cast(HeaderBar)header).setProperty("show-close-button", true);
header.packStart(buttonOpen);
header.packEnd(buttonSave);
(cast(HeaderBar)header).packStart(buttonOpen);
(cast(HeaderBar)header).packEnd(buttonSaveAs);
(cast(HeaderBar)header).packEnd(buttonSave);
}
//Status bar
@ -128,6 +136,18 @@ void main(string[] args)
buttonSave.addOnClicked((Button){
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){
import gtk.Dialog;
@ -137,8 +157,7 @@ void main(string[] args)
auto res = fc.run();
if(res==GtkResponseType.OK){
string filename = fc.getFilename();
version(Windows) Open(filename, tree, cast(Object)window);
else Open(filename, tree, cast(Object)header);
Open(filename, tree);
}
fc.destroy();
});
@ -230,8 +249,7 @@ void main(string[] args)
//Open if exists
if(args.length>=2 && exists(args[1])){
version(Windows) Open(args[1], tree, cast(Object)window);
else Open(args[1], tree, cast(Object)header);
Open(args[1], tree);
}
window.showAll();
@ -273,9 +291,13 @@ void SaySomething(string msg){
}).start();
}
void Save(ref TreeView tree){
void Save(ref TreeView tree, string newpath=""){
auto store = cast(ListStore)tree.getModel();
if(store !is null){
if(newpath!=""){
openedFile = newpath;
SetTitle(openedFile);
}
auto file = File(openedFile, "w");
@ -303,18 +325,21 @@ void Save(ref TreeView tree){
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;
void Open(string file, ref TreeView tree, Object header){
void Open(string file, ref TreeView tree){
auto twoda = new TwoDA(file);
openedFile = file;
version(Windows) (cast(MainWindow)header).setTitle(file);
else{
import gtk.HeaderBar;
(cast(HeaderBar)header).setSubtitle(file);
}
SetTitle(openedFile);
//Delete old store
auto oldstore = cast(ListStore)tree.getModel();