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 f7271c40c2
commit e87e420e53
16 changed files with 81 additions and 25 deletions

View File

@ -28,7 +28,7 @@ add_definitions("-DPACKAGE=\"${PROJECT_NAME}\"")
add_executable(bygfoot WIN32
src/bet.c src/bet.h src/bet_struct.h
src/bygfoot.h
src/bygfoot.c src/bygfoot.h
src/callback_func.c src/callback_func.h
src/callbacks.c src/callbacks.h
src/cup.c src/cup.h src/cup_struct.h

19
src/bygfoot.c Normal file
View File

@ -0,0 +1,19 @@
#include "bygfoot.h"
#include "misc.h"
#include "user.h"
#include "xml_country.h"
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 "user_struct.h"
#include "bygfoot_struct.h"
/**
* Program version number and year (copyright).
@ -202,4 +204,6 @@ typedef struct
/** The position of the horizontal divider is stored here. */
gint paned_pos;
} Windows;
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
{
gint unused;
} 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

@ -44,11 +44,12 @@ G_MODULE_EXPORT void
on_button_add_player_clicked (GtkButton *button,
gpointer user_data)
{
Bygfoot *bygfoot = (Bygfoot*)user_data;
#ifdef DEBUG
printf("on_button_add_player_clicked\n");
#endif
misc_callback_add_player();
misc_callback_add_player(bygfoot);
}
@ -56,11 +57,12 @@ G_MODULE_EXPORT void
on_entry_player_name_activate (GtkEntry *entry,
gpointer user_data)
{
Bygfoot *bygfoot = (Bygfoot*)user_data;
#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

@ -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);