add new game_changeDict() and enough gtk code to test it.

This commit is contained in:
Eric House 2012-10-23 19:06:00 -07:00
parent 521d7042be
commit c69dbda36f
10 changed files with 229 additions and 6 deletions

View file

@ -44,6 +44,9 @@ assertUtilOK( XW_UtilCtxt* util )
# define assertUtilOK(u)
#endif
static void gi_setDict( MPFORMAL CurGameInfo* gi, const DictionaryCtxt* dict );
static void
checkServerRole( CurGameInfo* gi, XP_U16* nPlayersHere, XP_U16* nPlayersTotal )
{
@ -183,6 +186,15 @@ game_reset( MPFORMAL XWGame* game, CurGameInfo* gi,
board_prefsChanged( game->board, cp );
} /* game_reset */
void
game_changeDict( MPFORMAL XWGame* game, CurGameInfo* gi, DictionaryCtxt* dict )
{
model_destroyDicts( game->model );
model_setDictionary( game->model, dict );
gi_setDict( MPPARM(mpool) gi, dict );
server_resetEngines( game->server );
}
XP_Bool
game_makeFromStream( MPFORMAL XWStreamCtxt* stream, XWGame* game,
CurGameInfo* gi, DictionaryCtxt* dict,
@ -196,7 +208,7 @@ game_makeFromStream( MPFORMAL XWStreamCtxt* stream, XWGame* game,
XP_Bool hasComms;
#endif
strVersion = stream_getU8( stream );
XP_DEBUGF( "%s: strVersion = 0x%x", __func__, (XP_U16)strVersion );
XP_DEBUGF( "%s: strVersion = 0x%x", __func__, (XP_U16)strVersion );
if ( strVersion > CUR_STREAM_VERS ) {
XP_LOGF( "%s: aborting; stream version too new!", __func__ );
@ -560,6 +572,18 @@ gi_writeToStream( XWStreamCtxt* stream, const CurGameInfo* gi )
}
} /* gi_writeToStream */
static void
gi_setDict( MPFORMAL CurGameInfo* gi, const DictionaryCtxt* dict )
{
XP_U16 ii;
const XP_UCHAR* name = dict_getName( dict );
replaceStringIfDifferent( mpool, &gi->dictName, name );
for ( ii = 0; ii < gi->nPlayers; ++ii ) {
const LocalPlayer* pl = &gi->players[ii];
XP_FREEP( mpool, &pl->dictName );
}
}
XP_Bool
player_hasPasswd( LocalPlayer* player )
{

View file

@ -95,6 +95,8 @@ void game_makeNewGame( MPFORMAL XWGame* game, CurGameInfo* gi,
);
void game_reset( MPFORMAL XWGame* game, CurGameInfo* gi, XW_UtilCtxt* util,
CommonPrefs* cp, const TransportProcs* procs );
void game_changeDict( MPFORMAL XWGame* game, CurGameInfo* gi,
DictionaryCtxt* dict );
XP_Bool game_makeFromStream( MPFORMAL XWStreamCtxt* stream, XWGame* game,
CurGameInfo* gi, DictionaryCtxt* dict,

View file

@ -1581,6 +1581,15 @@ server_getEngineFor( ServerCtxt* server, XP_U16 playerNum )
return engine;
} /* server_getEngineFor */
void
server_resetEngines( ServerCtxt* server )
{
XP_U16 nPlayers = server->vol.gi->nPlayers;
while ( 0 < nPlayers-- ) {
server_resetEngine( server, nPlayers );
}
}
void
server_resetEngine( ServerCtxt* server, XP_U16 playerNum )
{

View file

@ -83,6 +83,7 @@ void server_setGameOverListener( ServerCtxt* server, GameOverListener gol,
EngineCtxt* server_getEngineFor( ServerCtxt* server, XP_U16 playerNum );
void server_resetEngine( ServerCtxt* server, XP_U16 playerNum );
void server_resetEngines( ServerCtxt* server );
XP_U16 server_secondsUsedBy( ServerCtxt* server, XP_U16 playerNum );

View file

@ -173,6 +173,7 @@ GTK_OBJS = \
$(BUILD_PLAT_DIR)/gtkconnsdlg.o \
$(BUILD_PLAT_DIR)/gtkutils.o \
$(BUILD_PLAT_DIR)/gtkntilesask.o \
$(BUILD_PLAT_DIR)/gtkaskdict.o \
$(BUILD_PLAT_DIR)/gtkchat.o
endif
ifdef DO_CURSES

116
xwords4/linux/gtkaskdict.c Normal file
View file

@ -0,0 +1,116 @@
/* -*- compile-command: "make -j3 MEMDEBUG=TRUE"; -*- */
/*
* Copyright 2000-2009 by Eric House (xwords@eehouse.org). All rights
* reserved.
*
* 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
#include "gtkaskdict.h"
// adapted from code at: http://zetcode.com/tutorials/gtktutorial/gtktreeview/
// This thing really needs a cancel button
enum { LIST_ITEM, N_ITEMS };
static void
init_list( GtkWidget* list )
{
GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
GtkTreeViewColumn* column =
gtk_tree_view_column_new_with_attributes( "Dict files", renderer,
"text", LIST_ITEM, NULL );
gtk_tree_view_append_column( GTK_TREE_VIEW(list), column );
GtkListStore* store = gtk_list_store_new( N_ITEMS, 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, 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 );
gtk_list_store_set( store, &iter, LIST_ITEM, str, -1 );
}
static void
on_changed( GtkWidget *widget, gpointer buf )
{
GtkTreeIter iter;
GtkTreeModel *model;
if ( gtk_tree_selection_get_selected( GTK_TREE_SELECTION(widget),
&model, &iter ) ) {
gchar* value;
gtk_tree_model_get( model, &iter, LIST_ITEM, &value, -1 );
strcat( buf, value );
g_free( value );
}
}
static void
on_clicked( GtkWidget* XP_UNUSED(widget), void* XP_UNUSED(closure) )
{
gtk_main_quit();
} /* on_clicked */
gchar*
gtkaskdict( GSList* dicts, gchar* buf, gint buflen )
{
gchar* result = NULL;
buf[0] = '\0';
XP_USE(buflen);
GtkWidget* dialog = gtk_dialog_new();
gtk_window_set_modal( GTK_WINDOW( dialog ), TRUE );
GtkWidget* list = gtk_tree_view_new();
gtk_box_pack_start( GTK_BOX(GTK_DIALOG(dialog)->vbox), list, TRUE, TRUE, 5);
init_list( list );
/* GtkTreeSelection* selection = */
g_signal_connect( gtk_tree_view_get_selection( GTK_TREE_VIEW(list) ),
"changed", G_CALLBACK(on_changed), buf );
GSList* iter;
for ( iter = dicts; !!iter; iter = iter->next ) {
add_to_list( list, (gchar*)iter->data );
}
GtkWidget* button = gtk_button_new_with_label( "Ok" );
g_signal_connect( GTK_OBJECT(button), "clicked",
G_CALLBACK(on_clicked), NULL );
gtk_box_pack_start( GTK_BOX(GTK_DIALOG(dialog)->vbox), button,
FALSE, TRUE, 0 );
// gtk_widget_show( button );
gtk_widget_show_all( dialog );
gtk_main();
gtk_widget_destroy( dialog );
if ( '\0' != buf[0] ) {
XP_ASSERT( buflen > XP_STRLEN(buf) );
result = buf;
}
LOG_RETURNF( "%s", result );
return result;
}
#endif /* PLATFORM_GTK */

View file

@ -0,0 +1,31 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
/*
* Copyright 2012 by Eric House (xwords@eehouse.org). All rights reserved.
*
* 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
#ifndef _GTKASKDICT_H_
#define _GTKASKDICT_H_
#include "gtkmain.h"
gchar* gtkaskdict( GSList* dicts, gchar* buf, gint buflen );
#endif /* _GTKLETTERASK_H_ */
#endif /* PLATFORM_GTK */

View file

@ -58,6 +58,7 @@
#include "gtkletterask.h"
#include "gtkpasswdask.h"
#include "gtkntilesask.h"
#include "gtkaskdict.h"
/* #include "undo.h" */
#include "gtkdraw.h"
#include "memstream.h"
@ -861,10 +862,21 @@ save_game( GtkWidget* XP_UNUSED(widget), GtkAppGlobals* XP_UNUSED(globals) )
} /* save_game */
static void
load_dictionary( GtkWidget* XP_UNUSED(widget),
GtkAppGlobals* XP_UNUSED(globals) )
change_dictionary( GtkWidget* XP_UNUSED(widget), GtkAppGlobals* globals )
{
} /* load_dictionary */
LaunchParams* params = globals->cGlobals.params;
GSList* dicts = listDicts( params );
gchar buf[265];
gchar* name = gtkaskdict( dicts, buf, VSIZE(buf) );
if ( !!name ) {
DictionaryCtxt* dict =
linux_dictionary_make( MPPARM(params->util->mpool) params, name,
params->useMmap );
game_changeDict( MPPARM(params->util->mpool) &globals->cGlobals.game,
&params->gi, dict );
}
g_slist_free( dicts );
} /* change_dictionary */
static void
handle_undo( GtkWidget* XP_UNUSED(widget), GtkAppGlobals* XP_UNUSED(globals) )
@ -1009,8 +1021,8 @@ makeMenus( GtkAppGlobals* globals, int XP_UNUSED(argc),
(void)createAddItem( fileMenu, "Save game",
GTK_SIGNAL_FUNC(save_game), globals );
(void)createAddItem( fileMenu, "Load dictionary",
GTK_SIGNAL_FUNC(load_dictionary), globals );
(void)createAddItem( fileMenu, "Change dictionary",
GTK_SIGNAL_FUNC(change_dictionary), globals );
(void)createAddItem( fileMenu, "Cancel trade",
GTK_SIGNAL_FUNC(handle_trade_cancel), globals );

View file

@ -1462,6 +1462,32 @@ getDictPath( const LaunchParams *params, const char* name,
return success;
}
GSList*
listDicts( const LaunchParams *params )
{
GSList* result = NULL;
GSList* iter = params->dictDirs;
while ( !!iter ) {
const gchar *path = iter->data;
GDir* dir = g_dir_open( path, 0, NULL );
if ( !!dir ) {
for ( ; ; ) {
const gchar* name = g_dir_read_name( dir );
if ( !name ) {
break;
}
if ( g_str_has_suffix( name, ".xwd" ) ) {
gint len = strlen(name) - 4;
result = g_slist_prepend( result, g_strndup( name, len ) );
}
}
g_dir_close( dir );
}
iter = iter->next;
}
return result;
}
int
main( int argc, char** argv )
{

View file

@ -68,6 +68,7 @@ XWStreamCtxt* streamFromDB( CommonGlobals* cGlobals, void* closure );
void writeToFile( XWStreamCtxt* stream, void* closure );
XP_Bool getDictPath( const LaunchParams *params, const char* name,
char* result, int resultLen );
GSList* listDicts( const LaunchParams *params );
void saveGame( CommonGlobals* cGlobals );
int blocking_read( int fd, unsigned char* buf, int len );