Only in work: civscore.log diff -urd -X freeciv.current/diff_ignore freeciv.current/client/gui-gtk/cityrep.c work/client/gui-gtk/cityrep.c --- freeciv.current/client/gui-gtk/cityrep.c Thu Sep 13 19:24:26 2001 +++ work/client/gui-gtk/cityrep.c Fri Sep 14 21:54:02 2001 @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -192,6 +193,22 @@ else return can_build_improvement(pcity, number); } + +struct item { + int id, section; + char descr[MAX_LEN_NAME + 10]; +}; + +static int my_cmp(const void *p1, const void *p2) +{ + const struct item *i1 = (const struct item *) p1; + const struct item *i2 = (const struct item *) p2; + + if (i1->section == i2->section) + return mystrcasecmp(i1->descr, i2->descr); + return (i1->section - i2->section); +} + /**************************************************************** ... *****************************************************************/ @@ -202,89 +219,94 @@ gboolean change_prod, TestCityFunc test_func, GtkSignalFunc callback) -{ +{ gint i; gint first = append_units ? B_LAST : 0; gint last = append_units ? game.num_unit_types + B_LAST : B_LAST; - gboolean something_appended = FALSE; - GtkWidget *item; + struct item items[U_LAST + B_LAST]; + gint items_used = 0; + GtkWidget *item; - for(i=first; icities, pcity) - { - append |= test_func(pcity, i); - } city_list_iterate_end; - } - else - { - GList *selection; + if (!change_prod) { + city_list_iterate(game.player_ptr->cities, pcity) { + append |= test_func(pcity, i); + } + city_list_iterate_end; + } else { + GList *selection; - g_assert (GTK_CLIST(city_list)->selection); - selection = GTK_CLIST(city_list)->selection; - for(; selection; selection = g_list_next(selection)) - append |= test_func(city_from_glist(selection), i); + g_assert(GTK_CLIST(city_list)->selection); + selection = GTK_CLIST(city_list)->selection; + for (; selection; selection = g_list_next(selection)) + append |= test_func(city_from_glist(selection), i); + } + + if (append) { + gint cost, section; + gchar *name; + + if (append_units) { + cost = get_unit_type(i - B_LAST)->build_cost; + name = get_unit_name(i - B_LAST); + section = unit_flag(i - B_LAST, F_NONMIL) ? 0 : 1; + } else { + if (i == B_CAPITAL) { + cost = -1; + section = 0; + } else { + cost = get_improvement_type(i)->build_cost; + section = 1; } - - if(append) - { - gint cost; - gchar* name; - - if(append_units) - { - cost = get_unit_type(i-B_LAST)->build_cost; - name = get_unit_name(i-B_LAST); - } - else - { - cost = (i==B_CAPITAL) ? -1 : get_improvement_type(i)->build_cost; - if(append_wonders) - { - /* We need a city to get the right name for wonders */ - struct city *pcity = GTK_CLIST(city_list)->row_list->data; - name = get_impr_name_ex(pcity, i); - } - else - name = get_improvement_name(i); - } - something_appended = TRUE; + if (append_wonders) { + /* We need a city to get the right name for wonders */ + struct city *pcity = GTK_CLIST(city_list)->row_list->data; + name = get_impr_name_ex(pcity, i); + } else { + name = get_improvement_name(i); + } + } - if (change_prod) - { - gchar *label; - if (cost < 0) { - label = g_strdup_printf("%s (XX)",name); - } else { - label = g_strdup_printf("%s (%d)",name, cost); - } - item=gtk_menu_item_new_with_label( label ); - g_free (label); - } - else - item=gtk_menu_item_new_with_label(name); - - gtk_menu_append(GTK_MENU(menu),item); - - gtk_signal_connect(GTK_OBJECT(item),"activate", callback, - GINT_TO_POINTER(i)); + if (change_prod) { + if (cost < 0) { + snprintf(items[items_used].descr, sizeof(items[items_used].descr), + "%s (XX)", name); + } else { + snprintf(items[items_used].descr, sizeof(items[items_used].descr), + "%s (%d)", name, cost); } + } else { + mystrlcpy(items[items_used].descr, name, + sizeof(items[items_used].descr)); + } + items[items_used].id = i; + items[items_used].section = section; + items_used++; + assert(items_used < ARRAY_SIZE(items)); } + } - if(!something_appended) - { - item=gtk_menu_item_new_with_label( nothing_appended_text ); - gtk_widget_set_sensitive(item, FALSE); - gtk_menu_append(GTK_MENU(menu),item); - } + qsort(items, items_used, sizeof(struct item), my_cmp); + + for (i = 0; i < items_used; i++) { + item = gtk_menu_item_new_with_label(items[i].descr); + + gtk_menu_append(GTK_MENU(menu), item); + + gtk_signal_connect(GTK_OBJECT(item), "activate", callback, + GINT_TO_POINTER(items[i].id)); + } + + if (items_used == 0) { + item = gtk_menu_item_new_with_label(nothing_appended_text); + gtk_widget_set_sensitive(item, FALSE); + gtk_menu_append(GTK_MENU(menu), item); + } } /**************************************************************** @@ -345,36 +367,38 @@ gboolean append_units, TestCityFunc test_func) { - if(append_improvements) - { - /* Add all buildings */ - append_impr_or_unit_to_menu_sub(menu, _("No Buildings Available"), - FALSE, FALSE, change_prod, - (gboolean (*)(struct city*,gint)) - test_func, - select_impr_or_unit_callback); - /* Add a separator */ - gtk_menu_append(GTK_MENU(menu),gtk_menu_item_new ()); - - /* Add all wonders */ - append_impr_or_unit_to_menu_sub(menu, _("No Wonders Available"), - FALSE, TRUE, change_prod, - (gboolean (*)(struct city*,gint)) - test_func, - select_impr_or_unit_callback); - /* Add a separator */ - if(append_units) - gtk_menu_append(GTK_MENU(menu),gtk_menu_item_new ()); - } - - if(append_units) - { - /* Add all units */ - append_impr_or_unit_to_menu_sub(menu, _("No Units Available"), - TRUE, FALSE, change_prod, - test_func, - select_impr_or_unit_callback); - } + if (append_improvements) { + /* Add all buildings */ + append_impr_or_unit_to_menu_sub(menu, _("No Buildings Available"), + FALSE, FALSE, change_prod, + (gboolean(*)(struct city *, gint)) + test_func, + select_impr_or_unit_callback); + /* Add a separator */ + gtk_menu_append(GTK_MENU(menu), gtk_menu_item_new()); + } + + if (append_units) { + /* Add all units */ + append_impr_or_unit_to_menu_sub(menu, _("No Units Available"), + TRUE, FALSE, change_prod, + test_func, + select_impr_or_unit_callback); + } + + if (append_improvements) { + /* Add a separator */ + if (append_units) + gtk_menu_append(GTK_MENU(menu), gtk_menu_item_new()); + + /* Add all wonders */ + append_impr_or_unit_to_menu_sub(menu, _("No Wonders Available"), + FALSE, TRUE, change_prod, + (gboolean(*)(struct city *, gint)) + test_func, + select_impr_or_unit_callback); + } + gtk_object_set_data(GTK_OBJECT(menu), "freeciv_test_func", test_func); gtk_object_set_data(GTK_OBJECT(menu), "freeciv_change_prod",