@ -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 ) ;
}
//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 ) ; } ) ;
header . packStart ( buttonOpen ) ;
header . packEnd ( buttonSave ) ;
}
//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 ) ) ;
}