From a8014a855bc511f56846cd02b150e34b12065e17 Mon Sep 17 00:00:00 2001 From: Eric House Date: Sat, 5 Jan 2013 19:40:29 -0800 Subject: [PATCH] now games are displayed in lists. Next: open them. --- xwords4/linux/gamesdb.c | 40 +++++++++++++++++++++++++++++++++ xwords4/linux/gamesdb.h | 8 +++++++ xwords4/linux/gtkaskdict.c | 4 ++-- xwords4/linux/gtkmain.c | 46 ++++++++++++++++++++++++++++++-------- xwords4/linux/main.h | 5 +++++ 5 files changed, 92 insertions(+), 11 deletions(-) diff --git a/xwords4/linux/gamesdb.c b/xwords4/linux/gamesdb.c index 105d45999..de8589ae3 100644 --- a/xwords4/linux/gamesdb.c +++ b/xwords4/linux/gamesdb.c @@ -88,3 +88,43 @@ writeToDB( XWStreamCtxt* stream, void* closure ) sqlite3_finalize( stmt ); } } + +GSList* +listGames( GTKGamesGlobals* gg ) +{ + GSList* list = NULL; + + sqlite3_stmt *ppStmt; + int result = sqlite3_prepare_v2( gg->pDb, + "SELECT rowid FROM games ORDER BY rowid", -1, + &ppStmt, NULL ); + XP_ASSERT( SQLITE_OK == result ); + while ( NULL != ppStmt ) { + switch( sqlite3_step( ppStmt ) ) { + case SQLITE_ROW: /* have data */ + { + sqlite3_int64* data = g_malloc( sizeof( *data ) ); + *data = sqlite3_column_int64( ppStmt, 0 ); + XP_LOGF( "%s: got a row; id=%lld", __func__, *data ); + list = g_slist_append( list, data ); + } + break; + case SQLITE_DONE: + sqlite3_finalize( ppStmt ); + ppStmt = NULL; + break; + default: + XP_ASSERT( 0 ); + break; + } + } + return list; +} + +void +getGameName( GTKGamesGlobals* XP_UNUSED(gg), const sqlite3_int64* rowid, + XP_UCHAR* buf, XP_U16 len ) +{ + snprintf( buf, len, "Game %lld", *rowid ); + LOG_RETURNF( "%s", buf ); +} diff --git a/xwords4/linux/gamesdb.h b/xwords4/linux/gamesdb.h index 5a7c6c15b..5a6a4d067 100644 --- a/xwords4/linux/gamesdb.h +++ b/xwords4/linux/gamesdb.h @@ -22,7 +22,9 @@ #define _GAMESDB_H_ #include +#include +#include "main.h" #include "comtypes.h" sqlite3* openGamesDB( void ); @@ -30,4 +32,10 @@ void closeGamesDB( sqlite3* dbp ); void writeToDB( XWStreamCtxt* stream, void* closure ); +/* Return GSList whose data is (ptrs to) rowids */ +GSList* listGames( GTKGamesGlobals* gg ); +void getGameName( GTKGamesGlobals* gg, const sqlite3_int64* rowid, + XP_UCHAR* buf, XP_U16 len ); + + #endif diff --git a/xwords4/linux/gtkaskdict.c b/xwords4/linux/gtkaskdict.c index 565c662c3..d635d595b 100644 --- a/xwords4/linux/gtkaskdict.c +++ b/xwords4/linux/gtkaskdict.c @@ -41,9 +41,9 @@ init_list( GtkWidget* list ) } static void -add_to_list( GtkWidget *list, const gchar *str ) +add_to_list( GtkWidget* list, const gchar* str ) { - GtkListStore *store = + GtkListStore* store = GTK_LIST_STORE( gtk_tree_view_get_model(GTK_TREE_VIEW(list))); GtkTreeIter iter; gtk_list_store_append( store, &iter ); diff --git a/xwords4/linux/gtkmain.c b/xwords4/linux/gtkmain.c index 4560e3641..dfcc374f8 100644 --- a/xwords4/linux/gtkmain.c +++ b/xwords4/linux/gtkmain.c @@ -25,26 +25,45 @@ #include "gtkboard.h" #include "linuxmain.h" -enum { ROW_ITEM, N_ITEMS }; - -typedef struct _GTKGamesGlobals { - sqlite3* pDb; - LaunchParams* params; -} GTKGamesGlobals; +enum { ROW_ITEM, NAME_ITEM, N_ITEMS }; static void init_games_list( GtkWidget* list ) { GtkCellRenderer* renderer = gtk_cell_renderer_text_new(); GtkTreeViewColumn* column = - gtk_tree_view_column_new_with_attributes( "Games", renderer, - "row", ROW_ITEM, NULL ); + gtk_tree_view_column_new_with_attributes( "Row", renderer, "text", + ROW_ITEM, NULL ); gtk_tree_view_append_column( GTK_TREE_VIEW(list), column ); - GtkListStore* store = gtk_list_store_new( N_ITEMS, G_TYPE_STRING ); + + renderer = gtk_cell_renderer_text_new(); + column = gtk_tree_view_column_new_with_attributes( "Name", renderer, "text", + NAME_ITEM, NULL ); + gtk_tree_view_append_column( GTK_TREE_VIEW(list), column ); + + GtkListStore* store = gtk_list_store_new( N_ITEMS, // G_TYPE_INT64, + G_TYPE_STRING, G_TYPE_STRING ); gtk_tree_view_set_model( GTK_TREE_VIEW(list), GTK_TREE_MODEL(store) ); g_object_unref( store ); } +static void +add_to_list( GtkWidget* list, sqlite3_int64* rowid, const gchar* str ) +{ + GtkListStore* store = + GTK_LIST_STORE( gtk_tree_view_get_model(GTK_TREE_VIEW(list))); + GtkTreeIter iter; + gtk_list_store_append( store, &iter ); + XP_LOGF( "adding %lld, %s", *rowid, str ); + gchar buf[16]; + snprintf( buf, sizeof(buf), "%lld", *rowid ); + gtk_list_store_set( store, &iter, + ROW_ITEM, buf, + NAME_ITEM, str, + -1 ); + XP_LOGF( "DONE adding" ); +} + static void handle_newgame_button( GtkWidget* XP_UNUSED(widget), void* closure ) { @@ -97,6 +116,15 @@ makeGamesWindow( GTKGamesGlobals* gg ) init_games_list( list ); gtk_widget_show( list ); + GSList* games = listGames( gg ); + for ( GSList* iter = games; !!iter; iter = iter->next ) { + XP_UCHAR name[128]; + sqlite3_int64* rowid = (sqlite3_int64*)iter->data; + getGameName( gg, rowid, name, VSIZE(name) ); + add_to_list( list, rowid, name ); + } + g_slist_free( games ); + GtkWidget* hbox = gtk_hbox_new( FALSE, 0 ); gtk_widget_show( hbox ); gtk_container_add( GTK_CONTAINER(vbox), hbox ); diff --git a/xwords4/linux/main.h b/xwords4/linux/main.h index ecf34680d..bb8af4fa0 100644 --- a/xwords4/linux/main.h +++ b/xwords4/linux/main.h @@ -209,4 +209,9 @@ struct CommonGlobals { XP_U16 curSaveToken; }; +typedef struct _GTKGamesGlobals { + sqlite3* pDb; + LaunchParams* params; +} GTKGamesGlobals; + #endif