Factor user creation code out from the gtk callbacks

Also introduce a new Bygfoot struct for holding all the global data.
This will eventually replace all the global variables in bygfoot, but
for now it is an empty struct that we are just passing it through to
functions that access global variables.
This commit is contained in:
Tom Stellard 2020-12-15 20:01:39 -08:00
parent ecbae9da08
commit 99104a9909
17 changed files with 142 additions and 29 deletions

View File

@ -26,7 +26,59 @@ endif (GLIB_PKG_FOUND)
add_definitions("-DPACKAGE_DATA_DIR=\"${CMAKE_INSTALL_FULL_DATADIR}\"")
add_definitions("-DPACKAGE=\"${PROJECT_NAME}\"")
add_executable(bygfoot WIN32 src/bet.c src/bet.h src/finance.h src/fixture.h src/game_gui.h src/league.h src/main.h src/maths.h src/misc.h src/option.h src/player.h src/support.h src/treeview2.h src/user.h src/callback_func.c src/callback_func.h src/callbacks.h src/cup.h src/gui.h src/live_game.h src/start_end.h src/team.h src/transfer.h src/treeview.h src/treeview_helper.h src/window.h src/callbacks.c src/debug.h src/free.h src/game.h src/job.h src/load_save.h src/misc2_callback_func.h src/table.h src/cup.c src/variables.h src/xml_league.h src/debug.c src/strategy.h src/file.c src/file.h src/language.h src/finance.c src/fixture.c src/free.c src/bet_struct.h src/lg_commentary_struct.h src/strategy_struct.h src/game.c src/game_gui.c src/gui.c src/interface.c src/interface.h src/job.c src/xml_country.h src/language.c src/lg_commentary.h src/lg_commentary.c src/xml_lg_commentary.h src/league.c src/stat.h src/live_game.c src/misc_callback_func.h src/load_save.c src/xml.h src/xml_loadsave_cup.h src/xml_loadsave_jobs.h src/xml_loadsave_league.h src/xml_loadsave_leagues_cups.h src/xml_loadsave_misc.h src/xml_loadsave_season_stats.h src/xml_loadsave_transfers.h src/xml_loadsave_users.h src/main.c src/job_struct.h src/misc_callbacks.h src/name_struct.h src/stat_struct.h src/transfer_struct.h src/xml_strategy.h src/maths.c src/misc.c src/misc_callbacks.c src/misc_callback_func.c src/xml_name.h src/misc2_callbacks.c src/misc2_callbacks.h src/misc2_interface.h src/misc3_callbacks.c src/misc3_callbacks.h src/misc3_interface.h src/misc2_callback_func.c src/misc_interface.c src/misc_interface.h src/misc2_interface.c src/misc3_interface.c src/name.c src/name.h src/news.c src/news.h src/xml_news.h src/option.c src/option_gui.c src/option_gui.h src/training.h src/options_callbacks.c src/options_callbacks.h src/options_interface.h src/options_interface.c src/player.c src/start_end.c src/youth_academy.h src/stat.c src/table_struct.h src/strategy.c src/support.c src/table.c src/team.c src/xml_team.h src/training.c src/enums.h src/training_callbacks.c src/bygfoot.h src/training_callbacks.h src/training_interface.h src/training_struct.h src/training_interface.c src/transfer.c src/treeview.c src/treeview2.c src/treeview_helper.c src/user.c src/xml_mmatches.h src/window.c src/xml.c src/xml_loadsave_fixtures.h src/xml_loadsave_league_stat.h src/xml_loadsave_live_game.h src/xml_loadsave_table.h src/xml_loadsave_teams.h src/xml_loadsave_cup.c src/xml_loadsave_fixtures.c src/xml_loadsave_jobs.c src/xml_loadsave_league.c src/xml_loadsave_leagues_cups.c src/league_struct.h src/xml_loadsave_league_stat.c src/xml_loadsave_live_game.c src/xml_loadsave_misc.c src/xml_loadsave_newspaper.c src/xml_loadsave_newspaper.h src/xml_loadsave_players.c src/xml_loadsave_players.h src/xml_loadsave_season_stats.c src/xml_loadsave_table.c src/xml_loadsave_teams.c src/xml_loadsave_transfers.c src/xml_loadsave_users.c src/xml_cup.c src/xml_cup.h src/xml_country.c src/xml_league.c src/cup_struct.h src/xml_lg_commentary.c src/xml_mmatches.c src/xml_name.c src/xml_news.c src/xml_strategy.c src/xml_team.c src/youth_academy.c src/zip/zip.c src/zip/zip.h src/zip/unzip.c src/zip/unzip.h)
add_executable(bygfoot src/bet.c src/bet.h src/finance.h src/fixture.h
src/game_gui.h src/league.h src/main.h src/maths.h src/misc.h
src/option.h src/player.h src/support.h src/treeview2.h src/user.h
src/callback_func.c src/callback_func.h src/callbacks.h src/cup.h
src/gui.h src/live_game.h src/start_end.h src/team.h src/transfer.h
src/treeview.h src/treeview_helper.h src/window.h src/callbacks.c
src/debug.h src/free.h src/game.h src/job.h src/load_save.h
src/misc2_callback_func.h src/table.h src/cup.c src/variables.h
src/xml_league.h src/debug.c src/strategy.h src/file.c src/file.h
src/language.h src/finance.c src/fixture.c src/free.c src/bet_struct.h
src/lg_commentary_struct.h src/strategy_struct.h src/game.c
src/game_gui.c src/gui.c src/interface.c src/interface.h src/job.c
src/xml_country.h src/language.c src/lg_commentary.h
src/lg_commentary.c src/xml_lg_commentary.h src/league.c src/stat.h
src/live_game.c src/misc_callback_func.h src/load_save.c src/xml.h
src/xml_loadsave_cup.h src/xml_loadsave_jobs.h
src/xml_loadsave_league.h src/xml_loadsave_leagues_cups.h
src/xml_loadsave_misc.h src/xml_loadsave_season_stats.h
src/xml_loadsave_transfers.h src/xml_loadsave_users.h src/main.c
src/job_struct.h src/misc_callbacks.h src/name_struct.h
src/stat_struct.h src/transfer_struct.h src/xml_strategy.h src/maths.c
src/misc.c src/misc_callbacks.c src/misc_callback_func.c
src/xml_name.h src/misc2_callbacks.c src/misc2_callbacks.h
src/misc2_interface.h src/misc3_callbacks.c src/misc3_callbacks.h
src/misc3_interface.h src/misc2_callback_func.c src/misc_interface.c
src/misc_interface.h src/misc2_interface.c src/misc3_interface.c
src/name.c src/name.h src/news.c src/news.h src/xml_news.h
src/option.c src/option_gui.c src/option_gui.h src/training.h
src/options_callbacks.c src/options_callbacks.h
src/options_interface.h src/options_interface.c src/player.c
src/start_end.c src/youth_academy.h src/stat.c src/table_struct.h
src/strategy.c src/support.c src/table.c src/team.c src/xml_team.h
src/training.c src/enums.h src/training_callbacks.c src/bygfoot.h
src/training_callbacks.h src/training_interface.h
src/training_struct.h src/training_interface.c src/transfer.c
src/treeview.c src/treeview2.c src/treeview_helper.c src/user.c
src/xml_mmatches.h src/window.c src/xml.c src/xml_loadsave_fixtures.h
src/xml_loadsave_league_stat.h src/xml_loadsave_live_game.h
src/xml_loadsave_table.h src/xml_loadsave_teams.h
src/xml_loadsave_cup.c src/xml_loadsave_fixtures.c
src/xml_loadsave_jobs.c src/xml_loadsave_league.c
src/xml_loadsave_leagues_cups.c src/league_struct.h
src/xml_loadsave_league_stat.c src/xml_loadsave_live_game.c
src/xml_loadsave_misc.c src/xml_loadsave_newspaper.c
src/xml_loadsave_newspaper.h src/xml_loadsave_players.c
src/xml_loadsave_players.h src/xml_loadsave_season_stats.c
src/xml_loadsave_table.c src/xml_loadsave_teams.c
src/xml_loadsave_transfers.c src/xml_loadsave_users.c src/xml_cup.c
src/xml_cup.h src/xml_country.c src/xml_league.c src/cup_struct.h
src/xml_lg_commentary.c src/xml_mmatches.c src/xml_name.c
src/xml_news.c src/xml_strategy.c src/xml_team.c src/youth_academy.c
src/zip/zip.c src/zip/zip.h src/zip/unzip.c src/zip/unzip.h
src/bygfoot.c)
# Some gtk headers use deprecated glib features, so disable this warning
# since we can't do anything about it.

25
src/bygfoot.c Normal file
View File

@ -0,0 +1,25 @@
#include "bygfoot.h"
#include "misc.h"
#include "user.h"
#include "xml_country.h"
Country *bygfoot_load_country(Bygfoot *bygfoot, const gchar *country_name)
{
xml_country_read(country_name, NULL);
return &country;
}
User *bygfoot_add_user(Bygfoot *bygfoot, const gchar *username, Team *tm)
{
User new_user = user_new();
new_user.tm = tm;
new_user.team_id = tm->id;
/* FIXME: Use strnlen */
if(strlen(username) > 0)
misc_string_assign(&new_user.name, username);
g_array_append_val(users, new_user);
return &g_array_index(users, User, users->len - 1);
}

View File

@ -44,6 +44,8 @@
#include <gtk/gtk.h>
#include "gettext_macros.h"
#include "debug.h"
#include "bygfoot_struct.h"
#include "user_struct.h"
/**
* Program version number and year (copyright).
@ -202,4 +204,7 @@ typedef struct
/** The position of the horizontal divider is stored here. */
gint paned_pos;
} Windows;
Country *bygfoot_load_country(Bygfoot *bygfoot, const gchar *country_name);
User *bygfoot_add_user(Bygfoot *bygfoot, const gchar *username, Team *tm);
#endif

12
src/bygfoot_struct.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef BYGFOOT_STRUCT_H
#define BYGFOOT_STRUCT_H
/** This struct holds all of the global state for a bygfoot game. The goal
* is for ths struct to eventually replace all the global variables.
*/
typedef struct
{
gchar *id;
} Bygfoot;
#endif

View File

@ -585,7 +585,7 @@ on_menu_new_activate (GtkMenuItem *menuitem,
free_memory();
main_init(NULL, NULL);
window_show_startup();
window_show_startup(user_data);
stat0 = STATUS_TEAM_SELECTION;
statp = NULL;
}

View File

@ -33,6 +33,7 @@
#endif
#include "bet_struct.h"
#include "bygfoot_struct.h"
#include "debug.h"
#include "file.h"
#include "free.h"
@ -407,6 +408,7 @@ main_init(gint *argc, gchar ***argv)
gint
main (gint argc, gchar *argv[])
{
Bygfoot bygfoot;
#ifdef DEBUG
printf("main\n");
#endif
@ -440,7 +442,7 @@ main (gint argc, gchar *argv[])
}
else
{
window_show_startup();
window_show_startup(&bygfoot);
stat0 = STATUS_TEAM_SELECTION;
}
}

View File

@ -213,7 +213,7 @@ on_button_splash_new_game_clicked (GtkButton *button,
window_destroy(&window.splash);
window_show_startup();
window_show_startup(user_data);
stat0 = STATUS_TEAM_SELECTION;
}

View File

@ -130,7 +130,7 @@ misc_callback_start_game(void)
/** Add a user to the users array. */
void
misc_callback_add_player(void)
misc_callback_add_player(Bygfoot *bygfoot)
{
#ifdef DEBUG
printf("misc_callback_add_player\n");
@ -145,22 +145,15 @@ misc_callback_add_player(void)
GtkComboBox *combo_leagues =
GTK_COMBO_BOX(lookup_widget(window.startup, "combobox_start_league"));
const gchar *player_name = gtk_entry_get_text(entry_player_name);
User new_user = user_new();
Team *tm = (Team*)treeview_helper_get_pointer(treeview_startup, 2);
User *new_user = bygfoot_add_user(bygfoot, player_name, tm);
gint start_league =
gtk_combo_box_get_active(combo_leagues);
if(strlen(player_name) > 0)
misc_string_assign(&new_user.name, player_name);
gtk_entry_set_text(entry_player_name, "");
new_user.tm = tm;
new_user.team_id = tm->id;
new_user.scout = (start_league == 0 || tm->clid == lig(start_league - 1).id) ? -1 : start_league - 1;
new_user->scout = (start_league == 0 || tm->clid == lig(start_league - 1).id) ? -1 : start_league - 1;
g_array_append_val(users, new_user);
treeview_show_users(treeview_users);

View File

@ -35,7 +35,7 @@ void
misc_callback_show_team_list(GtkWidget *widget, const gchar *country_file);
void
misc_callback_add_player(void);
misc_callback_add_player(Bygfoot *bygfoot);
void
misc_callback_remove_user(GdkEventButton *event);

View File

@ -42,25 +42,25 @@
G_MODULE_EXPORT void
on_button_add_player_clicked (GtkButton *button,
gpointer user_data)
Bygfoot *bygfoot)
{
#ifdef DEBUG
printf("on_button_add_player_clicked\n");
#endif
misc_callback_add_player();
misc_callback_add_player(bygfoot);
}
G_MODULE_EXPORT void
on_entry_player_name_activate (GtkEntry *entry,
gpointer user_data)
Bygfoot *bygfoot)
{
#ifdef DEBUG
printf("on_entry_player_name_activate\n");
#endif
on_button_add_player_clicked(NULL, NULL);
on_button_add_player_clicked(NULL, bygfoot);
}

View File

@ -75,7 +75,7 @@ on_spinbutton_speed_value_changed (GtkSpinButton *spinbutton,
void
on_button_add_player_clicked (GtkButton *button,
gpointer user_data);
Bygfoot *bygfoot);
gboolean
@ -85,7 +85,7 @@ on_treeview_users_button_press_event (GtkWidget *widget,
void
on_entry_player_name_activate (GtkEntry *entry,
gpointer user_data);
Bygfoot *bygfoot);
void
on_spinbutton_capacity_value_changed (GtkSpinButton *spinbutton,

View File

@ -36,17 +36,18 @@
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include "bygfoot_struct.h"
#include "misc_callbacks.h"
#include "misc_interface.h"
#include "support.h"
#include "file.h"
GtkWidget*
create_window_startup (void)
create_window_startup (Bygfoot *bygfoot)
{
GtkWidget *window_startup;
GtkBuilder *builder;
builder = load_ui(file_find_support_file("bygfoot_misc.glade", TRUE));
builder = load_ui_with_userdata(file_find_support_file("bygfoot_misc.glade", TRUE), bygfoot);
window_startup = GTK_WIDGET (gtk_builder_get_object (builder, "window_startup"));
/* Store pointers to all widgets, for use by lookup_widget(). */

View File

@ -2,7 +2,9 @@
* DO NOT EDIT THIS FILE - it is generated by Glade.
*/
GtkWidget* create_window_startup (void);
#include "bygfoot_struct.h"
GtkWidget* create_window_startup (Bygfoot *bygfoot);
GtkWidget* create_window_font_sel (void);
GtkWidget* create_window_live (void);
GtkWidget* create_window_stadium (void);

View File

@ -68,6 +68,12 @@ lookup_widget (GtkWidget *widget,
*/
GtkBuilder*
load_ui (const gchar *filename)
{
return load_ui_with_userdata(filename, NULL);
}
GtkBuilder*
load_ui_with_userdata (const gchar *filename, Bygfoot *bygfoot)
{
GtkBuilder *builder;
GError *error = NULL;
@ -80,7 +86,7 @@ load_ui (const gchar *filename)
": Problems found in the glade file: %s\n", error->message);
}
gtk_builder_connect_signals (builder, NULL);
gtk_builder_connect_signals (builder, bygfoot);
return builder;
}

View File

@ -23,6 +23,8 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "bygfoot_struct.h"
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
@ -76,5 +78,8 @@ GtkWidget* lookup_widget (GtkWidget *widget,
GtkBuilder*
load_ui (const gchar *filename);
GtkBuilder*
load_ui_with_userdata (const gchar *filename, Bygfoot *bygfoot);
/* Use this function to set the directory containing installed pixmaps. */
void add_pixmap_directory (const gchar *directory);

View File

@ -289,14 +289,14 @@ is_capital_sensitive (GtkCellLayout *cell_layout,
'country_' from $HOME/.bygfoot/definitions are appended to a combo box.
*/
void
window_show_startup(void)
window_show_startup(Bygfoot *bygfoot)
{
#ifdef DEBUG
printf("window_show_startup\n");
#endif
GtkWidget *window_startup =
window_create(WINDOW_STARTUP);
window_create_with_userdata(WINDOW_STARTUP, bygfoot);
GtkWidget *combo_country =
lookup_widget(window_startup, "combo_country");
GPtrArray *country_files = NULL;
@ -794,6 +794,12 @@ window_main_load_geometry(void)
@see #Windows */
GtkWidget*
window_create(gint window_type)
{
return window_create_with_userdata(window_type, NULL);
}
GtkWidget*
window_create_with_userdata(gint window_type, Bygfoot *bygfoot)
{
gchar buf[SMALL];
GtkWidget *wind = NULL;
@ -827,7 +833,7 @@ window_create(gint window_type)
if(window.startup != NULL)
debug_print_message("window_create: called on already existing window\n");
else
window.startup = create_window_startup();
window.startup = create_window_startup(bygfoot);
wind = window.startup;
break;
case WINDOW_LIVE:

View File

@ -27,6 +27,7 @@
#define WINDOW_H
#include "bygfoot.h"
#include "bygfoot_struct.h"
/** These are used to keep track of open windows.
@see window_create() */
@ -64,11 +65,14 @@ void
window_show_help(gint page);
void
window_show_startup(void);
window_show_startup(Bygfoot *bygfoot);
GtkWidget*
window_create(gint window_type);
GtkWidget*
window_create_with_userdata(gint window_type, Bygfoot *bygfoot);
void
window_destroy(GtkWidget **wind);