<URL: http://bugs.freeciv.org/Ticket/Display.html?id=40710 >
> [jmarda...@ifrance.com - Sat Feb 07 21:26:04 2009]: > > I have made a patch adding in the Economic report dialog > (GTK client only) a list of all the traderoutes. The total > amount of trade is also displayed. > > The patch is based on SVN version 15463. I apologize for not getting to this patch sooner. There are some problems with committing these code changes as they are now: - You must follow the patch creation guidelines: http://freeciv.wikia.com/wiki/How_to_Contribute even if there is only one file is changed. - You must follow the coding style: http://freeciv.wikia.com/wiki/Coding_Style even if you are copying from older code already in the repository. There are some design problems too: - Traderoutes to your own cities are listed twice (the information is the same). - The improvement related buttons in the economic report should be grouped with the improvement list. - With all those columns in the traderoute list the economy window may be too wide. I started to fix these problems but stopped since it was taking too long and really you should be the one to do it (if you are still interested). I will attach though a "work in progress" patch which you can base a better patch on. In particular I took the time to greatly improve the gtk coding style since you had unfortunately mimicked a particularly bad section of old gui code. :( ----------------------------------------------------------------------- 怠け者じゃないの?
diff --git a/client/gui-gtk-2.0/repodlgs.c b/client/gui-gtk-2.0/repodlgs.c index d3172a0..e3c46f0 100644 --- a/client/gui-gtk-2.0/repodlgs.c +++ b/client/gui-gtk-2.0/repodlgs.c @@ -49,6 +49,7 @@ #include "packhand_gen.h" #include "control.h" #include "reqtree.h" +#include "plrdlg.h" /* for get_flag() */ #include "text.h" #include "canvas.h" @@ -76,6 +77,7 @@ enum { }; static void create_economy_report_dialog(bool make_modal); +static void traderoutes_selection_changed(GtkTreeSelection *selection); static void economy_command_callback(struct gui_dialog *dlg, int response, gpointer data); static void economy_selection_callback(GtkTreeSelection *selection, @@ -85,6 +87,7 @@ static struct universal economy_row_type[U_LAST + B_LAST]; static struct gui_dialog *economy_dialog_shell = NULL; static GtkWidget *economy_label2; static GtkListStore *economy_store; +static GtkWidget *traderoutes_view; static GtkTreeSelection *economy_selection; static GtkWidget *sellall_command, *sellobsolete_command; static int economy_dialog_shell_is_modal; @@ -637,6 +640,14 @@ void popdown_economy_report_dialog(void) *****************************************************************/ void create_economy_report_dialog(bool make_modal) { + GtkWidget *frame, *hbox, *vbox, *view, *sw, *label; + GtkListStore *store; + GtkTreeSelection *sel; + GtkCellRenderer *renderer; + GtkTreeViewColumn *col; + const char *title; + int i; + const char *titles[5] = { /* TRANS: Image header */ _("Type"), @@ -646,8 +657,6 @@ void create_economy_report_dialog(bool make_modal) /* TRANS: Upkeep total, count*cost */ _("U Total") }; - int i; - static GType model_types[5] = { G_TYPE_NONE, G_TYPE_STRING, @@ -655,16 +664,45 @@ void create_economy_report_dialog(bool make_modal) G_TYPE_INT, G_TYPE_INT }; - GtkWidget *view, *sw, *align; + + const char *traderoutes_titles[8] = { + _("Source City"), + Q_("?City:Size"), + _("Dest. City"), + _("Flag"), + _("Nation"), + Q_("?City:Size"), + _("Distance"), + _("Trade") + }; + static GType traderoutes_types[8] = { + G_TYPE_STRING, + G_TYPE_INT, + G_TYPE_STRING, + G_TYPE_NONE, /* GDK_TYPE_PIXBUF */ + G_TYPE_STRING, + G_TYPE_INT, + G_TYPE_INT, + G_TYPE_INT + }; model_types[0] = GDK_TYPE_PIXBUF; + traderoutes_types[3] = GDK_TYPE_PIXBUF; gui_dialog_new(&economy_dialog_shell, GTK_NOTEBOOK(top_notebook), NULL); gui_dialog_set_title(economy_dialog_shell, _("Economy")); - align = gtk_alignment_new(0.5, 0.0, 0.0, 1.0); - gtk_box_pack_start(GTK_BOX(economy_dialog_shell->vbox), align, - TRUE, TRUE, 0); + vbox = economy_dialog_shell->vbox; + + hbox = gtk_hbox_new(FALSE, 8); + gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0); + + /* Improvement list on the left side. */ + frame = gtk_frame_new(_("Buildings")); + gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, TRUE, 2); + + vbox = gtk_vbox_new(FALSE, 0); + gtk_container_add(GTK_CONTAINER(frame), vbox); economy_store = gtk_list_store_newv(ARRAY_SIZE(model_types), model_types); @@ -673,7 +711,7 @@ void create_economy_report_dialog(bool make_modal) GTK_SHADOW_ETCHED_IN); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); - gtk_container_add(GTK_CONTAINER(align), sw); + gtk_box_pack_start(GTK_BOX(vbox), sw, TRUE, TRUE, 0); view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(economy_store)); g_object_unref(economy_store); @@ -684,9 +722,6 @@ void create_economy_report_dialog(bool make_modal) G_CALLBACK(economy_selection_callback), NULL); for (i=0; i<ARRAY_SIZE(model_types); i++) { - GtkCellRenderer *renderer; - GtkTreeViewColumn *col; - if (model_types[i] == GDK_TYPE_PIXBUF) { renderer = gtk_cell_renderer_pixbuf_new(); @@ -715,8 +750,7 @@ void create_economy_report_dialog(bool make_modal) gtk_container_add(GTK_CONTAINER(sw), view); economy_label2 = gtk_label_new(_("Total Cost:")); - gtk_box_pack_start(GTK_BOX(economy_dialog_shell->vbox), economy_label2, - FALSE, FALSE, 0); + gtk_box_pack_start(GTK_BOX(vbox), economy_label2, FALSE, FALSE, 0); gtk_misc_set_padding(GTK_MISC(economy_label2), 5, 5); sellobsolete_command = @@ -729,22 +763,130 @@ void create_economy_report_dialog(bool make_modal) ECONOMY_SELL_ALL); gtk_widget_set_sensitive(sellall_command, FALSE); + /* The traderoutes list in right side */ + frame = gtk_frame_new(_("City Traderoutes")); + gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, TRUE, 0); + + vbox = gtk_vbox_new(FALSE, 0); + gtk_container_add(GTK_CONTAINER(frame), vbox); + + sw = gtk_scrolled_window_new(NULL,NULL); + gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), + GTK_SHADOW_ETCHED_IN); + gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), + GTK_POLICY_AUTOMATIC, + GTK_POLICY_AUTOMATIC); + gtk_box_pack_start(GTK_BOX(vbox), sw, TRUE, TRUE, 0); + + store = gtk_list_store_newv(ARRAY_SIZE(traderoutes_types), + traderoutes_types); + + view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)); + gtk_widget_set_name(view, "small_font"); + gtk_tree_view_columns_autosize(GTK_TREE_VIEW(view)); + + for (i = 0; i < ARRAY_SIZE(traderoutes_types); i++) { + title = traderoutes_titles[i]; + + if (traderoutes_types[i] == GDK_TYPE_PIXBUF) { + renderer = gtk_cell_renderer_pixbuf_new(); + col = gtk_tree_view_column_new_with_attributes(title, renderer, + "pixbuf", i, NULL); + } else { + renderer = gtk_cell_renderer_text_new(); + col = gtk_tree_view_column_new_with_attributes(title, renderer, + "text", i, NULL); + gtk_tree_view_column_set_sort_column_id(col, i); + } + gtk_tree_view_append_column(GTK_TREE_VIEW(view), col); + } + gtk_container_add(GTK_CONTAINER(sw), view); + traderoutes_view = view; + + sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); + gtk_tree_selection_set_mode(sel, GTK_SELECTION_MULTIPLE); + g_signal_connect(sel, "changed", + G_CALLBACK(traderoutes_selection_changed), NULL); + + hbox = gtk_hbox_new(FALSE, 0); + gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); + + label = gtk_label_new(_("Total: 0")); + gtk_misc_set_padding(GTK_MISC(label), 5, 5); + gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); + g_object_set_data(G_OBJECT(view), "total label", label); + + label = gtk_label_new(_("Selected: 0")); + gtk_misc_set_padding(GTK_MISC(label), 5, 5); + gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); + g_object_set_data(G_OBJECT(view), "selected label", label); + + /* Close button and update */ gui_dialog_add_button(economy_dialog_shell, GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE); gui_dialog_response_set_callback(economy_dialog_shell, economy_command_callback); - economy_report_dialog_update(); gui_dialog_set_default_size(economy_dialog_shell, -1, 350); gui_dialog_set_default_response(economy_dialog_shell, GTK_RESPONSE_CLOSE); gui_dialog_show_all(economy_dialog_shell); + economy_report_dialog_update(); gtk_tree_view_focus(GTK_TREE_VIEW(view)); } +/*************************************************************************** + Update the value displayed by the "total trade" label in the ecconomic + report, or make it blank if nothing can be bought. +***************************************************************************/ +static void update_trade_labels(void) +{ + GtkWidget *label, *view; + GList *rows, *p; + GtkTreeModel *model; + GtkTreeSelection *sel; + GtkTreePath *path; + GtkTreeIter iter; + int trade, total; + char buf[128]; + + view = traderoutes_view; + if (!view) { + return; + } + + sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); + rows = gtk_tree_selection_get_selected_rows(sel, &model); + total = 0; + + for (p = rows; p != NULL; p = p->next) { + path = p->data; + if (gtk_tree_model_get_iter(model, &iter, path)) { + gtk_tree_model_get(model, &iter, 7, &trade, -1); + total += trade; + } + gtk_tree_path_free(path); + } + g_list_free(rows); + + label = g_object_get_data(G_OBJECT(view), "selected label"); + if (label) { + my_snprintf(buf, sizeof(buf), _("Selected: %d"), total); + gtk_label_set_text(GTK_LABEL(label), buf); + } +} + +/************************************************************************** + Update total trade label when the user makes a change in the selection + of traderoutes. +**************************************************************************/ +static void traderoutes_selection_changed(GtkTreeSelection *selection) +{ + update_trade_labels(); +} /**************************************************************** Called when a building type is selected in the economy list. @@ -856,12 +998,20 @@ static void economy_command_callback(struct gui_dialog *dlg, int response, void economy_report_dialog_update(void) { if(!is_report_dialogs_frozen() && economy_dialog_shell) { - int tax, total_impr, total_unit, i, entries_used, nbr_impr; + int tax, dist, i, entries_used, nbr_impr; + int total_trade, total_impr, total_unit; char economy_total[48]; + const char *nation; struct improvement_entry entries[B_LAST]; struct unit_entry entries_units[U_LAST]; + GtkWidget *label; + GtkTreeView *view; + GtkListStore *store; GtkTreeIter it; GValue value = { 0, }; + GdkPixbuf *flag; + struct city *pcitydest; + const struct player *pplayer = client_player(); gtk_list_store_clear(economy_store); @@ -908,7 +1058,52 @@ void economy_report_dialog_update(void) my_snprintf(economy_total, sizeof(economy_total), _("Income: %d Total Costs: %d"), tax, total_impr + total_unit); gtk_label_set_text(GTK_LABEL(economy_label2), economy_total); - } + + /* Update traderoutes list. */ + view = GTK_TREE_VIEW(traderoutes_view); + store = GTK_LIST_STORE(gtk_tree_view_get_model(view)); + gtk_list_store_clear(store); + total_trade = 0; + + city_list_iterate(pplayer->cities, pcity) { + for (i = 0; i < NUM_TRADEROUTES; i++) { + if (pcity->trade[i] <= 0 && pcity->trade_value[i] <= 0) { + continue; + } + + total_trade += pcity->trade_value[i]; + + /* Display infos of the dest city trade */ + pcitydest = game_find_city_by_number(pcity->trade[i]); + dist = map_distance(pcity->tile, pcitydest->tile); + nation = nation_adjective_translation(nation_of_city(pcitydest)); + gtk_list_store_append(store, &it); + gtk_list_store_set(store, &it, + 0, city_name(pcity), + 1, pcity->size, + 2, city_name(pcitydest), + 4, nation, + 5, pcitydest->size, + 6, dist, + 7, pcity->trade_value[i], + -1); + + flag = get_flag(nation_of_city(pcitydest)); + if (flag != NULL) { + gtk_list_store_set(store, &it, 3, flag, -1); + g_object_unref(flag); + } + } + } city_list_iterate_end; + + /* Total trade */ + label = g_object_get_data(G_OBJECT(view), "total label"); + if (label) { + char buf[128]; + my_snprintf(buf, sizeof(buf), _("Total: %d"), total_trade); + gtk_label_set_text(GTK_LABEL(label), buf); + } + } } /****************************************************************
_______________________________________________ Freeciv-dev mailing list Freeciv-dev@gna.org https://mail.gna.org/listinfo/freeciv-dev