Complete.Org: Mailing Lists: Archives: freeciv-dev: June 2003:
[Freeciv-Dev] Re: (PR#4426) Adding append command to gui-gtk-2.0
Home

[Freeciv-Dev] Re: (PR#4426) Adding append command to gui-gtk-2.0

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] Re: (PR#4426) Adding append command to gui-gtk-2.0
From: "jjc@xxxxxxxxxxxxxxxxxx" <jjc@xxxxxxxxxxxxxxxxxx>
Date: Fri, 20 Jun 2003 13:39:33 -0700
Reply-to: rt@xxxxxxxxxxxxxx

On Thu, Jun 19, 2003 at 07:50:19PM -0700, Guest wrote:
> 
> Adds the ability to add an improvement or unit to a city's worklist in 
> the city report dialog in gui-gtk-2.0.
Added some more comments in the current version of the patch.  
No code changes.  Tested for a full game and did not find any problems.
 


-- 
Josh Cogliati


Index: client/gui-gtk-2.0/cityrep.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/cityrep.c,v
retrieving revision 1.45
diff -U9 -r1.45 cityrep.c
--- client/gui-gtk-2.0/cityrep.c        2003/05/18 16:19:29     1.45
+++ client/gui-gtk-2.0/cityrep.c        2003/06/20 20:30:44
@@ -61,36 +61,44 @@
 static void city_activated_callback(GtkTreeView *view, GtkTreePath *path,
                                    GtkTreeViewColumn *col, gpointer data);
 
 static void city_buy_callback(GtkWidget *w, gpointer data);
 static void city_refresh_callback(GtkWidget *w, gpointer data);
 static void city_selection_changed_callback(GtkTreeSelection *selection);
 
 static void create_select_menu(GtkWidget *item);
 static void create_change_menu(GtkWidget *item);
+static void create_append_menu(GtkWidget *item);
 
 static GtkWidget *city_dialog_shell=NULL;
 
 static GtkWidget *city_view;
 static GtkTreeSelection *city_selection;
 static GtkListStore *city_model;
 
 static void popup_select_menu(GtkMenuShell *menu, gpointer data);
 static void popup_change_menu(GtkMenuShell *menu, gpointer data);
+static void popup_append_menu(GtkMenuShell *menu, gpointer data);
 
 static GtkWidget *city_center_command, *city_popup_command, *city_buy_command;
 static GtkWidget *city_change_command;
+static GtkWidget *city_append_command;
 
 static GtkWidget *change_improvements_item;
 static GtkWidget *change_units_item;
 static GtkWidget *change_wonders_item;
 static GtkWidget *change_cma_item;
 
+static GtkWidget *append_improvements_item;
+static GtkWidget *append_units_item;
+static GtkWidget *append_wonders_item;
+
+
 static GtkWidget *select_island_item;
 
 static GtkWidget *select_bunit_item;
 static GtkWidget *select_bimprovement_item;
 static GtkWidget *select_bwonder_item;
 
 static GtkWidget *select_supported_item;
 static GtkWidget *select_present_item;
 static GtkWidget *select_built_improvements_item;
@@ -160,18 +168,19 @@
 typedef bool (*TestCityFunc)(struct city *, gint);
 
 /****************************************************************
 ...
 *****************************************************************/
 static void append_impr_or_unit_to_menu_item(GtkMenuItem *parent_item,
                                             bool append_units,
                                             bool append_wonders,
                                             bool change_prod,
+                                            bool worklist_append,
                                             TestCityFunc test_func,
                                             GCallback callback,
                                             int size)
 {
   GtkWidget *menu;
   cid cids[U_LAST + B_LAST];
   struct item items[U_LAST + B_LAST];
   int i, item, cids_used;
   char *row[4];
@@ -220,19 +229,22 @@
   name_and_sort_items(cids, cids_used, items, change_prod, NULL);
 
   for (i = 0; i < 4; i++) {
     row[i] = buf[i];
   }
 
   g_object_set_data(G_OBJECT(menu), "freeciv_test_func", test_func);
   g_object_set_data(G_OBJECT(menu), "freeciv_change_prod",
                    GINT_TO_POINTER(change_prod));
+  g_object_set_data(G_OBJECT(menu), "freeciv_worklist_append",
+                   GINT_TO_POINTER(worklist_append));
 
+
   for (i = 0; i < 3; i++) {
     group[i] = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
   }
   
   for (item = 0; item < cids_used; item++) {
     cid cid = items[item].cid;
     GtkWidget *menu_item, *hbox, *label;
     char txt[256];
 
@@ -289,27 +301,57 @@
 
   packet = *(struct packet_city_request *)data;
   gtk_tree_model_get(model, it, 1, &id, -1);
 
   packet.city_id = id;
   send_packet_city_request(&aconnection, &packet, PACKET_CITY_CHANGE);
 }
 
 /****************************************************************
+called by select_impr_or_unit_callback for each city that 
+is selected in the city list dialog to have a object appended
+to the worklist.
+*****************************************************************/
+static void worklist_append_impr_or_unit_iterate(GtkTreeModel *model, 
+                                                GtkTreePath *path,
+                                                GtkTreeIter *it, 
+                                                gpointer data)
+{
+  struct packet_city_request packet;
+  gint id;
+  struct city *pcity;  
+
+  packet = *(struct packet_city_request *)data;
+  gtk_tree_model_get(model, it, 1, &id, -1);
+  pcity = city_list_find_id(&game.player_ptr->cities,id);
+  worklist_append(&pcity->worklist, packet.build_id, 
+                 packet.is_build_id_unit_id);
+  copy_worklist(&packet.worklist, &pcity->worklist);
+
+  packet.city_id = id;
+  send_packet_city_request(&aconnection, &packet, PACKET_CITY_WORKLIST);
+}
+
+
+
+/****************************************************************
 ...
 *****************************************************************/
 static void select_impr_or_unit_callback(GtkWidget *w, gpointer data)
 {
   cid cid = GPOINTER_TO_INT(data);
   GObject *parent = G_OBJECT(w->parent);
   TestCityFunc test_func = g_object_get_data(parent, "freeciv_test_func");
   bool change_prod = 
     GPOINTER_TO_INT(g_object_get_data(parent, "freeciv_change_prod"));
+  bool worklist_append = 
+    GPOINTER_TO_INT(g_object_get_data(parent, "freeciv_worklist_append"));
+    
 
   /* If this is not the change production button */
   if (!change_prod) {
     ITree it;
     GtkTreeModel *model = GTK_TREE_MODEL(city_model);
 
     gtk_tree_selection_unselect_all(city_selection);
     for (itree_begin(model, &it); !itree_end(&it); itree_next(&it)) {
       struct city *pcity;
@@ -325,20 +367,27 @@
   } else {
     bool is_unit = cid_is_unit(cid);
     int id = cid_id(cid);
     struct packet_city_request packet;
 
     packet.build_id = id;
     packet.is_build_id_unit_id = is_unit;
 
     connection_do_buffer(&aconnection);
-    gtk_tree_selection_selected_foreach(city_selection, impr_or_unit_iterate,
-       &packet);
+    if(worklist_append) {
+      gtk_tree_selection_selected_foreach(city_selection, 
+                                         worklist_append_impr_or_unit_iterate,
+                                         &packet);
+      
+    } else { 
+      gtk_tree_selection_selected_foreach(city_selection, impr_or_unit_iterate,
+                                         &packet);
+    }
     connection_do_unbuffer(&aconnection);
   }
 }
 
 /****************************************************************
 ...
 *****************************************************************/
 static void cma_iterate(GtkTreeModel *model, GtkTreePath *path,
                        GtkTreeIter *it, gpointer data)
@@ -556,18 +605,24 @@
   GtkWidget *menubar, *item;
 
   menubar = gtk_menu_bar_new();
   
   item = gtk_menu_item_new_with_mnemonic(_("_Change"));
   gtk_menu_shell_append(GTK_MENU_SHELL(menubar), item);
   city_change_command = item;
   create_change_menu(item);
 
+  item = gtk_menu_item_new_with_mnemonic(_("_Append"));
+  gtk_menu_shell_append(GTK_MENU_SHELL(menubar), item);
+  city_append_command = item;
+  create_append_menu(item);
+
+
   item = gtk_menu_item_new_with_mnemonic(_("_Select"));
   gtk_menu_shell_append(GTK_MENU_SHELL(menubar), item);
   create_select_menu(item);
 
   item = gtk_menu_item_new_with_mnemonic(_("S_how"));
   gtk_menu_shell_append(GTK_MENU_SHELL(menubar), item);
   update_view_menu(item);
   return menubar;
 }
@@ -1162,43 +1217,88 @@
   change_improvements_item = gtk_menu_item_new_with_label(_("Improvements"));
   gtk_menu_shell_append(GTK_MENU_SHELL(menu), change_improvements_item);
   change_wonders_item = gtk_menu_item_new_with_label(_("Wonders"));
   gtk_menu_shell_append(GTK_MENU_SHELL(menu), change_wonders_item);
   change_cma_item = gtk_menu_item_new_with_label(_("CMA"));
   gtk_menu_shell_append(GTK_MENU_SHELL(menu), change_cma_item);
 }
 
 /****************************************************************
+Creates the append menu.
+*****************************************************************/
+static void create_append_menu(GtkWidget *item)
+{
+  GtkWidget *menu;
+
+  menu = gtk_menu_new();
+  gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
+  g_signal_connect(menu, "show", G_CALLBACK(popup_append_menu), NULL);
+
+  append_units_item = gtk_menu_item_new_with_label(_("Units"));
+  gtk_menu_shell_append(GTK_MENU_SHELL(menu), append_units_item);
+  append_improvements_item = gtk_menu_item_new_with_label(_("Improvements"));
+  gtk_menu_shell_append(GTK_MENU_SHELL(menu), append_improvements_item);
+  append_wonders_item = gtk_menu_item_new_with_label(_("Wonders"));
+  gtk_menu_shell_append(GTK_MENU_SHELL(menu), append_wonders_item);
+}
+
+
+/****************************************************************
 ...
 *****************************************************************/
 static void popup_change_menu(GtkMenuShell *menu, gpointer data)
 {
   int n;
 
   n = 0;
   gtk_tree_selection_selected_foreach(city_selection, selected_iterate, &n);
 
   append_impr_or_unit_to_menu_item(GTK_MENU_ITEM(change_improvements_item),
-                                 FALSE, FALSE, TRUE,
+                                 FALSE, FALSE, TRUE, FALSE,
                                  city_can_build_impr_or_unit,
                                  G_CALLBACK(select_impr_or_unit_callback), n);
   append_impr_or_unit_to_menu_item(GTK_MENU_ITEM(change_units_item),
-                                 TRUE, FALSE, TRUE,
+                                 TRUE, FALSE, TRUE, FALSE,
                                  city_can_build_impr_or_unit,
                                  G_CALLBACK(select_impr_or_unit_callback), n);
   append_impr_or_unit_to_menu_item(GTK_MENU_ITEM(change_wonders_item),
-                                 FALSE, TRUE, TRUE,
+                                 FALSE, TRUE, TRUE, FALSE,
                                  city_can_build_impr_or_unit,
                                  G_CALLBACK(select_impr_or_unit_callback), n);
   append_cma_to_menu_item(GTK_MENU_ITEM(change_cma_item), TRUE);
 }
 
 /****************************************************************
+pops up the append menu.
+*****************************************************************/
+static void popup_append_menu(GtkMenuShell *menu, gpointer data)
+{
+  int n;
+
+  n = 0;
+  gtk_tree_selection_selected_foreach(city_selection, selected_iterate, &n);
+
+  append_impr_or_unit_to_menu_item(GTK_MENU_ITEM(append_improvements_item),
+                                 FALSE, FALSE, TRUE, TRUE,
+                                 city_can_build_impr_or_unit,
+                                 G_CALLBACK(select_impr_or_unit_callback), n);
+  append_impr_or_unit_to_menu_item(GTK_MENU_ITEM(append_units_item),
+                                 TRUE, FALSE, TRUE, TRUE,
+                                 city_can_build_impr_or_unit,
+                                 G_CALLBACK(select_impr_or_unit_callback), n);
+  append_impr_or_unit_to_menu_item(GTK_MENU_ITEM(append_wonders_item),
+                                 FALSE, TRUE, TRUE, TRUE,
+                                 city_can_build_impr_or_unit,
+                                 G_CALLBACK(select_impr_or_unit_callback), n);
+}
+
+
+/****************************************************************
 ...
 *****************************************************************/
 static void create_select_menu(GtkWidget *item)
 {
   GtkWidget *menu;
 
   menu = gtk_menu_new();
   gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
   g_signal_connect(menu, "show", G_CALLBACK(popup_select_menu), NULL);
@@ -1328,57 +1428,57 @@
 
   if (select_menu_cached)
     return;
 
   n = 0;
   gtk_tree_selection_selected_foreach(city_selection, selected_iterate, &n);
   gtk_widget_set_sensitive(select_island_item, (n > 0));
 
   append_impr_or_unit_to_menu_item(GTK_MENU_ITEM(select_bunit_item),
-                                 TRUE, FALSE, FALSE,
+                                 TRUE, FALSE, FALSE, FALSE,
                                  city_building_impr_or_unit,
                                  G_CALLBACK(select_impr_or_unit_callback), -1);
   append_impr_or_unit_to_menu_item(GTK_MENU_ITEM(select_bimprovement_item),
-                                 FALSE, FALSE, FALSE,
+                                 FALSE, FALSE, FALSE, FALSE,
                                  city_building_impr_or_unit,
                                  G_CALLBACK(select_impr_or_unit_callback), -1);
   append_impr_or_unit_to_menu_item(GTK_MENU_ITEM(select_bwonder_item),
-                                 FALSE, TRUE, FALSE,
+                                 FALSE, TRUE, FALSE, FALSE,
                                  city_building_impr_or_unit,
                                  G_CALLBACK(select_impr_or_unit_callback), -1);
 
   append_impr_or_unit_to_menu_item(GTK_MENU_ITEM(select_supported_item),
-                                 TRUE, FALSE, FALSE,
+                                 TRUE, FALSE, FALSE, FALSE,
                                  city_unit_supported,
                                  G_CALLBACK(select_impr_or_unit_callback), -1);
   append_impr_or_unit_to_menu_item(GTK_MENU_ITEM(select_present_item),
-                                 TRUE, FALSE, FALSE,
+                                 TRUE, FALSE, FALSE, FALSE,
                                  city_unit_present,
                                  G_CALLBACK(select_impr_or_unit_callback), -1);
   
append_impr_or_unit_to_menu_item(GTK_MENU_ITEM(select_built_improvements_item),
-                                 FALSE, FALSE, FALSE,
+                                 FALSE, FALSE, FALSE, FALSE,
                                  city_got_building,
                                  G_CALLBACK(select_impr_or_unit_callback), -1);
   append_impr_or_unit_to_menu_item(GTK_MENU_ITEM(select_built_wonders_item),
-                                 FALSE, TRUE, FALSE,
+                                 FALSE, TRUE, FALSE, FALSE,
                                  city_got_building,
                                  G_CALLBACK(select_impr_or_unit_callback), -1);
 
   append_impr_or_unit_to_menu_item(GTK_MENU_ITEM(select_improvements_item),
-                                 FALSE, FALSE, FALSE,
+                                 FALSE, FALSE, FALSE, FALSE,
                                  city_can_build_impr_or_unit,
                                  G_CALLBACK(select_impr_or_unit_callback), -1);
   append_impr_or_unit_to_menu_item(GTK_MENU_ITEM(select_units_item),
-                                 TRUE, FALSE, FALSE,
+                                 TRUE, FALSE, FALSE, FALSE,
                                  city_can_build_impr_or_unit,
                                  G_CALLBACK(select_impr_or_unit_callback), -1);
   append_impr_or_unit_to_menu_item(GTK_MENU_ITEM(select_wonders_item),
-                                 FALSE, TRUE, FALSE,
+                                 FALSE, TRUE, FALSE, FALSE,
                                  city_can_build_impr_or_unit,
                                  G_CALLBACK(select_impr_or_unit_callback), -1);
   append_cma_to_menu_item(GTK_MENU_ITEM(select_cma_item), FALSE);
 
   select_menu_cached = TRUE;
 }
 
 /****************************************************************
 ...
@@ -1386,19 +1486,21 @@
 static void city_selection_changed_callback(GtkTreeSelection *selection)
 {
   int n;
 
   n = 0;
   gtk_tree_selection_selected_foreach(selection, selected_iterate, &n);
 
   if (n == 0) {
     gtk_widget_set_sensitive(city_change_command, FALSE);
+    gtk_widget_set_sensitive(city_append_command, FALSE);
     gtk_widget_set_sensitive(city_center_command, FALSE);
     gtk_widget_set_sensitive(city_popup_command, FALSE);
     gtk_widget_set_sensitive(city_buy_command, FALSE);
   } else {
     gtk_widget_set_sensitive(city_change_command, can_client_issue_orders());
+    gtk_widget_set_sensitive(city_append_command, can_client_issue_orders());
     gtk_widget_set_sensitive(city_center_command, TRUE);
     gtk_widget_set_sensitive(city_popup_command, TRUE);
     gtk_widget_set_sensitive(city_buy_command, can_client_issue_orders());
   }
 }
Index: common/worklist.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/worklist.c,v
retrieving revision 1.14
diff -U9 -r1.14 worklist.c
--- common/worklist.c   2002/11/14 09:15:03     1.14
+++ common/worklist.c   2003/06/20 20:30:53
@@ -38,28 +38,29 @@
   strcpy(pwl->name, "a worklist");
 
   for (i = 0; i < MAX_LEN_WORKLIST; i++) {
     pwl->wlefs[i] = WEF_END;
     pwl->wlids[i] = 0;
   }
 }
 
 /****************************************************************
-...
+returns the number of entries in the worklist.
+The returned value can also be used as the next available
+index (assuming that len < MAX_LEN_WORKLIST)
 ****************************************************************/
 int worklist_length(const struct worklist *pwl)
 {
   int len = 0;
 
   if (pwl) {
-    for (len = 0; len < MAX_LEN_WORKLIST && pwl->wlefs[len] != WEF_END;
-        len++) {
-      /* nothing */
+    while (len < MAX_LEN_WORKLIST && pwl->wlefs[len] != WEF_END) {
+      len++;
     }
   }
 
   return len;
 }
 
 /****************************************************************
 ...
 ****************************************************************/
@@ -134,10 +135,30 @@
   if (idx < MAX_LEN_WORKLIST-1) {
     memmove(&pwl->wlefs[idx], &pwl->wlefs[idx+1],
            sizeof(enum worklist_elem_flag) * (MAX_LEN_WORKLIST-1-idx));
     memmove(&pwl->wlids[idx], &pwl->wlids[idx+1],
            sizeof(int) * (MAX_LEN_WORKLIST-1-idx));
   }
 
   pwl->wlefs[MAX_LEN_WORKLIST-1] = WEF_END;
   pwl->wlids[MAX_LEN_WORKLIST-1] = 0;
+}
+
+/***************************************************************
+Adds the id to the next available slot in the worklist
+Returns true if successful.
+***************************************************************/
+bool worklist_append(struct worklist *pwl, int id, bool is_unit)
+{
+  int next_index = worklist_length(pwl);
+  if (next_index >= MAX_LEN_WORKLIST) {
+    return FALSE;
+  }
+  
+  pwl->wlefs[next_index] = is_unit ? WEF_UNIT : WEF_IMPR;
+  pwl->wlids[next_index] = id;
+  
+  pwl->wlefs[next_index+1] = WEF_END;
+  pwl->wlids[next_index+1] = 0;
+
+  return TRUE;
 }
Index: common/worklist.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/worklist.h,v
retrieving revision 1.7
diff -U9 -r1.7 worklist.h
--- common/worklist.h   2002/11/15 21:24:30     1.7
+++ common/worklist.h   2003/06/20 20:30:53
@@ -39,11 +39,12 @@
 int worklist_length(const struct worklist *pwl);
 bool worklist_is_empty(const struct worklist *pwl);
 bool worklist_peek(const struct worklist *pwl, int *id, bool *is_unit);
 bool worklist_peek_ith(const struct worklist *pwl, int *id, bool *is_unit,
                      int idx);
 void worklist_advance(struct worklist *pwl);
 
 void copy_worklist(struct worklist *dst, const struct worklist *src);
 void worklist_remove(struct worklist *pwl, int idx);
+bool worklist_append(struct worklist *pwl, int id, bool is_unit);
 
 #endif /* FC__WORKLIST_H */

[Prev in Thread] Current Thread [Next in Thread]