From 2e9438a103c2d431144ca166ec1666921248b144 Mon Sep 17 00:00:00 2001 From: gunnar_g Date: Mon, 8 Dec 2008 13:46:11 +0000 Subject: [PATCH] The country combobox in the startup_window shows a layered structure to make it easier to navigate. There are still minor glitches to sort out. --- src/file.c | 50 ++++++++++++++++++--- src/main.c | 2 +- src/misc.c | 31 +++++++++++++ src/misc.h | 6 +++ src/treeview.c | 100 ++++++++++++++++++++++++++++++------------ src/treeview_helper.c | 9 ++-- src/treeview_helper.h | 2 +- src/variables.h | 10 +++++ src/window.c | 20 +++++++++ 9 files changed, 191 insertions(+), 39 deletions(-) diff --git a/src/file.c b/src/file.c index 322891b1..cfda3e47 100644 --- a/src/file.c +++ b/src/file.c @@ -36,6 +36,30 @@ #include "support.h" #include "variables.h" +/** + * Adds a definition directory +*/ +void add_definitions_directory(const gchar *directory) +{ +#ifdef DEBUG + printf("add_definitions_directory\n"); +#endif + gchar **dir_split_up; + + dir_split_up = g_strsplit_set (directory, "\\/", -1); + if (strcmp(dir_split_up[g_strv_length(dir_split_up)-1],"definitions")==0) + { + root_definitions_directories = g_list_prepend (root_definitions_directories, + g_strdup(directory)); + } + if (query_misc_string_starts_with(directory, root_definitions_directories)) + { + definitions_directories = g_list_prepend (definitions_directories, + g_strdup(directory)); + } + g_strfreev(dir_split_up); +} + /** Add the specified directory to the list of directories file_find_support_file() searches for support files. @@ -66,6 +90,7 @@ file_add_support_directory_recursive (const gchar *directo return; } + add_definitions_directory(directory); add_pixmap_directory(directory); support_directories = g_list_prepend (support_directories, g_strdup (directory)); @@ -453,7 +478,8 @@ file_dir_get_contents(const gchar *dir_name, const gchar *prefix, const gchar *s return contents; } -/** Return the country definition files found in the support dirs. */ +/** Return the country definition files found in the support dirs. + * The files are appended with the directories*/ GPtrArray* file_get_country_files(void) { @@ -462,26 +488,38 @@ file_get_country_files(void) #endif gint i; - GList *elem = support_directories; + GList *elem = definitions_directories; GPtrArray *country_files = g_ptr_array_new(); GPtrArray *dir_contents = NULL; + GPtrArray *country_files_full_path = g_ptr_array_new(); + gchar buf[SMALL]; + gchar *country_structure; while(elem != NULL) { dir_contents = file_dir_get_contents((gchar*)elem->data, "country_", ".xml"); - + country_structure = misc_strip_definitions_root((gchar*)elem->data); for(i=0;ilen;i++) + { if(!query_misc_string_in_array((gchar*)g_ptr_array_index(dir_contents, i), country_files)) + { g_ptr_array_add(country_files, - g_strdup((gchar*)g_ptr_array_index(dir_contents, i))); - + g_strdup((gchar*)g_ptr_array_index(dir_contents, i))); + + sprintf(buf, "%s%s%s", g_strdup(country_structure), + G_DIR_SEPARATOR_S, + g_strdup((gchar*)g_ptr_array_index(dir_contents, i))); + g_ptr_array_add(country_files_full_path,g_strdup(buf)); + } + } free_gchar_array(&dir_contents); elem = elem->next; } + free_gchar_array(&country_files); - return country_files; + return country_files_full_path; } /** Read the file until the next line that's not a comment or diff --git a/src/main.c b/src/main.c index bdf04840..7802c801 100644 --- a/src/main.c +++ b/src/main.c @@ -297,7 +297,7 @@ main_init(gint *argc, gchar ***argv) sprintf(buf, "%s%ssaves", pwd, G_DIR_SEPARATOR_S); file_add_support_directory_recursive(buf); g_free(pwd); - + /* initialize the random nr generator */ rand_generator = g_rand_new(); diff --git a/src/misc.c b/src/misc.c index 513dbe44..5c9c0f46 100644 --- a/src/misc.c +++ b/src/misc.c @@ -298,6 +298,37 @@ misc_float_compare(gfloat first, gfloat second) return 0; } +const gchar* +misc_strip_definitions_root(gchar *directory) +{ + gchar **vector; + GList *list = root_definitions_directories; + + while (list!=NULL) + { + if (g_str_has_prefix(directory, (gchar*)list->data)) + { + vector = g_strsplit(directory, (gchar*)list->data,-1); + return g_strdup((gchar*)vector[g_strv_length(vector)-1]); + } + list = list->next; + } + g_strfreev(vector); + return ""; +} + +/** Check whether the string starts with a string in the string array. */ +gboolean +query_misc_string_starts_with(const gchar *string, GList *list) +{ + while (list!=NULL) + { + return g_str_has_prefix(string, (gchar*)list->data); + list = list->next; + } + return FALSE; +} + /** Check whether the string is in the string array. */ gboolean query_misc_string_in_array(const gchar *string, const GPtrArray *array) diff --git a/src/misc.h b/src/misc.h index a234f5d7..87401100 100644 --- a/src/misc.h +++ b/src/misc.h @@ -58,6 +58,12 @@ misc_int_compare(gint first, gint second); gint misc_float_compare(gfloat first, gfloat second); +const gchar* +misc_strip_definitions_root(gchar *directory); + +gboolean +query_misc_string_starts_with(const gchar *string, GList *list); + gboolean query_misc_string_in_array(const gchar *string, const GPtrArray *array); diff --git a/src/treeview.c b/src/treeview.c index 6bf51900..5a80c84f 100644 --- a/src/treeview.c +++ b/src/treeview.c @@ -79,7 +79,7 @@ treeview_create_team_selection_list(gboolean show_cup_teams, gboolean show_user_ if(team_is_user(&g_array_index(lig(i).teams, Team, j)) == -1) { gtk_list_store_append(ls, &iter); - treeview_helper_insert_icon(ls, &iter, 1, g_array_index(lig(i).teams, Team, j).symbol); + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 1, g_array_index(lig(i).teams, Team, j).symbol); gtk_list_store_set(ls, &iter, 0, cnt++, 2, (gpointer)&g_array_index(lig(i).teams, Team, j), @@ -98,7 +98,7 @@ treeview_create_team_selection_list(gboolean show_cup_teams, gboolean show_user_ for(j=0;jlen;j++) { gtk_list_store_append(ls, &iter); - treeview_helper_insert_icon(ls, &iter, 1, + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 1, ((Team*)g_ptr_array_index(cp(i).teams, j))->symbol); gtk_list_store_set(ls, &iter, 0, cnt++, @@ -514,7 +514,8 @@ treeview_live_game_show_commentary(const LiveGameUnit *unit) unit->event.commentary); gtk_list_store_prepend(ls, &iter); - treeview_helper_insert_icon(ls, &iter, 1, treeview_helper_live_game_icon(unit->event.type)); + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 1, + treeview_helper_live_game_icon(unit->event.type)); gtk_list_store_set(ls, &iter, 0, buf, 2, buf2, -1); adjustment->value = adjustment->lower - adjustment->page_size; @@ -549,7 +550,8 @@ treeview_live_game_create_init_commentary(const LiveGameUnit *unit) sprintf(buf, "%3d.", unit->minute); gtk_list_store_append(ls, &iter); - treeview_helper_insert_icon(ls, &iter, 1, treeview_helper_live_game_icon(unit->event.type)); + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 1, + treeview_helper_live_game_icon(unit->event.type)); gtk_list_store_set(ls, &iter, 0, buf, 2, unit->event.commentary, -1); return GTK_TREE_MODEL(ls); @@ -1024,8 +1026,8 @@ treeview_create_fixtures_header(const Fixture *fix, GtkListStore *ls, gboolean b const_app("string_treeview_fixture_header_fg"), buf3); gtk_list_store_append(ls, &iter); - treeview_helper_insert_icon(ls, &iter, 0, symbol); - treeview_helper_insert_icon(ls, &iter, 4, symbol); + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 0, symbol); + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 4, symbol); gtk_list_store_set(ls, &iter, 1, buf, 2, "", 3, buf2, -1); if(blank_line) @@ -1091,8 +1093,8 @@ treeview_create_fixture(const Fixture *fix, GtkListStore *ls) colour_bg, colour_fg, buf_result); gtk_list_store_append(ls, &iter); - treeview_helper_insert_icon(ls, &iter, 0, symbol[0]); - treeview_helper_insert_icon(ls, &iter, 4, symbol[1]); + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 0, symbol[0]); + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 4, symbol[1]); gtk_list_store_set(ls, &iter, 1, buf[0], 2, buf[2], 3, buf[1], -1); } @@ -1235,7 +1237,7 @@ treeview_table_write_header(GtkListStore *ls, const Table *table, gint table_ind } gtk_list_store_append(ls, &iter); - treeview_helper_insert_icon(ls, &iter, 0, symbol); + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 0, symbol); gtk_list_store_set(ls, &iter, 1, "", 2, NULL, 3, buf, -1); for(i=4;i<12;i++) @@ -1267,16 +1269,16 @@ treeview_create_single_table(GtkListStore *ls, const Table *table, gint table_in elem = &g_array_index(table->elements, TableElement, i); if(table->clid >= ID_CUP_START) - treeview_helper_insert_icon(ls, &iter, 0, elem->team->symbol); + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 0, elem->team->symbol); if(elem->old_rank > i) - treeview_helper_insert_icon(ls, &iter, 2, + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 2, const_app("string_treeview_table_up_icon")); else if(elem->old_rank < i) - treeview_helper_insert_icon(ls, &iter, 2, + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 2, const_app("string_treeview_table_down_icon")); else - treeview_helper_insert_icon(ls, &iter, 2, + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 2, const_app("string_treeview_table_stay_icon")); treeview_helper_get_table_element_colours(table, table_index, i, @@ -2293,7 +2295,7 @@ treeview_create_user_history(void) user_history_to_string(&g_array_index(current_user.history, UserHistory, i), buf); gtk_list_store_append(ls, &iter); - treeview_helper_insert_icon(ls, &iter, 3, + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 3, treeview_helper_get_user_history_icon( g_array_index(current_user.history, UserHistory, i).type)); gtk_list_store_set(ls, &iter, @@ -2400,7 +2402,7 @@ treeview_create_league_stats(GtkListStore *ls, const LeagueStat *league_stat) gchar *colour_fg = NULL, *colour_bg = NULL; gtk_list_store_append(ls, &iter); - treeview_helper_insert_icon(ls, &iter, 0, + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 0, league_stat->league_symbol); gtk_list_store_set(ls, &iter, 1, const_int("int_treeview_helper_int_empty"), 2, league_stat->league_name, 3, "", 4, "", 5, "", -1); @@ -2413,7 +2415,7 @@ treeview_create_league_stats(GtkListStore *ls, const LeagueStat *league_stat) 2, "", 3, "", 4, "", 5, "", -1); gtk_list_store_append(ls, &iter); - treeview_helper_insert_icon(ls, &iter, 0, team_icons[i]); + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 0, team_icons[i]); gtk_list_store_set(ls, &iter, 1, const_int("int_treeview_helper_int_empty"), 2, team_titles[i], 4, _("Goals"), -1); @@ -2446,7 +2448,7 @@ treeview_create_league_stats(GtkListStore *ls, const LeagueStat *league_stat) 2, "", 3, "", 4, "", 5, "", -1); gtk_list_store_append(ls, &iter); - treeview_helper_insert_icon(ls, &iter, 0, player_icons[i]); + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 0, player_icons[i]); gtk_list_store_set(ls, &iter, 1, const_int("int_treeview_helper_int_empty"), /* Goals. */ 2, player_titles[i][0], 3, _("Go"), @@ -2567,7 +2569,7 @@ treeview_create_season_history_champions(GtkListStore *ls, const GArray* league_ for(i=0;i<2;i++) { gtk_list_store_append(ls, &iter); - treeview_helper_insert_icon(ls, &iter, 0, icons[i]); + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 0, icons[i]); gtk_list_store_set(ls, &iter, 1, const_int("int_treeview_helper_int_empty"), 2, titles[i], 3, "", 4, "", 5, "", -1); @@ -2637,20 +2639,62 @@ treeview_create_country_list(const GPtrArray *country_list) #endif gint i; - GtkListStore *ls = gtk_list_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_STRING); - GtkTreeIter iter; + guint j; + GtkTreeStore *ls = gtk_tree_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_STRING); gchar buf[SMALL], buf2[SMALL], trash[SMALL]; + gchar **dir_split_up; + // This will keep the iterators for each level of the TreeStore + GPtrArray *iterators; + iterators = g_ptr_array_new (); + gboolean create_new_line; + // This variable will be used to lookup + gchar* previous_element; for(i=0;ilen;i++) { - sscanf((gchar*)g_ptr_array_index(country_list, i), "country_%[^.]%[.xml]", - buf2, trash); - sprintf(buf, "flag_%s.png", buf2); + // We get countries as /<...>/ + // We then try to build a tree using "/ or \" as a separator + // The list is already sorted, so we don't need to verify + dir_split_up = g_strsplit_set ((gchar*)g_ptr_array_index(country_list, i), "\\/", -1); + for (j=0; jlenlen;i++) { gtk_list_store_append(ls, &iter); - treeview_helper_insert_icon(ls, &iter, 0, (gchar*)g_ptr_array_index(symbols, i)); + treeview_helper_insert_icon((GtkTreeModel*)ls, &iter, 0, (gchar*)g_ptr_array_index(symbols, i)); gtk_list_store_set(ls, &iter, 1, (gchar*)g_ptr_array_index(names, i), -1); } diff --git a/src/treeview_helper.c b/src/treeview_helper.c index 8a8518c0..bd98e66e 100644 --- a/src/treeview_helper.c +++ b/src/treeview_helper.c @@ -380,7 +380,7 @@ treeview_helper_unref(GObject *object) column. @param icon_name The filename of the icon. */ void -treeview_helper_insert_icon(GtkListStore *ls, GtkTreeIter *iter, gint column_nr, +treeview_helper_insert_icon(GtkTreeModel *ls, GtkTreeIter *iter, gint column_nr, gchar *icon_name) { #ifdef DEBUG @@ -388,8 +388,11 @@ treeview_helper_insert_icon(GtkListStore *ls, GtkTreeIter *iter, gint column_nr, #endif GdkPixbuf *symbol = treeview_helper_pixbuf_from_filename(icon_name); - - gtk_list_store_set(ls, iter, column_nr, symbol, -1); + if (GTK_IS_LIST_STORE (ls)){ + gtk_list_store_set(ls, iter, column_nr, symbol, -1); + } else if (GTK_IS_TREE_STORE(ls)){ + gtk_tree_store_set(ls, iter, column_nr, symbol, -1); + } treeview_helper_unref(G_OBJECT(symbol)); } diff --git a/src/treeview_helper.h b/src/treeview_helper.h index 7101b269..80e1b548 100644 --- a/src/treeview_helper.h +++ b/src/treeview_helper.h @@ -46,7 +46,7 @@ void treeview_helper_unref(GObject *object); void -treeview_helper_insert_icon(GtkListStore *ls, GtkTreeIter *iter, gint column_nr, +treeview_helper_insert_icon(GtkTreeModel *ls, GtkTreeIter *iter, gint column_nr, gchar *icon_name); GtkCellRenderer* diff --git a/src/variables.h b/src/variables.h index 850ef019..eb96e034 100644 --- a/src/variables.h +++ b/src/variables.h @@ -122,6 +122,16 @@ GRand *rand_generator; */ GList *support_directories; +/** + * The list of root defintions directories found (ending in definitions) + */ +GList *root_definitions_directories; + +/** + * The list of defintions directories found + */ +GList *definitions_directories; + /** The name of the current save file (gets updated when a game is saved or loaded). */ gchar *save_file; diff --git a/src/window.c b/src/window.c index 2788e6ff..4cc6f0e6 100644 --- a/src/window.c +++ b/src/window.c @@ -277,6 +277,20 @@ window_show_help(gint page) gtk_notebook_set_current_page(GTK_NOTEBOOK(lookup_widget(window.help, "notebook1")), page); } +static void +is_capital_sensitive (GtkCellLayout *cell_layout, + GtkCellRenderer *cell, + GtkTreeModel *tree_model, + GtkTreeIter *iter, + gpointer data) +{ + gboolean sensitive; + + sensitive = !gtk_tree_model_iter_has_child (tree_model, iter); + + g_object_set (cell, "sensitive", sensitive, NULL); +} + /** Show the country selection window. All files with prefix 'country_' from $HOME/.bygfoot/definitions are appended to a combo box. @@ -315,6 +329,11 @@ window_show_startup(void) language_pick_country(country_files); model = treeview_create_country_list(country_files); gtk_combo_box_set_model(GTK_COMBO_BOX(combo_country), model); + gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_country), + renderer, + is_capital_sensitive, + NULL, NULL); + g_object_unref(model); gtk_combo_box_set_active(GTK_COMBO_BOX(combo_country), 0); @@ -1140,3 +1159,4 @@ window_show_alr(void) gtk_spin_button_set_range(GTK_SPIN_BUTTON(lookup_widget(window.alr, "spinbutton_weekly_installment")), 0, -current_user.debt * powf(1 + current_user.debt_interest, const_int("int_finance_payback_weeks")) + 1); } +