From e4e16947c7aae490df9b137d122fd2ace7d26587 Mon Sep 17 00:00:00 2001 From: gyboth Date: Tue, 10 Mar 2009 20:58:34 +0000 Subject: [PATCH] Constants editing. --- src/file.c | 17 +++++++----- src/file.h | 2 +- src/load_save.c | 4 +-- src/misc.c | 40 +++++++++++++++++++++++++++ src/misc.h | 3 ++ src/option.c | 8 ++++-- src/option.h | 3 ++ src/option_gui.c | 2 +- src/options_callbacks.c | 8 +++--- src/treeview.c | 6 ++-- src/treeview2.c | 45 ++++++++++++++++++------------ src/treeview2.h | 2 +- src/treeview_helper.c | 61 +++++++++++++++++++++++++++++++++++++---- src/treeview_helper.h | 23 ++++++++++++---- src/window.c | 2 +- src/xml.c | 2 +- 16 files changed, 175 insertions(+), 53 deletions(-) diff --git a/src/file.c b/src/file.c index ad811f6f..6d2858cb 100644 --- a/src/file.c +++ b/src/file.c @@ -610,7 +610,7 @@ file_save_opt_file(const gchar *filename, OptionList *optionlist) /** Load a file containing name - value pairs into the specified array. */ void -file_load_opt_file(const gchar *filename, OptionList *optionlist) +file_load_opt_file(const gchar *filename, OptionList *optionlist, gboolean sort) { #ifdef DEBUG printf("file_load_opt_file\n"); @@ -651,6 +651,9 @@ file_load_opt_file(const gchar *filename, OptionList *optionlist) } } + if(sort) + g_array_sort(optionlist->list, (GCompareFunc)option_compare_func); + for(i=0;ilist->len;i++) g_datalist_set_data(&optionlist->datalist, g_array_index(optionlist->list, Option, i).name, &g_array_index(optionlist->list, Option, i)); @@ -679,7 +682,7 @@ file_load_hints_file(void) else strcpy(hints_file, "bygfoot_hints_en"); - file_load_opt_file(hints_file, &hints); + file_load_opt_file(hints_file, &hints, FALSE); } /** Load the options at the beginning of a new game from @@ -694,12 +697,12 @@ file_load_conf_files(void) gint i; gchar *conf_file = file_find_support_file("bygfoot.conf", TRUE); - file_load_opt_file(conf_file, &options); + file_load_opt_file(conf_file, &options, FALSE); g_free(conf_file); - file_load_opt_file(opt_str("string_opt_constants_file"), &constants); - file_load_opt_file(opt_str("string_opt_appearance_file"), &constants_app); - file_load_opt_file("bygfoot_tokens", &tokens); + file_load_opt_file(opt_str("string_opt_constants_file"), &constants, TRUE); + file_load_opt_file(opt_str("string_opt_appearance_file"), &constants_app, TRUE); + file_load_opt_file("bygfoot_tokens", &tokens, FALSE); file_load_hints_file(); for(i=0;ilen;i++) @@ -729,7 +732,7 @@ file_load_user_conf_file(User *user) file_find_support_file(opt_str("string_opt_default_user_conf_file"), TRUE); } - file_load_opt_file(conf_file, &user->options); + file_load_opt_file(conf_file, &user->options, FALSE); g_free(conf_file); } diff --git a/src/file.h b/src/file.h index 85406aa8..781d9185 100644 --- a/src/file.h +++ b/src/file.h @@ -46,7 +46,7 @@ gboolean file_get_next_opt_line(FILE *fil, gchar *opt_name, gchar *opt_value); void -file_load_opt_file(const gchar *filename, OptionList *optionlist); +file_load_opt_file(const gchar *filename, OptionList *optionlist, gboolean sort); void file_save_opt_file(const gchar *filename, OptionList *optionlist); diff --git a/src/load_save.c b/src/load_save.c index 5a464692..9c94ca5a 100644 --- a/src/load_save.c +++ b/src/load_save.c @@ -237,9 +237,9 @@ load_save_load_game(const gchar* filename, gboolean create_main_window) PIC_TYPE_LOAD); g_string_sprintf(buf, "%s%s%s___options", dirname, G_DIR_SEPARATOR_S, prefix); - file_load_opt_file(buf->str, &options); + file_load_opt_file(buf->str, &options, FALSE); g_string_sprintf(buf, "%s%s%s___settings", dirname, G_DIR_SEPARATOR_S, prefix); - file_load_opt_file(buf->str, &settings); + file_load_opt_file(buf->str, &settings, FALSE); language_set(language_get_code_index(opt_str("string_opt_language_code")) + 1); if(debug > 60) diff --git a/src/misc.c b/src/misc.c index 5783905a..967f3ff0 100644 --- a/src/misc.c +++ b/src/misc.c @@ -792,3 +792,43 @@ misc_string_replace_all_tokens(GPtrArray **token_rep, return (g_strrstr(dest, "_") == NULL); } + +/* Alphabetic compare function. */ +gint +misc_alphabetic_compare(gconstpointer a, gconstpointer b) +{ + const gchar *string[2] = {(const gchar*)a, + (const gchar*)b}; + gchar alphabet[26] = {'a','b','c','d','e','f','g', + 'h','i','j','k','l','m','n', + 'o','p','q','r','s','t','u', + 'v','w','x','y','z'}; + gint len[2] = {strlen(string[0]), strlen(string[1])}; + gint maxlen = MIN(len[0], len[1]); + gint letter[2]; + gint i, j, k; + + for(i = 0; i < maxlen; i++) + { + for(k = 0; k < 2; k++) + { + letter[k] = 0; + for(j = 0; j < 26; j++) + if(string[k][i] == alphabet[j]) + { + letter[k] = j; + break; + } + } + + if(letter[0] < letter[1]) + return -1; + else if(letter[0] > letter[1]) + return 1; + } + + if(len[0] != len[1]) + return 1 - 2 * (len[0] < len[1]); + + return 0; +} diff --git a/src/misc.h b/src/misc.h index 87401100..2808fc4f 100644 --- a/src/misc.h +++ b/src/misc.h @@ -135,4 +135,7 @@ void misc_token_add_bool(GPtrArray **token_rep, gint token_idx, gboolean value); +gint +misc_alphabetic_compare(gconstpointer a, gconstpointer b); + #endif diff --git a/src/option.c b/src/option.c index b55dd939..0f24cb25 100644 --- a/src/option.c +++ b/src/option.c @@ -194,8 +194,6 @@ option_add(OptionList *optionlist, const gchar *name, ((Option*)element)->string_value = (string_value == NULL) ? NULL : g_strdup(string_value); return; } -/* main_exit_program(EXIT_OPTION_NOT_FOUND, */ -/* "Option named '%s' already contained in optionlist.", name); */ new.name = g_strdup(name); new.value = int_value; @@ -214,3 +212,9 @@ option_add(OptionList *optionlist, const gchar *name, g_array_index(optionlist->list, Option, i).name, &g_array_index(optionlist->list, Option, i)); } + +gint +option_compare_func(gconstpointer a, gconstpointer b) +{ + return misc_alphabetic_compare(((const Option*)a)->name, ((const Option*)b)->name); +} diff --git a/src/option.h b/src/option.h index 82edf23b..295a31ef 100644 --- a/src/option.h +++ b/src/option.h @@ -90,4 +90,7 @@ option_set_int(const gchar *name, OptionList *optionlist, gint new_value); void option_add(OptionList *optionlist, const gchar *name, gint int_value, const gchar *string_value); +gint +option_compare_func(gconstpointer a, gconstpointer b); + #endif diff --git a/src/option_gui.c b/src/option_gui.c index 856d6cbd..1f3773cf 100644 --- a/src/option_gui.c +++ b/src/option_gui.c @@ -560,7 +560,7 @@ option_gui_write_options(void) if(i == ENTRY_OPT_CONSTANTS && strcmp(gtk_entry_get_text(entry_widgets[i]), opt_str("string_opt_constants_file")) != 0) - file_load_opt_file(gtk_entry_get_text(entry_widgets[i]), &constants); + file_load_opt_file(gtk_entry_get_text(entry_widgets[i]), &constants, TRUE); else if(i == ENTRY_OPT_FONT_NAME && strcmp(gtk_entry_get_text(entry_widgets[i]), opt_str("string_opt_font_name")) != 0) on_button_back_to_main_clicked(NULL, NULL); diff --git a/src/options_callbacks.c b/src/options_callbacks.c index 5a63cac6..f18ab886 100644 --- a/src/options_callbacks.c +++ b/src/options_callbacks.c @@ -108,7 +108,7 @@ on_button_reload_constants_clicked (GtkButton *button, const gchar *constants_file = gtk_entry_get_text(GTK_ENTRY(lookup_widget(window.options, "entry_constants_file"))); - file_load_opt_file(constants_file, &constants); + file_load_opt_file(constants_file, &constants, TRUE); } @@ -126,7 +126,7 @@ on_checkbutton_save_global_button_press_event { gchar *conf_file = file_find_support_file("bygfoot.conf", TRUE); - file_load_opt_file(conf_file, &options); + file_load_opt_file(conf_file, &options, FALSE); g_free(conf_file); option_gui_set_up_window(); @@ -225,11 +225,11 @@ on_button_constants_reload_clicked (GtkButton *button, { printf("hu\n"); file_load_opt_file(gtk_entry_get_text(GTK_ENTRY(lookup_widget(window.options, "entry_constants_file"))), - &constants); + &constants, TRUE); } else file_load_opt_file(opt_str("string_opt_constants_file"), - &constants); + &constants, TRUE); } diff --git a/src/treeview.c b/src/treeview.c index 8da1b6d3..33ffa1c1 100644 --- a/src/treeview.c +++ b/src/treeview.c @@ -137,7 +137,7 @@ treeview_set_up_team_selection_treeview(GtkTreeView *treeview) gtk_tree_view_set_search_column(treeview, 2); gtk_tree_view_set_search_equal_func(treeview, - treeview_helper_search_equal, + treeview_helper_search_equal_teams, NULL, NULL); /* Numbering the teams */ @@ -443,10 +443,8 @@ treeview_show_player_list(GtkTreeView *treeview, GPtrArray *players, treeview_helper_clear(treeview); for(i=0;iname, - 2, ((Option*)g_ptr_array_index(list, i))->value, + 0, ((Option*)g_ptr_array_index(list, i))->name, + 1, ((Option*)g_ptr_array_index(list, i))->value, -1); else if(type == CONSTANTS_TYPE_FLOAT) gtk_list_store_set(ls, &iter, - 0, i, - 1, ((Option*)g_ptr_array_index(list, i))->name, - 2, (gfloat)((Option*)g_ptr_array_index(list, i))->value / OPTION_FLOAT_DIVISOR, + 0, ((Option*)g_ptr_array_index(list, i))->name, + 1, (gfloat)((Option*)g_ptr_array_index(list, i))->value / OPTION_FLOAT_DIVISOR, -1); else gtk_list_store_set(ls, &iter, - 0, i, - 1, ((Option*)g_ptr_array_index(list, i))->name, - 2, ((Option*)g_ptr_array_index(list, i))->string_value, + 0, ((Option*)g_ptr_array_index(list, i))->name, + 1, ((Option*)g_ptr_array_index(list, i))->string_value, -1); } @@ -782,7 +779,7 @@ treeview2_create_constants(const GPtrArray *list, gint type) } void -treeview2_set_up_constants(GtkTreeView *treeview) +treeview2_set_up_constants(GtkTreeView *treeview, gint type) { #ifdef DEBUG printf("treeview2_set_up_constants\n"); @@ -790,9 +787,8 @@ treeview2_set_up_constants(GtkTreeView *treeview) GtkTreeViewColumn *col; GtkCellRenderer *renderer; - gchar *titles[3] = - {"", - _("Name"), + gchar *titles[2] = + {_("Name"), _("Value")}; gint i; @@ -800,8 +796,12 @@ treeview2_set_up_constants(GtkTreeView *treeview) GTK_SELECTION_SINGLE); gtk_tree_view_set_headers_visible(treeview, TRUE); gtk_tree_view_set_rules_hint(treeview, TRUE); + gtk_tree_view_set_search_column(treeview, 0); + gtk_tree_view_set_search_equal_func(treeview, + treeview_helper_search_equal_strings, + NULL, NULL); - for(i = 0; i < 3; i++) + for(i = 0; i < 2; i++) { col = gtk_tree_view_column_new(); gtk_tree_view_column_set_title(col, titles[i]); @@ -810,6 +810,15 @@ treeview2_set_up_constants(GtkTreeView *treeview) gtk_tree_view_column_pack_start(col, renderer, TRUE); gtk_tree_view_column_add_attribute(col, renderer, "text", i); + + if(i == 1) + { + g_object_set(renderer, "editable", TRUE, NULL); + g_signal_connect (renderer, + "edited", + G_CALLBACK (treeview_helper_constants_editing_done), + treeview); + } } } @@ -848,7 +857,7 @@ treeview2_show_constants(void) for(i = 0; i < 4; i++) { treeview_helper_clear(treeview[i]); - treeview2_set_up_constants(treeview[i]); + treeview2_set_up_constants(treeview[i], i); model = treeview2_create_constants(list[i], i); gtk_tree_view_set_model(treeview[i], model); g_object_unref(model); diff --git a/src/treeview2.h b/src/treeview2.h index 5b9a80ca..25c53833 100644 --- a/src/treeview2.h +++ b/src/treeview2.h @@ -86,7 +86,7 @@ GtkTreeModel* treeview2_create_constants(const GPtrArray *list, gint type); void -treeview2_set_up_constants(GtkTreeView *treeview); +treeview2_set_up_constants(GtkTreeView *treeview, gint type); void treeview2_show_constants(void); diff --git a/src/treeview_helper.c b/src/treeview_helper.c index 777ead1d..11d71459 100644 --- a/src/treeview_helper.c +++ b/src/treeview_helper.c @@ -1814,14 +1814,14 @@ treeview_helper_bet_odds(GtkTreeViewColumn *col, } gboolean -treeview_helper_search_equal(GtkTreeModel *model, - gint column, - const gchar *key, - GtkTreeIter *iter, - gpointer search_data) +treeview_helper_search_equal_teams(GtkTreeModel *model, + gint column, + const gchar *key, + GtkTreeIter *iter, + gpointer search_data) { #ifdef DEBUG - printf("treeview_helper_search_equal\n"); + printf("treeview_helper_search_equal_teams\n"); #endif const Team *tm = NULL; @@ -1837,6 +1837,24 @@ treeview_helper_search_equal(GtkTreeModel *model, return return_value; } +gboolean +treeview_helper_search_equal_strings(GtkTreeModel *model, + gint column, + const gchar *key, + GtkTreeIter *iter, + gpointer search_data) +{ +#ifdef DEBUG + printf("treeview_helper_search_equal_strings\n"); +#endif + + const gchar *string = NULL; + + gtk_tree_model_get(model, iter, column, &string, -1); + + return (g_strrstr(string, key) == NULL); +} + void treeview_helper_news_additional(GtkTreeViewColumn *col, GtkCellRenderer *renderer, @@ -2015,3 +2033,34 @@ treeview_helper_player_name_editing_started(GtkCellRenderer *renderer, gtk_widget_set_sensitive(lookup_widget(window.main, "menubar1"), FALSE); gtk_widget_set_sensitive(lookup_widget(window.main, "hbox1"), FALSE); } + +void +treeview_helper_constants_editing_done(GtkCellRendererText *renderer, + gchar *path, + gchar *new_text, + gpointer user_data) +{ + GtkTreeModel *model = gtk_tree_view_get_model((GtkTreeView*)user_data); + GtkTreeIter iter; + const gchar *name; + gfloat float_value = g_ascii_strtod(new_text, NULL); + + gtk_tree_model_get_iter_from_string(model, &iter, path); + gtk_tree_model_get(model, &iter, 0, &name, -1); + + if(g_str_has_prefix(name, "int_")) + { + option_set_int(name, &constants, (gint)float_value); + gtk_list_store_set(GTK_LIST_STORE(model), &iter, 1, (gint)float_value, -1); + } + else if(g_str_has_prefix(name, "float_")) + { + option_set_int(name, &constants, (gint)rint(g_ascii_strtod(new_text, NULL) * OPTION_FLOAT_DIVISOR)); + gtk_list_store_set(GTK_LIST_STORE(model), &iter, 1, float_value, -1); + } + else + { + option_set_string(name, &constants, new_text); + gtk_list_store_set(GTK_LIST_STORE(model), &iter, 1, new_text, -1); + } +} diff --git a/src/treeview_helper.h b/src/treeview_helper.h index 73478da3..c3d32c71 100644 --- a/src/treeview_helper.h +++ b/src/treeview_helper.h @@ -215,11 +215,18 @@ treeview_helper_bet_odds(GtkTreeViewColumn *col, gpointer user_data); gboolean -treeview_helper_search_equal(GtkTreeModel *model, - gint column, - const gchar *key, - GtkTreeIter *iter, - gpointer search_data); +treeview_helper_search_equal_teams(GtkTreeModel *model, + gint column, + const gchar *key, + GtkTreeIter *iter, + gpointer search_data); + +gboolean +treeview_helper_search_equal_strings(GtkTreeModel *model, + gint column, + const gchar *key, + GtkTreeIter *iter, + gpointer search_data); void treeview_helper_job_exchange(GtkTreeViewColumn *col, @@ -258,4 +265,10 @@ void treeview_helper_player_name_editing_canceled(GtkCellRendererText *renderer, gpointer user_data); +void +treeview_helper_constants_editing_done(GtkCellRendererText *renderer, + gchar *path, + gchar *new_text, + gpointer user_data); + #endif diff --git a/src/window.c b/src/window.c index 0c459fb1..1949f389 100644 --- a/src/window.c +++ b/src/window.c @@ -786,7 +786,7 @@ window_main_load_geometry(void) { optionlist.list = NULL; optionlist.datalist = NULL; - file_load_opt_file(filename, &optionlist); + file_load_opt_file(filename, &optionlist, FALSE); gtk_window_resize(GTK_WINDOW(window.main), option_int("int_window_settings_width", &optionlist), diff --git a/src/xml.c b/src/xml.c index 06a632ea..71cb039d 100644 --- a/src/xml.c +++ b/src/xml.c @@ -64,7 +64,7 @@ xml_load_users(const gchar *dirname, const gchar *basename) { sprintf(buf, "%s%s%s___user_%02d_options", dirname, G_DIR_SEPARATOR_S, basename, i); - file_load_opt_file(buf, &usr(i).options); + file_load_opt_file(buf, &usr(i).options, FALSE); sprintf(buf, "%s%s%s___user_%02d_live_game.xml", dirname, G_DIR_SEPARATOR_S, basename, i);