2013-01-07 15:10:44 +01:00
|
|
|
/* -*- compile-command: "make MEMDEBUG=TRUE -j3"; -*- */
|
2003-11-01 06:35:29 +01:00
|
|
|
/*
|
2013-01-06 01:08:19 +01:00
|
|
|
* Copyright 2000-2013 by Eric House (xwords@eehouse.org). All rights
|
|
|
|
* reserved.
|
2003-11-01 06:35:29 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef PLATFORM_GTK
|
|
|
|
|
2013-01-06 01:08:19 +01:00
|
|
|
#include "main.h"
|
|
|
|
#include "gamesdb.h"
|
|
|
|
#include "gtkboard.h"
|
|
|
|
#include "linuxmain.h"
|
2011-08-30 05:36:01 +02:00
|
|
|
|
2013-01-07 15:36:34 +01:00
|
|
|
static void
|
|
|
|
recordOpened( GTKGamesGlobals* gg, GtkAppGlobals* globals )
|
|
|
|
{
|
|
|
|
gg->globalsList = g_slist_prepend( gg->globalsList, globals );
|
|
|
|
globals->gg = gg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
recordClosed( GTKGamesGlobals* gg, GtkAppGlobals* globals )
|
|
|
|
{
|
|
|
|
gg->globalsList = g_slist_remove( gg->globalsList, globals );
|
|
|
|
}
|
|
|
|
|
|
|
|
static XP_Bool
|
|
|
|
gameIsOpen( GTKGamesGlobals* gg, sqlite3_int64 rowid )
|
|
|
|
{
|
|
|
|
XP_Bool found = XP_FALSE;
|
|
|
|
GSList* iter;
|
|
|
|
for ( iter = gg->globalsList; !!iter && !found; iter = iter->next ) {
|
|
|
|
GtkAppGlobals* globals = (GtkAppGlobals*)iter->data;
|
|
|
|
found = globals->cGlobals.selRow == rowid;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2013-01-06 04:40:29 +01:00
|
|
|
enum { ROW_ITEM, NAME_ITEM, N_ITEMS };
|
2011-09-03 03:36:03 +02:00
|
|
|
|
2013-01-06 06:01:26 +01:00
|
|
|
/* Prototype for selection handler callback */
|
|
|
|
static void
|
|
|
|
tree_selection_changed_cb( GtkTreeSelection* selection, gpointer data )
|
|
|
|
{
|
|
|
|
LOG_FUNC();
|
|
|
|
GTKGamesGlobals* gg = (GTKGamesGlobals*)data;
|
|
|
|
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeModel *model;
|
|
|
|
gchar *row;
|
|
|
|
|
|
|
|
if ( gtk_tree_selection_get_selected( selection, &model, &iter ) ) {
|
|
|
|
gtk_tree_model_get( model, &iter, ROW_ITEM, &row, -1 );
|
|
|
|
sscanf( row, "%lld", &gg->selRow );
|
|
|
|
g_print ("You selected row %s (parsed: %lld)\n", row, gg->selRow );
|
|
|
|
g_free( row );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget*
|
|
|
|
init_games_list( GTKGamesGlobals* gg )
|
2011-10-14 04:14:08 +02:00
|
|
|
{
|
2013-01-06 06:01:26 +01:00
|
|
|
GtkWidget* list = gtk_tree_view_new();
|
2013-01-06 01:08:19 +01:00
|
|
|
GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
|
|
|
|
GtkTreeViewColumn* column =
|
2013-01-06 04:40:29 +01:00
|
|
|
gtk_tree_view_column_new_with_attributes( "Row", renderer, "text",
|
|
|
|
ROW_ITEM, NULL );
|
|
|
|
gtk_tree_view_append_column( GTK_TREE_VIEW(list), column );
|
|
|
|
|
|
|
|
renderer = gtk_cell_renderer_text_new();
|
|
|
|
column = gtk_tree_view_column_new_with_attributes( "Name", renderer, "text",
|
|
|
|
NAME_ITEM, NULL );
|
2013-01-06 01:08:19 +01:00
|
|
|
gtk_tree_view_append_column( GTK_TREE_VIEW(list), column );
|
2013-01-06 04:40:29 +01:00
|
|
|
|
|
|
|
GtkListStore* store = gtk_list_store_new( N_ITEMS, // G_TYPE_INT64,
|
|
|
|
G_TYPE_STRING, G_TYPE_STRING );
|
2013-01-06 01:08:19 +01:00
|
|
|
gtk_tree_view_set_model( GTK_TREE_VIEW(list), GTK_TREE_MODEL(store) );
|
|
|
|
g_object_unref( store );
|
2013-01-06 06:01:26 +01:00
|
|
|
|
|
|
|
GtkTreeSelection* select =
|
|
|
|
gtk_tree_view_get_selection( GTK_TREE_VIEW (list) );
|
|
|
|
gtk_tree_selection_set_mode( select, GTK_SELECTION_SINGLE );
|
|
|
|
g_signal_connect( G_OBJECT(select), "changed",
|
|
|
|
G_CALLBACK (tree_selection_changed_cb), gg );
|
|
|
|
return list;
|
2011-08-30 05:36:01 +02:00
|
|
|
}
|
2003-11-01 06:35:29 +01:00
|
|
|
|
2013-01-06 04:40:29 +01:00
|
|
|
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" );
|
|
|
|
}
|
|
|
|
|
2003-11-01 06:35:29 +01:00
|
|
|
static void
|
2013-01-06 01:08:19 +01:00
|
|
|
handle_newgame_button( GtkWidget* XP_UNUSED(widget), void* closure )
|
2003-11-01 06:35:29 +01:00
|
|
|
{
|
2013-01-06 01:08:19 +01:00
|
|
|
GTKGamesGlobals* gg = (GTKGamesGlobals*)closure;
|
|
|
|
XP_LOGF( "%s called", __func__ );
|
|
|
|
GtkAppGlobals* globals = malloc( sizeof(*globals) );
|
2013-01-06 01:08:47 +01:00
|
|
|
gg->params->needsNewGame = XP_FALSE;
|
2013-01-06 01:08:19 +01:00
|
|
|
initGlobals( globals, gg->params );
|
|
|
|
if ( !makeNewGame( globals ) ) {
|
|
|
|
freeGlobals( globals );
|
2005-09-14 07:11:29 +02:00
|
|
|
} else {
|
2013-01-06 01:08:19 +01:00
|
|
|
GtkWidget* gameWindow = globals->window;
|
2013-01-06 01:08:47 +01:00
|
|
|
globals->cGlobals.pDb = gg->pDb;
|
2013-01-06 06:01:26 +01:00
|
|
|
globals->cGlobals.selRow = -1;
|
2013-01-07 15:36:34 +01:00
|
|
|
recordOpened( gg, globals );
|
2013-01-06 01:08:19 +01:00
|
|
|
gtk_widget_show( gameWindow );
|
2003-11-01 06:35:29 +01:00
|
|
|
}
|
2011-10-20 03:34:26 +02:00
|
|
|
}
|
|
|
|
|
2013-01-06 06:01:26 +01:00
|
|
|
static void
|
|
|
|
handle_open_button( GtkWidget* XP_UNUSED(widget), void* closure )
|
|
|
|
{
|
|
|
|
GTKGamesGlobals* gg = (GTKGamesGlobals*)closure;
|
2013-01-07 15:36:34 +01:00
|
|
|
if ( -1 != gg->selRow && !gameIsOpen( gg, gg->selRow ) ) {
|
2013-01-06 06:01:26 +01:00
|
|
|
gg->params->needsNewGame = XP_FALSE;
|
|
|
|
GtkAppGlobals* globals = malloc( sizeof(*globals) );
|
|
|
|
initGlobals( globals, gg->params );
|
|
|
|
globals->cGlobals.pDb = gg->pDb;
|
|
|
|
globals->cGlobals.selRow = gg->selRow;
|
2013-01-07 15:36:34 +01:00
|
|
|
recordOpened( gg, globals );
|
2013-01-06 06:01:26 +01:00
|
|
|
gtk_widget_show( globals->window );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-06 01:08:47 +01:00
|
|
|
static void
|
|
|
|
handle_quit_button( GtkWidget* XP_UNUSED(widget), gpointer data )
|
|
|
|
{
|
|
|
|
GTKGamesGlobals* gg = (GTKGamesGlobals*)data;
|
|
|
|
gg = gg;
|
|
|
|
gtk_main_quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_destroy( GtkWidget* XP_UNUSED(widget), gpointer data )
|
|
|
|
{
|
|
|
|
LOG_FUNC();
|
|
|
|
GTKGamesGlobals* gg = (GTKGamesGlobals*)data;
|
2013-01-07 15:36:34 +01:00
|
|
|
GSList* iter;
|
|
|
|
for ( iter = gg->globalsList; !!iter; iter = iter->next ) {
|
|
|
|
GtkAppGlobals* globals = (GtkAppGlobals*)iter->data;
|
|
|
|
freeGlobals( globals );
|
|
|
|
}
|
|
|
|
g_slist_free( gg->globalsList );
|
2013-01-06 01:08:47 +01:00
|
|
|
gtk_main_quit();
|
|
|
|
}
|
|
|
|
|
2013-01-07 15:10:44 +01:00
|
|
|
static void
|
|
|
|
addButton( gchar* label, GtkWidget* parent, GCallback proc, void* closure )
|
|
|
|
{
|
|
|
|
GtkWidget* button = gtk_button_new_with_label( label );
|
|
|
|
gtk_container_add( GTK_CONTAINER(parent), button );
|
|
|
|
g_signal_connect( GTK_OBJECT(button), "clicked",
|
|
|
|
G_CALLBACK(proc), closure );
|
|
|
|
gtk_widget_show( button );
|
|
|
|
}
|
2013-01-06 01:08:47 +01:00
|
|
|
|
2003-11-01 06:35:29 +01:00
|
|
|
static GtkWidget*
|
2013-01-06 01:08:19 +01:00
|
|
|
makeGamesWindow( GTKGamesGlobals* gg )
|
2003-11-01 06:35:29 +01:00
|
|
|
{
|
2013-01-06 01:08:19 +01:00
|
|
|
GtkWidget* window;
|
2003-11-01 06:35:29 +01:00
|
|
|
|
2013-01-06 01:08:19 +01:00
|
|
|
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
|
2013-01-06 01:08:47 +01:00
|
|
|
g_signal_connect( G_OBJECT(window), "destroy",
|
|
|
|
G_CALLBACK(handle_destroy), gg );
|
|
|
|
|
2013-01-06 01:08:19 +01:00
|
|
|
GtkWidget* vbox = gtk_vbox_new( FALSE, 0 );
|
|
|
|
gtk_container_add( GTK_CONTAINER(window), vbox );
|
2003-11-01 06:35:29 +01:00
|
|
|
gtk_widget_show( vbox );
|
2013-01-06 06:01:26 +01:00
|
|
|
GtkWidget* list = init_games_list( gg );
|
2013-01-06 01:08:19 +01:00
|
|
|
gtk_container_add( GTK_CONTAINER(vbox), list );
|
2013-01-06 06:01:26 +01:00
|
|
|
|
2013-01-06 01:08:47 +01:00
|
|
|
gtk_widget_show( list );
|
|
|
|
|
2013-01-06 04:40:29 +01:00
|
|
|
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 );
|
|
|
|
|
2013-01-06 01:08:47 +01:00
|
|
|
GtkWidget* hbox = gtk_hbox_new( FALSE, 0 );
|
|
|
|
gtk_widget_show( hbox );
|
|
|
|
gtk_container_add( GTK_CONTAINER(vbox), hbox );
|
2013-01-07 15:10:44 +01:00
|
|
|
|
|
|
|
addButton( "New game", hbox, G_CALLBACK(handle_newgame_button), gg );
|
|
|
|
addButton( "Open", hbox, G_CALLBACK(handle_open_button), gg );
|
|
|
|
addButton( "Quit", hbox, G_CALLBACK(handle_quit_button), gg );
|
2011-01-24 06:52:26 +01:00
|
|
|
|
2013-01-06 01:08:19 +01:00
|
|
|
gtk_widget_show( window );
|
|
|
|
return window;
|
2008-10-24 11:07:30 +02:00
|
|
|
}
|
|
|
|
|
2013-01-07 15:10:44 +01:00
|
|
|
static gint
|
|
|
|
freeGameGlobals( gpointer data )
|
|
|
|
{
|
|
|
|
LOG_FUNC();
|
|
|
|
GtkAppGlobals* globals = (GtkAppGlobals*)data;
|
2013-01-07 15:36:34 +01:00
|
|
|
GTKGamesGlobals* gg = globals->gg;
|
|
|
|
recordClosed( gg, globals );
|
2013-01-07 15:10:44 +01:00
|
|
|
freeGlobals( globals );
|
|
|
|
return 0; /* don't run again */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
windowDestroyed( GtkAppGlobals* globals )
|
|
|
|
{
|
|
|
|
/* schedule to run after we're back to main loop */
|
|
|
|
(void)g_idle_add( freeGameGlobals, globals );
|
|
|
|
}
|
|
|
|
|
2003-11-01 06:35:29 +01:00
|
|
|
int
|
2013-01-06 01:08:19 +01:00
|
|
|
gtkmain( LaunchParams* params )
|
2003-11-01 06:35:29 +01:00
|
|
|
{
|
2013-01-06 01:08:19 +01:00
|
|
|
GTKGamesGlobals gg = {0};
|
2013-01-06 06:01:26 +01:00
|
|
|
gg.selRow = -1;
|
2013-01-06 01:08:19 +01:00
|
|
|
gg.params = params;
|
|
|
|
XP_LOGF( "%s: I'M HERE!!! (calling makeGamesDB())", __func__ );
|
|
|
|
gg.pDb = openGamesDB();
|
2003-11-01 06:35:29 +01:00
|
|
|
|
2013-01-06 01:08:19 +01:00
|
|
|
(void)makeGamesWindow( &gg );
|
|
|
|
gtk_main();
|
2003-11-01 06:35:29 +01:00
|
|
|
|
2013-01-06 01:08:19 +01:00
|
|
|
closeGamesDB( gg.pDb );
|
2010-09-20 13:55:35 +02:00
|
|
|
|
2013-01-06 01:08:19 +01:00
|
|
|
XP_LOGF( "%s: I'M BACK!!!", __func__ );
|
2003-11-01 06:35:29 +01:00
|
|
|
return 0;
|
|
|
|
} /* gtkmain */
|
|
|
|
|
|
|
|
#endif /* PLATFORM_GTK */
|